-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.py
160 lines (130 loc) · 4.6 KB
/
parse.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import json
from xml.dom import minidom
from glob import glob
from pathlib import Path
from config import RSS_JSON_PATH, RAW_XML_DIR, RAW_JSON_DIR, RULE_PATH, RSS_XML_PATH
from datetime import datetime
def parse_item(item):
title = item.getElementsByTagName("title")[0].firstChild.nodeValue
link = item.getElementsByTagName("link")[0].firstChild.nodeValue
description = item.getElementsByTagName("description")[0].firstChild.nodeValue
guid = item.getElementsByTagName("guid")[0].firstChild.nodeValue
categories = [c.firstChild.nodeValue for c in item.getElementsByTagName("category")]
pubdate = item.getElementsByTagName("pubDate")[0].firstChild.nodeValue
announce_type = item.getElementsByTagName("arxiv:announce_type")[
0
].firstChild.nodeValue
rights = item.getElementsByTagName("dc:rights")[0].firstChild.nodeValue
creator = item.getElementsByTagName("dc:creator")[0].firstChild.nodeValue
# preprocess
description = description.split("Abstract: ")[1]
entry = {
"title": title,
"link": link,
"description": description,
"guid": guid,
"categories": categories,
"pubdate": pubdate,
"announce_type": announce_type,
"rights": rights,
"creator": creator,
}
return entry
def parse_xml() -> list[dict]:
xml_files = glob(f"{RAW_XML_DIR}/*.xml")
entrys = []
for file in xml_files:
cat_entrys = []
with open(file, "r") as f:
xml = minidom.parse(f)
items = xml.getElementsByTagName("item")
for item in items:
try:
entry = parse_item(item)
except Exception as e:
print(e)
continue
cat_entrys.append(entry)
filepath = Path(file)
with open(f"{RAW_JSON_DIR}/{filepath.stem}.json", "w") as f:
json.dump(cat_entrys, f)
entrys.extend(cat_entrys)
return entrys
def load_rules():
with open(RULE_PATH, "r") as f:
return json.load(f)
def _filter(entry, rule):
if isinstance(rule, str):
if rule.startswith("%") and rule.endswith("%"):
rule = rule[1:-1]
return rule.lower() in (
entry["title"] + entry["description"]
).lower().split(" ")
if rule.lower() in (entry["title"] + entry["description"]).lower():
return True
else:
return False
if isinstance(rule, list):
return any([_filter(entry, r) for r in rule])
if isinstance(rule, dict):
if rule["type"] == "AND":
return all([_filter(entry, r) for r in rule["rules"]])
elif rule["type"] == "NOT":
return not _filter(entry, rule["rule"])
raise ValueError(f"rule type not supported: {rule}, {type(rule)}")
def filter(entry, rules):
"""
str: include
list: OR
{ 'type' : 'AND' , 'rules' : [] }: AND
{ 'type' : 'NOT' , 'rule' : [] }: NOT
"""
for rule in rules:
if _filter(entry, rule):
return True
return False
def add_text_element(doc, parent, tag_name, text):
element = doc.createElement(tag_name)
element.appendChild(doc.createTextNode(text))
parent.appendChild(element)
def export_rss_xml(rss_entrys):
doc = minidom.Document()
rss = doc.createElement("rss")
doc.appendChild(rss)
channel = doc.createElement("channel")
rss.appendChild(channel)
channel_info = {
"title": "arxiv-rss",
"link": "",
"description": "",
"docs": "",
"language": "en-us",
"lastBuildDate": datetime.now().strftime("%a, %d %b %Y %H:%M:%S %z"),
"managingEditor": "",
"pubDate": datetime.now().strftime("%a, %d %b %Y %H:%M:%S %z"),
}
for key, value in channel_info.items():
add_text_element(doc, channel, key, value)
for item_data in rss_entrys:
item = doc.createElement("item")
channel.appendChild(item)
for key, value in item_data.items():
if key == "categories":
for category in value:
add_text_element(doc, item, "category", category)
else:
add_text_element(doc, item, key, value)
channel.appendChild(item)
fp = open(RSS_XML_PATH, "w")
doc.writexml(fp, addindent=" ", newl="\n")
fp.close()
def export_rss():
rules = load_rules()
rss_entrys = []
entrys = parse_xml()
for entry in entrys:
if filter(entry, rules):
rss_entrys.append(entry)
with open(RSS_JSON_PATH, "w") as f:
json.dump(rss_entrys, f)
export_rss_xml(rss_entrys)