-
Notifications
You must be signed in to change notification settings - Fork 0
/
converter.py
191 lines (149 loc) · 6.97 KB
/
converter.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import datetime
import json
import os
import re
from bs4 import BeautifulSoup
dataFolder = os.path.abspath('') + "/data/"
dateFormat = "%d-%m-%Y--%H-%M-%S"
def validateDateFormat(dateText):
try:
datetime.datetime.strptime(dateText, dateFormat)
except ValueError:
return False
return True
def removeHTMLTags(text):
clean = re.compile('<.*?>')
return re.sub(clean, '', text)
def convert(jsonData={}, timestamp="", filePaths=[]):
database = json.loads(
open(dataFolder + "database.json", encoding="utf-8").read())
def convertJsonData(jsonData, timestamp=""):
dataFromDate = datetime.datetime.strptime(
timestamp, dateFormat).strftime(dateFormat)
for index, news in enumerate(jsonData["news"]):
if not 'sophoraId' in news:
continue
# if news article in database with same sophoraId already exists,
# then just update lastDownload property
if any(x['sophoraId'] == news['sophoraId'] for x in database):
for x in database:
if (x['sophoraId'] == news['sophoraId']):
x["lastDownload"] = dataFromDate
if 'rankings' not in x:
x['rankings'] = []
# add new ranking
x["rankings"].append(
{"timestamp": dataFromDate, "score": index+1})
continue
newsObject = {
"sophoraId": news['sophoraId'],
"title": news["title"] if "title" in news else "",
"content": "",
"date": news['date'] if 'date' in news else "",
"firstDownload": dataFromDate,
"lastDownload": dataFromDate,
"author": "",
"sender": "",
"tags": [],
"geotags": [],
"linkBoxes": [],
"related": [],
"ressort": news['ressort'] if 'ressort' in news else "",
"breakingNews": news['breakingNews'] if 'breakingNews' in news else "",
"category": "",
"rankings": []
}
# add ranking
if index not in newsObject["rankings"]:
newsObject["rankings"].append(
{"timestamp": dataFromDate, "score": index+1})
# add tags
if 'tags' in news:
for tag in news['tags']:
newsObject["tags"].append(tag["tag"])
# add geotags
if 'geotags' in news:
for geotag in news['geotags']:
newsObject["geotags"].append(geotag["tag"])
# add category (from tracking)
if 'tracking' in news:
topicTree = news["tracking"][0]["sid"].split('.')
topicTree.pop(0)
topicTree.pop(-1)
newsObject["category"] = "/".join(topicTree)
# handling content
if 'content' in news:
for contentSection in news["content"]:
if (contentSection["type"] == "text" or contentSection["type"] == "headline"):
sectionValueRaw = contentSection["value"]
sectionValue = removeHTMLTags(
sectionValueRaw).replace("\"", "")
# add author
if (sectionValueRaw.startswith("<em>Von")):
newsFrom = sectionValue.replace(
'Von ', '').split(', ')
newsObject["author"] = newsFrom.pop(0)
newsObject["sender"] = ", ".join(newsFrom)
continue
# add content
newsObject["content"] += sectionValue if newsObject["content"] == "" else "\n" + sectionValue
# add linked articles
elif contentSection["type"] == "box":
contentSectionBox = contentSection["box"]
linkBox = {
"title": contentSectionBox["title"] if "title" in contentSectionBox else "",
"subtitle": contentSectionBox["subtitle"] if "subtitle" in contentSectionBox else "",
"sophoraId": "",
}
if "link" in contentSectionBox:
soup = BeautifulSoup(
contentSectionBox["link"], "html.parser")
links = [a['href']
for a in soup.find_all('a', href=True)]
linkBox["sophoraId"] = links[0].split(
'/')[-1].replace('.json', '')
newsObject["linkBoxes"].append(linkBox)
# add related articles
elif contentSection["type"] == "related":
for relatedArticle in contentSection["related"]:
relatedObject = {
"title": relatedArticle["title"].replace("\"", '"') if "title" in relatedArticle else "",
"subtitle": relatedArticle["subtitle"] if "subtitle" in relatedArticle else "",
"sophoraId": relatedArticle["sophoraId"] if "sophoraId" in relatedArticle else "",
"date": relatedArticle["date"] if "date" in relatedArticle else ""
}
newsObject["related"].append(relatedObject)
database.append(newsObject)
# if filePaths were given,
# then use their associated data for converting
if len(filePaths) > 0:
for filePath in filePaths:
file = open(filePath, encoding="utf-8")
fileName = os.path.basename(file.name).split('.')[0]
# check that fileName matches dateformat
if not validateDateFormat(fileName):
continue
fileJsonData = json.loads(file.read())
# check that fileJsonData has property named 'news'
if not 'news' in fileJsonData:
continue
convertJsonData(fileJsonData, timestamp=fileName)
# no filePaths were given
# use jsonData instead
else:
convertJsonData(jsonData, timestamp=timestamp)
file = open(dataFolder + "database.json", "w")
file.write(json.dumps(database, ensure_ascii=False, indent=4))
file.close()
def createDatabaseDir():
if not os.path.exists(dataFolder):
os.mkdir(dataFolder)
if not os.path.isfile(dataFolder + "database.json"):
open(dataFolder + "database.json", "x")
file = open(dataFolder + "database.json",
"w")
file.write(json.dumps([], ensure_ascii=False, indent=4))
file.close()
if __name__ == "__main__":
createDatabaseDir()
convert()