Andrei Panferov
commited on
Commit
·
27615f9
1
Parent(s):
7258c67
Upload collect.py
Browse files- collect.py +129 -0
collect.py
ADDED
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import numpy as np
|
3 |
+
from tqdm import tqdm, trange
|
4 |
+
import json
|
5 |
+
from time import sleep
|
6 |
+
|
7 |
+
import api2ch
|
8 |
+
import html2text
|
9 |
+
import re
|
10 |
+
import os
|
11 |
+
|
12 |
+
API = api2ch.DvachApi('b')
|
13 |
+
|
14 |
+
if os.path.exists("parsed_threads.json"):
|
15 |
+
with open("parsed_threads.json", "r") as file:
|
16 |
+
PARSED_TREADS = set(json.load(file)["ids"])
|
17 |
+
else:
|
18 |
+
PARSED_TREADS = set()
|
19 |
+
|
20 |
+
REPLY_RE = re.compile(">>[0-9]+")
|
21 |
+
|
22 |
+
HTML_STRIPPER = html2text.HTML2Text()
|
23 |
+
HTML_STRIPPER.ignore_links = True
|
24 |
+
HTML_STRIPPER.ignore_images = True
|
25 |
+
HTML_STRIPPER.ignore_emphasis = True
|
26 |
+
|
27 |
+
def SaveParsed():
|
28 |
+
with open("parsed_threads.json", "w") as file:
|
29 |
+
json.dump({"ids": list(PARSED_TREADS)}, file)
|
30 |
+
|
31 |
+
def ThreadGood(thread):
|
32 |
+
return thread.reply_count > 20
|
33 |
+
|
34 |
+
def AppendDialogues(id, dialogues):
|
35 |
+
with open("dialogues.jsonl", "a") as file:
|
36 |
+
for dialogue in dialogues:
|
37 |
+
json.dump({"id": id, "dialogue": dialogue}, file, ensure_ascii=False)
|
38 |
+
file.write("\n")
|
39 |
+
|
40 |
+
def FilterAndPrepareText(text):
|
41 |
+
text = HTML_STRIPPER.handle(text)
|
42 |
+
text = text.lower()
|
43 |
+
text = text.replace("\n", " ")
|
44 |
+
text = text.replace("(op)", "")
|
45 |
+
if text.find("бамп") != -1:
|
46 |
+
return
|
47 |
+
text = re.sub(' +', ' ', text)
|
48 |
+
text = re.sub('^ ', '', text)
|
49 |
+
text = re.sub(' $', '', text)
|
50 |
+
return text
|
51 |
+
|
52 |
+
class Post:
|
53 |
+
def __init__(self, idx: int, text: str):
|
54 |
+
self.idx = idx
|
55 |
+
|
56 |
+
self.parents = []
|
57 |
+
while True:
|
58 |
+
reply_match = REPLY_RE.match(text)
|
59 |
+
if reply_match is not None:
|
60 |
+
parent_id = int(reply_match.group(0)[2:])
|
61 |
+
span = reply_match.span()
|
62 |
+
self.parents.append(parent_id)
|
63 |
+
text = text[:span[0]] + text[span[1]:]
|
64 |
+
text = re.sub('^ ', '', text)
|
65 |
+
text = re.sub(' $', '', text)
|
66 |
+
else:
|
67 |
+
break
|
68 |
+
self.text = text
|
69 |
+
|
70 |
+
self.children = []
|
71 |
+
self.dialogue = [self.text]
|
72 |
+
self.appeared = False
|
73 |
+
|
74 |
+
def __repr__(self):
|
75 |
+
return {"idx": self.idx, "parents": self.parents, "text": self.text}.__repr__()
|
76 |
+
|
77 |
+
def build_random_dialogue(self, context):
|
78 |
+
self.appeared = True
|
79 |
+
if len(self.dialogue) != 1:
|
80 |
+
return self.dialogue
|
81 |
+
if len(self.parents) == 0:
|
82 |
+
return self.dialogue
|
83 |
+
chosen_parent = self.parents[0]
|
84 |
+
if chosen_parent in context.keys():
|
85 |
+
self.dialogue.extend(context[chosen_parent].build_random_dialogue(context))
|
86 |
+
return self.dialogue
|
87 |
+
|
88 |
+
def BuildDialogues(thread):
|
89 |
+
posts = {}
|
90 |
+
for post in thread:
|
91 |
+
idx = post.num
|
92 |
+
text = FilterAndPrepareText(post.comment)
|
93 |
+
if text is not None:
|
94 |
+
posts[idx] = Post(idx, text)
|
95 |
+
|
96 |
+
for _, post in reversed(posts.items()):
|
97 |
+
if not post.appeared:
|
98 |
+
post.build_random_dialogue(posts)
|
99 |
+
|
100 |
+
return [post.dialogue for post in list(posts.values()) if len(post.dialogue) > 1]
|
101 |
+
|
102 |
+
def main():
|
103 |
+
print("Started collecting...")
|
104 |
+
while True:
|
105 |
+
while True:
|
106 |
+
try:
|
107 |
+
board = API.get_board()
|
108 |
+
break
|
109 |
+
except:
|
110 |
+
print(f"Failed to fetch board. Sleeping for 60s")
|
111 |
+
sleep(60)
|
112 |
+
print("Got board")
|
113 |
+
|
114 |
+
for thread in board:
|
115 |
+
if thread.num not in PARSED_TREADS and ThreadGood(thread):
|
116 |
+
PARSED_TREADS.add(thread.num)
|
117 |
+
try:
|
118 |
+
thread = API.get_thread(thread.num)
|
119 |
+
except:
|
120 |
+
continue
|
121 |
+
dialogues = BuildDialogues(thread)
|
122 |
+
AppendDialogues(thread[0].num, dialogues)
|
123 |
+
print("Parsed")
|
124 |
+
SaveParsed()
|
125 |
+
print("Saved. Sleeping for 10m")
|
126 |
+
sleep(600)
|
127 |
+
|
128 |
+
if __name__ == "__main__":
|
129 |
+
main()
|