-
Notifications
You must be signed in to change notification settings - Fork 6
/
telegram-scraper.js
304 lines (160 loc) · 6.8 KB
/
telegram-scraper.js
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
import html_parser from './html-parser'
import https from 'https'
export async function telegram_scraper(channel) {
function HtmlEntitiesDecode(string) {
string = string.replace(/"/g, '"');
string = string.replace(/&/g, '&');
string = string.replaceAll(' ',' ')
string = string.replaceAll("'","'")
return string;
}
function get(targeturl,resolve){
https.get(targeturl, res => {
if((res.statusCode === 301 || res.statusCode === 302)&&(res.headers.location != undefined)){
return get(res.headers.location,resolve)
}
let data = []
res.on('data', chunk => { data.push(chunk) })
res.on('end', () => {
try{
const raw = Buffer.concat(data).toString()
resolve(raw)
} catch(err) {
let msg = JSON.stringify({status:'error',msg:err,targeturl})
resolve(msg)
}
})
})
}
async function get_page(targeturl){
return new Promise((resolve) => {
get(targeturl,resolve)
})
}
const targeturl = 'https://t.me/s/'+channel
const rawdata = await get_page(targeturl)
let temp = rawdata
if (temp.indexOf('<section class="tgme_channel_history js-message_history">') == -1) return 'Unknown telegram channel'
temp = temp.split('<section class="tgme_channel_history js-message_history">')
temp = temp[1].split('</section>')
temp = temp[0].split('<div class="tgme_widget_message_wrap js-widget_message_wrap">')
temp.splice(0, 1)
let data = []
for(var i=0;i<temp.length;i++){
let html = '<div class="tgme_widget_message_wrap js-widget_message_wrap">'+temp[i]
let arr = html_parser(html)
let message_text = ''
let message_photo = []
let message_video = []
let views = ''
let datetime = ''
let include = true
for(var j=0;j<arr[0].children[0].children[3].children.length;j++){
let child_class = ''
try{child_class = arr[0].children[0].children[3].children[j].attributes.class}catch{}
if (child_class == "tgme_widget_message_text js-message_text"){
function message_extractor(input){
let msg = ''
function loop(arr){
for(let k=0;k<arr.length;k++){
if (arr[k].text != null ) msg += arr[k].text
try{if (arr[k].attributes.class == "tgme_widget_service_strong_text") include = false }catch{}
if (arr[k].children != null){
if (arr[k].children.length > 0) loop(arr[k].children)
}
}
}
loop(input)
return msg.replaceAll('/>',' ')
}
let message_node = arr[0].children[0].children[3].children[j].children
message_text = message_extractor(message_node)
}
let arr_child_class = child_class.split(" ")
if(arr_child_class[0] == "tgme_widget_message_photo_wrap"){
message_photo.push(arr[0].children[0].children[3].children[j].attributes.style.split("'")[1])
}
if(child_class == "tgme_widget_message_footer compact js-message_footer"){
let get_views = arr[0].children[0].children[3].children[j].children[1].children[1].children[0].text
if (get_views != null) views = get_views
try{datetime = arr[0].children[0].children[3].children[j].children[1].children[3].children[0].children[0].attributes.datetime}catch{}
try{datetime = arr[0].children[0].children[3].children[j].children[1].children[3].children[2].children[0].attributes.datetime}catch{}
}
if(child_class == "tgme_widget_message_video_player blured js-message_video_player"){
message_video.push(arr[0].children[0].children[3].children[j].children[4].children[1].attributes.src)
}
if(child_class == "tgme_widget_message_video_player js-message_video_player"){
message_video.push(arr[0].children[0].children[3].children[j].children[2].children[1].attributes.src)
}
if(child_class == "media_supported_cont"){
try{
let src = arr[0].children[0].children[3].children[j].children[0].children[2].attributes.src
if(src != null)message_video.push(src)
}catch{}
try{
let src = arr[0].children[0].children[3].children[j].children[0].children[2].children[1].attributes.src
if(src != null)message_video.push(src)
}catch{}
try{
function message_extractor(input){
let msg = ''
function loop(arr){
for(let k=0;k<arr.length;k++){
if (arr[k].text != null ) msg += arr[k].text
try{if (arr[k].attributes.class == "tgme_widget_service_strong_text") include = false }catch{}
if (arr[k].children != null){
if (arr[k].children.length > 0) loop(arr[k].children)
}
}
}
loop(input)
return msg.replaceAll('/>',' ')
}
let message_node = arr[0].children[0].children[3].children[j].children[1].children
message_text = message_extractor(message_node)
}catch{}
}
if (child_class == "tgme_widget_message_grouped_wrap js-message_grouped_wrap"){
function loop(node){
for(let k=0;k<node.length;k++){
try{
if (node[k].attributes.class != null){
if (node[k].attributes.class == "tgme_widget_message_video js-message_video") message_video.push(node[k].attributes.src)
}
}catch{}
try{
if (node[k].attributes.class != null){
if (node[k].attributes.class == "tgme_widget_message_photo_wrap grouped_media_wrap blured js-message_photo") {
let photo = node[k].attributes.style.split("'")[1]
message_photo.push(photo)
}
}
}catch{}
if (node[k].children != null){
if (node[k].children.length > 0) loop(node[k].children)
}
}
}
let node = arr[0].children[0].children[3].children[j].children
loop(node)
}
}
let item = {
//raw:arr[0],
data_post: arr[0].children[0].attributes["data-post"],
data_view: arr[0].children[0].attributes["data-view"],
user_url: arr[0].children[0].children[1].children[0].attributes.href,
user_photo: arr[0].children[0].children[1].children[0].children[0].children[0].attributes.src,
user_name: arr[0].children[0].children[3].children[3].children[0].children[0].children[0].text,
message_url:'https://t.me/'+arr[0].children[0].attributes["data-post"],
message_text:HtmlEntitiesDecode(message_text),
message_photo,
message_video,
views,
datetime
}
if(include)data.push(item)
}
let result = JSON.stringify(data, null, 30)
return result
}