-
Notifications
You must be signed in to change notification settings - Fork 1
/
update.py
125 lines (89 loc) · 3.78 KB
/
update.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
"""
For information about how the program works, see the program notes file:
https://github.com/KatherineMichel/til-100-days-of-code-version/blob/master/program_notes.py
"""
import os
from datetime import datetime
from twython import Twython
twitter = Twython(
os.environ.get("APP_KEY"),
os.environ.get("APP_SECRET"),
os.environ.get("OAUTH_TOKEN"),
os.environ.get("OAUTH_TOKEN_SECRET"),
)
HEADER = """\
# TIL- 100 Days of Code Version
See my [TIL- 100 Days of Code Version](https://github.com/KatherineMichel/portfolio/blob/master/regular-blog-posts/til-100-days-of-code-version.md) blog post for information.
Before I created this app, I had already completed a considerable amount of 100 Days of Code. Although I may resume at some point and finish the 100 days within the TIL itself, it will be in excess of the 100 days.
"""
FOOTER = """\
## License
This project is adapted from a [Today I Learned](https://github.com/khanhicetea/today-i-learned/) project by [@khanhicetea](https://github.com/khanhicetea), distributed under the [Creative Commons Attribution License](http://creativecommons.org/licenses/by/3.0/).
See the [program_information.py](program_information.py) file for information about the changes I made.
"""
def main():
root = "."
excludes = ["archive", "drafts"]
categories = [
dir
for dir in os.listdir(root)
if os.path.isdir(dir) and dir not in excludes and not dir.startswith(".")
]
categories.sort()
content = ""
recent_content = ""
cat_content = ""
recent_tils = []
count = 0
content += HEADER
cat_content += "| **By Category** | :books: |\n| -------- | -------- |\n"
for cat in categories:
cat_tils = []
for file in os.listdir(os.path.join(root, cat)):
raw = read_files(os.path.join(root, cat, file))
parts = raw.split("/---/")
for part in parts:
til = parse_til(part.strip(), cat)
til["file_name"] = file
cat_tils.append(til)
recent_tils.append(til)
cat_tils.sort(key=lambda a: a["date"])
cat_content += f"| **{cat.capitalize()}** [ {len(cat_tils)} Tils ] | |\n"
for til in cat_tils:
count += 1
cat_content += f"| {count}. [{til['title']}]({til['category']}/{til['file_name']}) | {til['date'].strftime('%Y-%m-%d')} |\n"
cat_content += "\n"
recent_tils.sort(reverse=True, key=lambda a: a["date"])
recent_content += "| **5 Most Recent TILs** | :tada: |\n| -------- | -------- |\n"
for til in recent_tils[0:5]:
recent_content += f"| [{til['title']}]({til['category']}/{til['file_name']}) [{til['category']}] | {til['date'].strftime('%Y-%m-%d')} |\n"
recent_content += "\n"
content += recent_content
content += cat_content
content += FOOTER
with open("README.md", "w") as fd:
fd.write(content)
status = recent_tils[0]["status"]
tweet = status + " (auto-tweeted by my TIL bot)"
print(tweet)
# twitter.update_status(status=tweet)
def read_files(file_path):
with open(file_path, "r") as fd:
return fd.read()
def parse_til(content, category):
pos1 = content.find("- Date : ")
pos2 = content.find("- Tags : ", pos1)
pos3 = content.find("- Status : ", pos2)
pos4 = content.find("\n", pos3)
pos5 = content.find("##", pos4)
pos6 = content.find("\n", pos5)
post = {
"category": category,
"date": datetime.strptime(content[pos1 + 9 : pos2].strip(), "%Y-%m-%d"),
"tags": [t[1:] for t in content[pos2 + 9 : pos3].strip().split(" ")],
"status": content[pos3 + 11 : pos4].strip(),
"title": content[pos5 + 3 : pos6].strip(),
}
return post
if __name__ == "__main__":
main()