Created
August 28, 2022 11:28
-
-
Save Hiyorimi/5053c0d5fa9e40b1a958fe58f0895e3f to your computer and use it in GitHub Desktop.
Daily Notes hashtag updater
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import os | |
import datetime as dt | |
import glob | |
from typing import List | |
def get_date_from_filename(filename: str) -> dt.date: | |
print(filename) | |
date_string = filename.split('.md')[0][2:] | |
file_date = dt.datetime.strptime(date_string, '%Y-%m-%d') | |
print(file_date) | |
return file_date | |
def get_hashtag_string_from_date(date: dt.date) -> str: | |
year_string = date.isocalendar()[0] | |
week_string = date.isocalendar()[1] | |
hashtag_string = f'#w{week_string}_{year_string} #y{year_string}\n' | |
print(hashtag_string) | |
return hashtag_string | |
def add_hashtag_time_to_note(hashtag_string: str, file_content: List[str]) -> List[str]: | |
updated_content = [ | |
file_content[0], | |
'\n', | |
hashtag_string, | |
*file_content[1:], | |
] | |
return updated_content | |
for filename in glob.glob('./2020-*'): | |
print(filename) | |
updated_content: List[str] = [] | |
with open(filename, 'r') as fp: | |
lines = fp.readlines() | |
print(lines) | |
date: dt.date = get_date_from_filename(filename) | |
hashtag_string: str = get_hashtag_string_from_date(date) | |
updated_content = add_hashtag_time_to_note(hashtag_string, lines) | |
print(updated_content) | |
with open(filename, 'w') as fw: | |
fw.write(''.join(updated_content)) | |
exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment