-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtgindex.py
197 lines (173 loc) · 9 KB
/
tgindex.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
#!/usr/bin/env python
import logging
from elasticsearch import AsyncElasticsearch
from elasticsearch import Elasticsearch
import zipfile
import json
from threading import Thread
import traceback
import requests
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)s %(message)s')
logging.getLogger('urllib3').setLevel(logging.INFO)
class SystemBusyException(Exception):
pass
def validate_file(zip, filename):
try:
zip.getinfo(filename)
except KeyError:
logging.error("File name %s is missing" % (filename))
return "broken"
return filename
class TgIndex:
CONFIG_INDEX_NAME = "tg_archive_config"
DATA_INDEX_NAME = "tg_archive_data"
def __init__(self, elasticsearch_url, working_dir = None):
self.elasticsearch_url = elasticsearch_url
self.es_client = AsyncElasticsearch(elasticsearch_url)
self.es_client_sync = Elasticsearch(elasticsearch_url)
self.thread = None
self.index_info = {}
self.working_dir = working_dir
if not self.es_client_sync.indices.exists(index=TgIndex.DATA_INDEX_NAME):
self.es_client_sync.indices.create(
index=TgIndex.DATA_INDEX_NAME,
body={
"settings": {
"analysis": {
"analyzer": {
"default": {
"type": "russian"
}
}
}
}
})
if not self.es_client_sync.indices.exists(index=TgIndex.CONFIG_INDEX_NAME):
self.es_client_sync.indices.create(index=TgIndex.CONFIG_INDEX_NAME)
self.sync_config()
def sync_config(self):
logging.info("Updating sites list")
resp = self.es_client_sync.search(
index=TgIndex.CONFIG_INDEX_NAME,
query={"match_all": {}},
size=1000
)
self.sites = list(map(lambda x: {'id': x['_id'], 'name': x['_source']['name'], 'file_name': x['_source']['file_name']}, resp['hits']['hits']))
self.sites_map = dict(map(lambda x: (x['id'], x['file_name']), self.sites))
logging.info("Update sites list: %d sites" % len(self.sites))
def index_background(self, filename):
if self.thread is None or not self.thread.is_alive():
self.thread = Thread(target=self._index_zip, args=(filename, ), daemon=True)
self.thread.start()
else:
logging.info("System is busy")
raise SystemBusyException()
def download(self, url, file_name):
if self.thread is None or not self.thread.is_alive():
self.thread = Thread(target=self._download_url, args=(url, file_name,), daemon=True)
self.thread.start()
else:
logging.info("System is busy")
raise SystemBusyException()
def _download_url(self, url, file_name):
logging.info("Processing download %s to %s" % (url, file_name))
try:
with requests.get(url, stream=True) as r:
r.raise_for_status()
self.index_info[file_name] = {'state': 'downloading'}
with open("%s/%s" % (self.working_dir, file_name), 'wb') as f:
for chunk in r.iter_content(chunk_size=8192):
f.write(chunk)
self.index_info[file_name] = {'state': 'downloaded'}
except:
logging.info("Unable to download")
tb = traceback.format_exc()
logging.error("Indexation failed", tb)
self.index_info[file_name] = {'state': 'download failed'}
def _index_zip(self, filename):
logging.info("Processing file %s" % filename)
full_path = "%s/%s" % (self.working_dir, filename)
self.index_info[filename] = {'state': 'running', 'processed': 0}
try:
with zipfile.ZipFile(full_path, 'r') as archive:
with archive.open("result.json") as content:
obj = json.load(content)
self.index_info[filename]['total'] = len(obj['messages'])
logging.info("Total %s messages" % len(obj['messages']))
with zipfile.ZipFile(full_path, 'r') as archive:
with archive.open("result.json") as content:
obj = json.load(content)
tg_id = obj['id']
tg_name = obj['name']
messages = obj['messages']
logging.info("Starting indexing %s: %d messages" % (tg_name, len(messages)))
for message in messages:
self.index_info[filename]['processed'] += 1
if message['type'] != 'message':
continue
assert message['from_id'] == "channel%s" % tg_id, message
texts = []
message_doc = {
'site_id': tg_id,
'type': message['type'],
'date': message['date'],
}
links = []
if 'text' in message:
if type(message['text']) == str:
texts.append(message['text'])
elif type(message['text']) == list:
for text in message['text']:
if type(text) == str:
texts.append(text)
else:
if 'text' in text:
texts.append(text['text'])
if 'href' in text:
links.append(text['href'])
else:
raise Exception("Unsupported text block type: %s" % message['text'])
if 'media_type' in message:
message_doc['media_type'] = message['media_type']
if message['media_type'] in ['video_file', 'video_message', 'animation']:
message_doc['mime_type'] = message['mime_type']
message_doc['file'] = validate_file(archive, message['file'])
message_doc['thumbnail'] = validate_file(archive, message.get('thumbnail', None))
message_doc['duration_seconds'] = message.get('duration_seconds', None)
message_doc['width'] = message.get('width', None)
message_doc['height'] = message.get('height', None)
elif message['media_type'] in ['audio_file', 'voice_message']:
message_doc['mime_type'] = message['mime_type']
message_doc['file'] = validate_file(archive, message['file'])
texts.append(message.get('title', ''))
message_doc['duration_seconds'] = message.get('duration_seconds', None)
elif message['media_type'] == 'sticker':
if 'text' not in message_doc:
message_doc['text'] = ''
message_doc['text'] += ' ' + message['sticker_emoji']
texts.append(message_doc['text'].strip())
else:
raise Exception("Media type not supported: %s" % message)
if 'photo' in message:
message_doc['photo'] = validate_file(archive, message['photo'])
assert 'width' not in message_doc
message_doc['width'] = message.get('width', None)
message_doc['height'] = message.get('height', None)
message_doc['text'] = "\n".join(list(map(lambda x: x.strip(), texts))).strip()
if len(links) > 0:
message_doc['links'] = links
msg_id = "%s_%s" % (tg_id, message['id'])
self.es_client_sync.index(index=TgIndex.DATA_INDEX_NAME, id=msg_id, document=message_doc)
logging.info("Indexing finished for %s" % (tg_name))
tg_config = {
'name': tg_name,
'file_name': filename
}
self.es_client_sync.index(index=TgIndex.CONFIG_INDEX_NAME, id=tg_id, document=tg_config)
self.index_info[filename]['state'] = 'finished'
self.sync_config()
except:
tb = traceback.format_exc()
logging.error("Indexation failed", tb)
self.index_info[filename]['error'] = tb
self.index_info[filename]['state'] = 'failed'