-
Notifications
You must be signed in to change notification settings - Fork 0
/
mptest.py
executable file
·355 lines (296 loc) · 13.3 KB
/
mptest.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
from multiprocessing import Process, Queue, Manager, Pool
import multiprocessing as mp
import random
import sys
import os
import json
import time
import string
from multiprocessing.dummy import Pool as ThreadPool
from instaloader import InstaloaderContext, Post
from typing import Any, Callable, Dict, Tuple, Iterator, List, Optional, Union
from instaloader.structures import PostComment, PostCommentAnswer, Profile
result = []
class TPost(Post):
def __init__(self, context: InstaloaderContext, node: Dict[str, Any],
owner_profile: Optional['Profile'] = None):
assert 'shortcode' in node or 'code' in node
self._context = InstaloaderContextTommy()
self._node = node
self._owner_profile = owner_profile
self._full_metadata_dict = None # type: Optional[Dict[str, Any]]
self._rhx_gis_str = None # type: Optional[str]
self._location = None # type: Optional[PostLocation]
self.comments_list = []
@property
def ownerid(self) -> int:
"""The ID of the post owner."""
return self._node["owner"]["id"]
@property
def comment_count(self):
return self._node["edge_media_to_comment"]["count"]
def get_comments(self) -> Iterator[PostComment]:
r"""Iterate over all comments of the post.
Each comment is represented by a PostComment namedtuple with fields text (string), created_at (datetime),
id (int), owner (:class:`Profile`) and answers (:class:`~typing.Iterator`\ [:class:`PostCommentAnswer`])
if available.
"""
print("My: get_comments")
def _postcommentanswer(node):
return PostCommentAnswer(id=int(node['id']),
created_at_utc=datetime.utcfromtimestamp(node['created_at']),
text=node['text'],
owner=Profile(self._context, node['owner']))
def _postcommentanswers(node):
if 'edge_threaded_comments' not in node:
return
answer_count = node['edge_threaded_comments']['count']
if answer_count == 0:
# Avoid doing additional requests if there are no comment answers
return
answer_edges = node['edge_threaded_comments']['edges']
if answer_count == len(answer_edges):
# If the answer's metadata already contains all comments, don't do GraphQL requests to obtain them
yield from (_postcommentanswer(comment['node']) for comment in answer_edges)
return
yield from (_postcommentanswer(answer_node) for answer_node in
self._context.graphql_node_list("51fdd02b67508306ad4484ff574a0b62",
{'comment_id': node['id']},
'https://www.instagram.com/p/' + self.shortcode + '/',
lambda d: d['data']['comment']['edge_threaded_comments']))
def _postcomment(node):
return PostComment(*_postcommentanswer(node),
answers=_postcommentanswers(node))
if self.comments < 4:
# Avoid doing additional requests if there are no comments
return
try:
comment_edges = self._field('edge_media_to_parent_comment', 'edges')
answers_count = sum([edge['node']['edge_threaded_comments']['count'] for edge in comment_edges])
threaded_comments_available = True
except KeyError:
comment_edges = self._field('edge_media_to_comment', 'edges')
answers_count = 0
threaded_comments_available = False
if self.comments == len(comment_edges) + answers_count:
# If the Post's metadata already contains all parent comments, don't do GraphQL requests to obtain them
yield from (_postcomment(comment['node']) for comment in comment_edges)
return
yield from (_postcomment(node) for node in
self._context.graphql_node_list(
"97b41c52301f77ce508f55e66d17620e" if threaded_comments_available
else "f0986789a5c5d17c2400faebf16efd0d",
{'shortcode': self.shortcode},
'https://www.instagram.com/p/' + self.shortcode + '/',
lambda d:
d['data']['shortcode_media'][
'edge_media_to_parent_comment' if threaded_comments_available else 'edge_media_to_comment'],
self._rhx_gis))
def _obtain_metadata(self):
print("my: obtain_metadata")
# print(self.shortcode)
if not self._full_metadata_dict:
pic_json = self._context.get_json("p/{0}/".format(self.shortcode), params={})
self._full_metadata_dict = pic_json['entry_data']['PostPage'][0]['graphql']['shortcode_media']
self._rhx_gis_str = pic_json.get('rhx_gis')
if self.shortcode != self._full_metadata_dict['shortcode']:
self._node.update(self._full_metadata_dict)
raise PostChangedException
class InstaloaderTommy(Instaloader):
def __init__(self):
super().__init__('Instaloader')
self.context = InstaloaderContextTommy()
self.download_comments=True
def update_comments(self, filename: str, post: Post) -> None:
print("NEW update_comments")
def _postcommentanswer_asdict(comment,parentId):
print("NEW _postcommentanswer")
# update_comments_db(comment,post,parentId)
return {'id': comment.id,
'created_at': int(comment.created_at_utc.replace(tzinfo=timezone.utc).timestamp()),
'text': comment.text,
'owner': comment.owner._asdict()}
def _postcomment_asdict(comment):
print("NEW _postcomment")
return {**_postcommentanswer_asdict(comment,comment.id),
'answers': sorted([_postcommentanswer_asdict(answer,comment.id) for answer in comment.answers],
key=lambda t: int(t['id']),
reverse=True)}
def get_unique_comments(comments, combine_answers=False):
if not comments:
return list()
comments_list = sorted(sorted(list(comments), key=lambda t: int(t['id'])),
key=lambda t: int(t['created_at']), reverse=True)
unique_comments_list = [comments_list[0]]
for x, y in zip(comments_list[:-1], comments_list[1:]):
if x['id'] != y['id']:
unique_comments_list.append(y)
elif combine_answers:
combined_answers = unique_comments_list[-1].get('answers') or list()
if 'answers' in y:
combined_answers.extend(y['answers'])
unique_comments_list[-1]['answers'] = get_unique_comments(combined_answers)
return unique_comments_list
filename += '_comments.json'
try:
with open(filename) as fp:
comments = json.load(fp)
except (FileNotFoundError, json.decoder.JSONDecodeError):
comments = list()
comments.extend(_postcomment_asdict(comment) for comment in post.get_comments())
if comments:
comments = get_unique_comments(comments, combine_answers=True)
answer_ids = set(int(answer['id']) for comment in comments for answer in comment.get('answers', []))
with open(filename, 'w') as file:
file.write(json.dumps(list(filter(lambda t: int(t['id']) not in answer_ids, comments)), indent=4))
self.context.log('comments', end=' ', flush=True)
def get_hashtag_posts(self, hashtag: str, resume=False, end_cursor=False) -> Iterator[Post]:
"""Get Posts associated with a #hashtag."""
print("get_hashtag_posts")
has_next_page = True
if resume:
end_cursor = get_end_cursor(hashtag)
# print(str(end_cursor))
while has_next_page:
if end_cursor:
params = {'__a': 1, 'max_id': end_cursor}
else:
params = {'__a': 1}
hashtag_data = self.context.get_json('explore/tags/{0}/'.format(hashtag),
params)['graphql']['hashtag']['edge_hashtag_to_media']
end_cursor = hashtag_data['page_info']['end_cursor']
dump_page_json(end_cursor,hashtag_data,hashtag)
# print(str(len(hashtag_data['edges'])))
#yield from (Post(self.context, edge['node']) for edge in hashtag_data['edges'])
yield len(hashtag_data['edges'])
has_next_page = hashtag_data['page_info']['has_next_page']
if end_cursor and has_next_page:
print(bcolors.OKBLUE + "has next page" + bcolors.ENDC)
# print("end_cursor: "+str(end_cursor))
update_end_cursor(end_cursor,hashtag, has_next_page)
else:
update_end_cursor("",hashtag,0)
def load_json_from_fileobj(file):
global result
jfile = json.loads(file)
result.append(jfile)
def fill_file_list(file, files):
with open(file, 'r') as fp:
files.append(fp.read())
def single_read():
start = time.time()
pool = Pool(2)
jobs = []
for file in os.scandir(path):
with open(file, 'r') as fp:
f = fp.read()
p = Process(target=load_json_from_fileobj, args=(f,))
jobs.append(p)
p.start()
p.join()
print("single: end jobs[] len: " + str(len(jobs)))
end = time.time()
print(end - start)
def load_json_page(file):
with open(file, 'r') as fp:
f = fp.read()
return json.loads(f)
# posts.append(json.loads(f))
def load_json_posts_file_dir(path, posts):
for file in os.scandir(path):
with open(file, 'r') as fp:
f = fp.read()
posts.append(json.loads(f))
def single_thread_test(path, posts):
start = time.time()
for dir in os.scandir(path):
load_json_posts_file_dir(dir, posts)
print("single: end posts len: " + str(len(posts)))
# end = time.time()
print(time.time() - start)
def multi_threads_test(path):
start = time.time()
with Manager() as manager:
posts = manager.list()
# queue = Queue()
processes = []
for dir in os.scandir(path):
for file in os.scandir(dir):
processes.append(
Process(target=load_json_page, args=(file, posts)))
for p in processes:
p.start()
for p in processes:
p.join()
print("multi: end posts len: " + str(len(posts)))
print(time.time() - start)
def async_test(posts):
start = time.time()
with Manager() as manager:
pool = Pool(processes=4)
posts = manager.list()
multiple_results = [
pool.apply_async(
load_json_posts_file_dir, args=[dir, posts]
) for dir in os.scandir(path)]
# print([res.get(timeout=1) for res in multiple_results])
print(len(posts))
print(len(shared_list))
print(time.time() - start)
def remove_stopwords(text, stopword):
text = [word for word in text if word not in stopword]
return text
def extract_hashtags(caption):
regexp = re.compile(r"(?:#)(\w(?:(?:\w|(?:\.(?!\.))){0,28}(?:\w))?)")
tags = []
def repl(m):
tags.append(m.group(0))
return ""
caption = regexp.sub(repl, caption.lower())
return caption, tags
def remove_punct(text):
translator = str.maketrans('', '', string.punctuation)
return text.translate(translator)
def tokenize(text):
text = re.split('\W+', text)
return text
def pre_proc_text(caption):
from nltk.corpus import stopwords
stopwords = stopwords.words('english')
caption = re.split('\W+', caption)
caption, tags = extract_hashtags(post['caption'])
caption, mentions = extract_mentions(caption)
caption = remove_punct(caption)
caption = tokenize(caption)
caption = remove_stopwords(caption, stopwords)
caption = ' '.join(caption)
return caption
if __name__ == "__main__":
scraper = InstaloaderTommy()
if len(sys.argv) > 1:
path = sys.argv[1]
files = []
single_thread_test(path, files)
posts = []
for f in files:
for edge in f:
posts.append(TPost(scraper.context, edge['node']))
print(len(files))
print(len(posts))
# pool = ThreadPool(len(files))
# posts = [x for x in pool.map(load_json_page, files) if x is not None]
# print(len(posts))
# with Manager() as manager:
# posts = manager.list()
# pool = manager.Pool()
# for i in files:
# pool.apply_async(load_json_page, args=(i, posts))
# # pool.apply_async(load_json_page, args=(i, posts), callback=log_result)
# pool.close()
# pool.join()
# print(str(len(posts)))
# multi_threads_test(path)
# async_test(path)
# print("posts: " + len(posts))
# single_read()
# print("single: end result[] len: " + str(len(result)))