This repository has been archived by the owner on Dec 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extractor.py
277 lines (223 loc) · 8.12 KB
/
extractor.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
#
#
# This file contains the main entrypoint
# and contains the logic that calls the API
#
#
import argparse
import base64
import logging
import os
from datetime import datetime
from typing import Tuple
import config
from askfm_api import AskfmApi, AskfmApiError
from askfm_api import requests as r
from askfm_model import AskFM, askFMChat, askFMProfileDetails
from database import Database
from processor import Processor
OUTPUT_DIRECTORY = config.output_directory
logger = logging.getLogger(__name__)
processor = Processor()
logging.getLogger("requests").setLevel(logging.WARNING)
logging.getLogger("urllib3").setLevel(logging.WARNING)
def _get_chat_messages(qid: int) -> askFMChat | None:
try:
chats = api.request(r.fetch_chats(qid=qid))
except AskfmApiError as e:
if str(e) != "data_not_found":
logger.error(f"error when retrieveing chat for qid={qid}: {e}")
return None
else:
return chats
def _get_profile_answer_count(username: str) -> int:
profile = api.request(r.fetch_profile(username))
answer_count = profile["answerCount"]
return answer_count
def _get_stored_answered_count(username: str) -> int:
db = Database(config.db_file)
db.connect()
count = db.get_answer_count(uid=username)
db.close()
return count
def _get_remaining_answer_count(username: str, force: bool) -> int:
answer_count = _get_profile_answer_count(username)
stored_count = _get_stored_answered_count(username)
if force:
return answer_count
return abs(answer_count - stored_count)
def _get_newest_answer_time_stamp(username: str) -> int:
db = Database(config.db_file)
db.connect()
timestamp = db.get_newest_answer_time_stamp(uid=username)
db.close()
if timestamp is None:
return -1
return timestamp
def _get_oldest_answer_time_stamp(username: str) -> int:
db = Database(config.db_file)
db.connect()
timestamp = db.get_oldest_answer_time_stamp(uid=username)
db.close()
if timestamp is None:
return datetime.now().timestamp()
return timestamp
def extract_answers_and_chats(username: str, force: bool = False, offset=None):
"""
@username is the username
@force if true, then extraction will continue until the last answer is reached, otherwise it will
stop when it reaches the last answer stored in the database.
@offset the unix timestamp from which extraction begins. If None then starts from the beginning
"""
if offset is not None:
logger.debug(f"extracting answers and chats from offset: {offset}")
else:
logger.debug("extracting answers and chats")
profile_stream = api.request_iter(
r.fetch_profile_stream(username=username, skip="answer_chats", from_ts=offset)
)
answers: list[AskFM] = []
chats: list[askFMChat] = []
i = 0
remaining = _get_remaining_answer_count(username, force)
newest_answer_timestamp = _get_newest_answer_time_stamp(username)
if offset is not None:
# reset since it means the process was interrupted
newest_answer_timestamp = -1
answers_count = 0
chats_count = 0
prev_answer: AskFM = None
skipped_count = 0
while True:
answer: AskFM = next(profile_stream, None)
if answer is None:
break
if answer["type"] == "photopoll":
skipped_count += 1
continue
if (
not force
and answer["data"]["answer"]["createdAt"] <= newest_answer_timestamp
):
break
if answer["type"] == "answer_chat":
# technically shouldn't be possible since we are skipping `answer_chats`
# this won't impact the extraction of chats.
continue
if answer["type"] != "question":
logger.warning(
f'question id: {answer["data"]["qid"]} has an unusual answer type: {answer["type"]}'
)
continue
if (
prev_answer is not None
and prev_answer["data"]["qid"] == answer["data"]["qid"]
):
continue
answers.append(answer)
answers_count += 1
if answer["data"].get("chat", None):
chat = _get_chat_messages(answer["data"]["qid"])
chats.append(chat)
chats_count += 1
if len(answers) % 1000 == 0:
processor.process(answers)
processor.process_chat(chats)
answers.clear()
chats.clear()
prev_answer = answer
i += 1
print(f"Progress: {i/remaining*100:.1f}% - extraction\033[K", end="\r")
processor.process(answers)
processor.process_chat(chats)
logger.info(
f"extracted {answers_count} answers and {chats_count} chats, skipped {skipped_count} photo polls"
)
return answers, chats
def extract_new_chats(username: str, limit: int = 700):
logger.debug("extracting new chats for existing answers")
db = Database(config.db_file)
db.connect()
answer_ids = db.get_top_n_answers(uid=username, limit=limit)
db.close()
chats: list[askFMChat] = []
i = 0
for id in answer_ids:
chat = _get_chat_messages(qid=id)
if chat is not None:
chats.append(chat)
i += 1
print(f"Progress: {i/len(answer_ids)*100:.1f}% - new chats\033[K", end="\r")
print()
processor.process_chat(chats)
logger.debug(f"number of new chats extracted: {len(chats)}")
def extract_profile_info(username: str):
profile: askFMProfileDetails = api.request(r.fetch_profile(username))
# remove useless keys
uesless_keys = [
"avatarThumbUrl",
"backgroundThumbUrl",
"online",
"unregisteredAvailable",
"blocked",
"active",
"friend",
"allowAnonymousQuestion",
"allowAnswerSharing",
"allowSubscribing",
"showAds",
"verifiedAccount",
"emoodjiId",
]
for key in uesless_keys:
if key in profile:
profile.pop(key)
processor.process_profile(profile)
def run(usernames: list[str], force: bool = False, offset=None):
try:
api.log_in(config.username, config.password)
except AskfmApiError as e:
logger.error(f"error logging-in: {e}")
i = 0
for username in usernames:
i += 1
logger.info(f"starting job {i}/{len(usernames)} for {username}")
username = username.lower()
try:
os.makedirs(os.path.join(OUTPUT_DIRECTORY, username), exist_ok=True)
extract_profile_info(username)
if not force:
extract_new_chats(username=username, limit=100)
extract_answers_and_chats(username, force, offset=offset)
oldest_timestamp = _get_oldest_answer_time_stamp(username)
archived_count = _get_stored_answered_count(username)
remaining_count = _get_profile_answer_count(username)
if remaining_count > archived_count:
logger.info(f"continuing extracting from timestamp: {oldest_timestamp}")
extract_answers_and_chats(username, offset=oldest_timestamp)
except AskfmApiError as e:
logger.error(f"error: {e}")
print()
if __name__ == "__main__":
parser = argparse.ArgumentParser(
prog="askfm-archiver", description="archive ask.fm profiles"
)
parser.add_argument("usernames", nargs="+")
args = parser.parse_args()
logging.basicConfig(
level=logging.INFO,
format="time=%(asctime)s origin=%(name)s level=%(levelname)s msg=%(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
filename="extractor.log",
filemode="a",
)
logging.getLogger().addHandler(logging.FileHandler("extractor.log"))
logging.getLogger().addHandler(logging.StreamHandler())
if len(config.username) == 0 or len(config.password) == 0:
print("askfm credentials are missing. Edit config.py with your credentials")
exit(-1)
if len(config.key) == 0:
print("API Key is missing from config.py. The key can be found in the README. ")
exit(-1)
api = AskfmApi(base64.b64decode(config.key).decode("ascii"))
run(args.usernames)