-
Notifications
You must be signed in to change notification settings - Fork 15
/
getchat.py
57 lines (46 loc) · 1.71 KB
/
getchat.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
from chat_downloader import ChatDownloader
import json
import threading
import os
import time
import const
import utils
class ChatArchiver:
def __init__(self, url, output_file, start_timestamp=None):
self.url = url
self.output_file = output_file
with open(self.output_file, "a+", encoding="utf8") as f:
data = {
"time": time.time(),
"url": url
}
if start_timestamp:
data["startTimestamp"] = start_timestamp
data = json.dumps(data, ensure_ascii=False)
f.write(f"# {data}\n")
self.chat = ChatDownloader().get_chat(url, message_groups='all', inactivity_timeout=const.CHAT_INACTIVITY_DURATION)
self.timer = utils.RepeatedTimer(const.CHAT_BUFFER_TIME, self.__save_chat)
self.buffer = [[], []]
self.buffer_index = 0
self.is_stop = False
self.download_task = threading.Thread(target=self.__download_chat, daemon=True)
self.download_task.start()
def __save_chat(self):
old_buffer_index = self.buffer_index
self.buffer_index = (self.buffer_index + 1) % 2
with open(self.output_file, "a+", encoding="utf8") as f:
for chat in self.buffer[old_buffer_index]:
json.dump(chat, f, ensure_ascii=False)
f.write("\n")
self.buffer[old_buffer_index] = []
def __download_chat(self):
for chat in self.chat:
self.buffer[self.buffer_index].append(chat)
self.is_stop = True
self.timer.stop()
self.__save_chat()
def is_finished(self):
return self.is_stop
def stop(self):
self.is_stop = True
self.timer.stop()