url2markdown/url2markdown/file.py

21 lines
592 B
Python
Raw Normal View History

2020-10-13 23:11:47 +02:00
import os.path
2020-10-12 22:25:47 +02:00
import markdownify
2020-10-13 23:11:47 +02:00
def _build_path(title):
2020-11-09 12:32:32 +01:00
ressources_folder = "~/03_documents/ressources/articles/"
2020-10-13 23:11:47 +02:00
base_filename = str(title)
short_path = os.path.join(ressources_folder, base_filename + ".md")
return os.path.expanduser(short_path)
2020-10-12 22:25:47 +02:00
def write_to_file(article, header):
2020-10-13 23:11:47 +02:00
file_path = _build_path(article.title)
2020-10-13 21:17:05 +02:00
markdown = markdownify.markdownify(article.html, heading_style='ATX')
2020-10-13 23:11:47 +02:00
with open(file_path, 'w') as f:
2020-10-12 22:25:47 +02:00
f.write(header)
f.write('------')
f.write('\n')
2020-10-13 23:11:47 +02:00
with open(file_path, 'a') as f:
2020-10-12 22:25:47 +02:00
f.write(markdown)