-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.py
executable file
·206 lines (174 loc) · 7.41 KB
/
parser.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
from insert2DB import *
from utils import *
def parse_comment_tags(tag_dict_arr):
tags_arr = []
if tag_dict_arr:
for tag_dict in tag_dict_arr:
tags_arr.append(tag_dict['name'])
return tags_arr
def parse_quotes(quote, href):
try:
start = quote['startOffset']
end = quote['endOffset']
paras = quote['paragraphs']
except KeyError:
print("quote key error with url: "+href, file=sys.stderr)
return None, None
content = ''
corrStnMediumIDs = []
for para in paras:
content += para['text']
corrStnMediumIDs.append(para['name'])
content = content[start:end]
return content, corrStnMediumIDs
def parse_sentence(uid, articleID, href, session):
tree = load_html(session, href)
if tree is None: return
section = tree.xpath('//section/div[@class="section-content"]')
for sec in section:
body = sec.xpath('./div[contains(@class,"section-inner")]/*')
parse_para(body, uid, articleID)
def parse_para(body, uid, articleID):
for para in body:
sentence = para.text_content()
try: key = para.xpath('@id')[0]
except:
sub_body = para.xpath('./*')
parse_para(sub_body, uid, articleID)
continue
if sentence == "" or not key: continue
save_sentence({
'content': sentence,
'id': key,
'articleID': articleID
})
def parse_highlight(uid, articleID, session):
href = 'https://medium.com/p/'+uid+'/quotes'
resp_data = load_json(session, href)
if resp_data is None: return
for highlight in resp_data['value']:
content, corrStnMediumIDs = parse_quotes(highlight, href)
if content is None: continue
save_highlight({
'content': content,
'corrStnMediumIDs': corrStnMediumIDs,
'numLikes': highlight.get('count', -1),
'articleMediumID': uid
}, articleID)
def parse_responseStream(uid, session, href, references):
if 'Post' in references and 'User' in references:
comm_data = references['Post']
user_data = references['User']
else: return
commment_dict = {}
comment_map = {}
# parse all comments
for self_article_mediumID, value in comm_data.items():
if exist_article(self_article_mediumID): continue
try:
comment_full = value['previewContent']['isFullContent']
comment_paras = value['previewContent']['bodyModel']['paragraphs']
creator_id = value['creatorId']
numLikes = value['virtuals']['totalClapCount']
num_responses = value['virtuals']['responsesCreatedCount']
recommends = value['virtuals']['recommends']
tag_arr = parse_comment_tags(value['virtuals']['tags'])
comment_title = value['title']
media_id = value['inResponseToMediaResourceId']
corr_article_mediumID = value['inResponseToPostId']
timestamp = convert_unixtime(value['latestPublishedAt'])
except KeyError:
print("id key error with url: "+href, file=sys.stderr)
continue
try:
username = user_data[creator_id]['username']
authorID = save_author({
'name': user_data[creator_id]['name'],
'mediumID': creator_id,
'username': username,
'bio': user_data[creator_id]['bio']
})
except KeyError: print("comment author key error with url: "+href, file=sys.stderr)
self_articleID = save_article({
'mediumID': self_article_mediumID,
'authorMediumID': creator_id,
'recommends' : recommends,
'title': comment_title,
'time': timestamp,
'tags': tag_arr,
'numLikes': numLikes
}, authorID=authorID)
if comment_full:
for comm_para in comment_paras:
save_sentence({
'content': comm_para['text'],
'id': comm_para['name'],
'articleID': self_articleID
})
else:
unique_slug = value['uniqueSlug']
comment_href = 'https://medium.com/@'+username+'/'+unique_slug
parse_sentence(self_article_mediumID, self_articleID, comment_href, session)
parse_highlight(self_article_mediumID, self_articleID, session)
if num_responses > 0: parse_comment(self_article_mediumID, session)
if media_id != '': commment_dict[media_id] = self_article_mediumID
comment_map[self_article_mediumID] = {
'selfArticleID': self_articleID,
'corrArticleMediumID':corr_article_mediumID,
'corrHighlightID':-1
}
# parse quote
if 'MediaResource' in references and 'Quote' in references:
media_data=references['MediaResource']
quote_data=references['Quote']
for mediaResourceId, media in media_data.items():
if 'mediumQuote' in media:
media_id = str(mediaResourceId)
self_article_mediumID = commment_dict.get(media_id)
if self_article_mediumID is None: continue
# print(mediaResourceId)
quote = quote_data[media['mediumQuote']['quoteId']]
corr_article_mediumID = quote['postId']
content, corrStnMediumIDs = parse_quotes(quote, href)
if content is None: continue
highlightID = exist_highlight(corr_article_mediumID, content)
if highlightID == -1:
highlightID = save_highlight({
'content': content,
'corrStnMediumIDs': corrStnMediumIDs,
'numLikes': -1,
'articleMediumID': corr_article_mediumID
})
comment_map[self_article_mediumID]['corrHighlightID'] = highlightID
for _, comment_obj in comment_map.items(): save_comment(comment_obj)
def parse_comment(uid, session):
href = "https://medium.com/_/api/posts/"+uid+"/responsesStream"
resp_data = load_json(session, href)
if resp_data is None: return
references = resp_data['references']
paging = resp_data['paging']
parse_responseStream(uid, session, href, references)
while 'next' in paging:
resp_data = load_json(session, href, params=paging['next'])
if resp_data is None: break
references = resp_data['references']
paging = resp_data['paging']
parse_responseStream(uid, session, href, references)
def parse_topicStream(references, session):
for mediumID, value in references.get('Post', {}).items():
if not exist_article(mediumID):
parse_comment(mediumID, session)
def parse(href, session, uid=None, articleID=None, tree=None):
if not uid: uid = parse_uid(href)
parse_comment(uid, session)
if __name__ == '__main__':
if len(sys.argv) == 3:
parse(sys.argv[1], int(sys.argv[2]))
else:
# sys.stdout = open('logs/std.log', 'w')
# sys.stderr = open('logs/err.log', 'w')
initdb()
session = load_obj('./objects/login.obj')
# session = login()
# parse('https://timeline.com/it-was-sex-all-the-time-at-this-1800s-commune-with-anyone-you-wanted-and-none-of-the-guilt-c7ea4734e9ca', session)
parse('https://medium.com/@beyondtherobot/my-real-life-superpower-c2a9b309cf7', session)