-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathupdateNews.py
More file actions
executable file
·66 lines (42 loc) · 1.45 KB
/
updateNews.py
File metadata and controls
executable file
·66 lines (42 loc) · 1.45 KB
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
#!/usr/bin/env python3
import argparse
import collections
import os
from datetime import datetime
parser = argparse.ArgumentParser(description = "Update news")
parser.add_argument("-p", "--push", help = "Automatically push to github repo", action = "store_true")
parser.add_argument("-n", "--num", help = "Number of news items to display", default = 5)
args = parser.parse_args()
args.num = int(args.num)
def convert2Html(k,v):
html = "<li> "
v = v.replace("{", '<a href="')
v = v.replace("}", '">')
v = v.replace("[", "")
v = v.replace("]", "</a>")
html = html + v + " [" + datetime.strftime(k, "%b %Y") + "]" + "</li>"
return(html)
newsFile = open("news.txt", 'r')
newsItems = {}
for i, line in enumerate(newsFile):
l = line.replace('\n', '').split(";")
text = l[0]
date = l[1].replace(' ', '')
date = datetime.strptime(date, "%m/%d/%Y")
newsItems[date] = text
formattedItems = {}
for k, v in newsItems.items():
formattedItems[k] = convert2Html(k,v)
od = collections.OrderedDict(sorted(formattedItems.items(), reverse = True))
newsFile.close()
htmlFile = open("_includes/news.html", "w")
if (args.num > len(formattedItems.keys())):
args.num = len(formattedItems.keys())
lst_di = list(od.items())
for i in range(0, args.num):
htmlFile.write(lst_di[i][1] + '\n')
htmlFile.close()
if (args.push):
os.system("git add --all")
os.system("git commit -m 'update news'")
os.system("git push")