-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautorss.py
More file actions
67 lines (67 loc) · 2.14 KB
/
autorss.py
File metadata and controls
67 lines (67 loc) · 2.14 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
65
66
67
import sys
import os
import datetime
import xml.etree.ElementTree as ET
post_file = ""
rss_file = "rsstest.xml"
lines = []
today = datetime.datetime.now()
post_number = str(sys.argv[1])
for file in os.listdir("posts/"):
if file.startswith(post_number):
post_file = file
if post_file == "":
raise Exception("File not found")
else:
print(post_file)
with open("./posts/"+post_file, 'r', encoding='UTF-8') as file:
while (line := file.readline().rstrip()):
lines.append(line)
tree = ET.parse(rss_file)
root = tree.getroot()
root = root[0]
item = ET.Element("item")
title = ET.Element("title")
title.text = lines[1].replace("#TITL","")
link = ET.Element("link")
link.text = "https://blog.pouekdev.one/?post="+str(post_file)
guid = ET.Element("guid")
guid.text = "?post="+str(post_file)
guid.set("isPermaLink","false")
day_name = today.strftime("%A")
day_name = day_name[0:3]
day_name += ","
day_number = today.strftime("%d")
month = today.strftime("%B")
month = month[0:3]
year = today.strftime("%Y")
other = today.strftime("%H:%M:%S")
pdate = ET.Element("pubDate")
pdate.text = day_name + " " + day_number + " " + month + " " + year + " " + other + " +0100"
for line in lines:
try:
line.index("#TEXT")
except ValueError:
try:
line.index("#DESC")
except ValueError:
pass
else:
description_text = line.replace("#DESC","")
break
else:
description_text = line.replace("#TEXT","")
description_text = description_text.split("~")
description_text = description_text[0]
break
description_text = description_text[:-1]
description_text += " [...]"
description = ET.Element("description")
description.text = description_text
item.insert(0,title)
item.insert(1,link)
item.insert(2,guid)
item.insert(3,pdate)
item.insert(4,description)
root.insert(4,item)
tree.write(rss_file, "UTF-8")