url2markdown/url2markdown/file.py

30 lines
884 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
def _clean_title(raw_title):
title = str(raw_title)
bad_characters = ['«', '»', ',', '.', "'", "`",
"?", "", "{", "}", "(", ")", "[", "]", "!", "$"]
for character in bad_characters:
title = title.replace(character, '')
return title
2020-10-13 23:11:47 +02:00
def _build_path(title):
2020-11-11 19:51:55 +01:00
ressources_folder = "~/03_documents/obsidian/articles/"
base_filename = _clean_title(title)
2020-10-13 23:11:47 +02:00
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)