forked from Ghostofapacket/socialscrape-bot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
irc.py
576 lines (542 loc) · 33.7 KB
/
irc.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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
# -*- coding: utf-8 -*-
import base64
import socket
import ssl
import datetime
import json
import threading
import re
import requests
import time
import hashlib
import subprocess
import os
from multiprocessing import Process, Queue, Pool
from shlex import quote
import abcalc
import settings
class IRC(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.channel_bot = settings.irc_channel_bot
self.nick = settings.irc_nick
self.operator = settings.irc_operator
self.sasl = settings.irc_sasl
self.server_name = settings.irc_server_name
self.server_port = settings.irc_server_port
self.server_ssl = settings.irc_server_ssl
self.http2irc_url = settings.http2irc_url
self.server = None
self.scrapesite = None
self.incoming_buffer = b''
self.send_queue = Queue()
# self.messages_received = []
# self.messages_sent = []
self.commands_received = []
self.commands_sent = []
# self.running_instagram = []
# self.running_instagram = 0
# self.processpool = Pool(processes=1)
def run(self):
self.connect()
def connect(self):
if self.server:
self.server.close()
settings.logger.log('Connecting to IRC server ' + self.server_name)
self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server.connect((self.server_name, self.server_port))
if self.server_ssl:
self.rawserver = self.server
self.server = ssl.create_default_context().wrap_socket(self.rawserver, server_hostname = self.server_name)
self.start_sender()
if self.sasl:
self.send('CAP', 'REQ :sasl')
self.send('NICK', '{nick}'.format(nick=self.nick))
self.send('USER', '{nick} {nick} {nick} :I am a socialscrape-bot operated by {operator}.'
.format(nick=self.nick, operator=self.operator))
if self.http2irc_url:
self.start_http2irc_listener()
self.listener()
self.start_pinger()
settings.logger.log('Connected to ' + self.server_name + ' as ' + self.nick)
def start_pinger(self):
self.pinger = threading.Thread(target=self.pinger)
self.pinger.daemon = True
self.pinger.start()
def pinger(self):
while True:
time.sleep(600)
self.send('PING', ':')
def start_sender(self):
self.sender = threading.Thread(target = self.sender)
self.sender.daemon = True
self.sender.start()
def sender(self):
while True:
line = self.send_queue.get()
self.real_send(line)
time.sleep(1)
def send(self, command, string, channel=''):
if channel != '':
channel += ' :'
message = '{command} {channel}{string}'.format(**locals())
self.send_queue.put(message)
def real_send(self, message):
# Must only be called from a single thread
try:
settings.logger.log('IRC - {message}'.format(**locals()))
self.server.send('{message}\r\n'.format(**locals()).encode('utf-8'))
except Exception as exception:
settings.logger.log('{exception}'.format(**locals()), 'WARNING')
def start_http2irc_listener(self):
self.h2ilistener = threading.Thread(target = self.http2irc_listener)
self.h2ilistener.daemon = True
self.h2ilistener.start()
def http2irc_listener(self):
while True:
try:
r = requests.get(self.http2irc_url, stream = True)
for line in r.iter_lines():
settings.logger.log(f'Message from http2irc: {line}')
try:
o = json.loads(line)
except (json.JSONDecodeError, UnicodeDecodeError) as e:
settings.logger.log(f'Error parsing http2irc line {line!r}: {e}', 'WARNING')
continue
if o['command'] != 'PRIVMSG' or not o['message'].startswith(f'{self.nick}: '):
continue
if not any(x in o['user']['modes'] for x in ('+', '@')):
settings.logger.log(f'Unauthenticated message received, ignoring')
continue
command = list(filter(bool, map(str.strip, o['message'][len(self.nick) + 2:].split(' '))))
settings.logger.log(f'COMMAND - Received in channel {o["channel"]} - {command[0]}')
self.command(command, o['user']['nick'], o['channel'])
except requests.RequestException as e:
settings.logger.log(f'Error in http2irc listener: {type(e).__module__}.{type(e).__name__} {e!s}')
time.sleep(5)
def listener(self):
command_message_prefix_pattern = r'^:.+PRIVMSG[^:]+:' + re.escape(self.nick)
while True:
data = self.server.recv(4096)
self.incoming_buffer += data
for rawmessage in self.incoming_buffer.split(b'\r\n')[:-1]:
try:
rawmessage = rawmessage.decode('utf-8')
except UnicodeDecodeError as e:
settings.logger.log('IRC - Skipping undecodable rawmessage {!r}: {!s}'.format(rawmessage, e))
continue
settings.logger.log('IRC - {rawmessage}'.format(**locals()))
if rawmessage.startswith(':'):
prefix, message = rawmessage.split(' ', 1)
else:
message = rawmessage
if message.startswith('PING :'):
settings.logger.log('Received message ' + message)
message_new = re.search(r'^[^:]+:(.*)$', message).group(1)
self.send('PONG', ':{message_new}'.format(**locals()))
elif self.sasl and message.startswith('CAP '):
if message != 'CAP * ACK :sasl':
settings.logger.log('IRC - ERROR: SASL capability not ACKd, cannot continue')
raise RuntimeError('SASL not acknowledged by server')
self.send('AUTHENTICATE', 'PLAIN')
elif self.sasl and message == 'AUTHENTICATE +':
username, password = self.sasl.split(':')
sasl = base64.b64encode(f'{username}\0{username}\0{password}'.encode('utf-8')).decode('ascii')
self.send('AUTHENTICATE', sasl)
elif self.sasl and message.startswith('903 '): # RPL_SASLSUCCESS
self.send('CAP', 'END')
elif self.sasl and message[:4] in ('902 ', '904 ', '905 ', '906 ', '907 ', '908 '):
settings.logger.log('IRC - ERROR: SASL authentication failed, cannot continue')
raise RuntimeError('SASL authentication failed')
elif message.startswith('001 '): # Connection registered
self.send('JOIN', self.channel_bot)
if self.server_name in ('irc.servercentral.net', 'irc.colosolutions.net', 'irc.mzima.net'):
self.send('PRIVMSG', 'Fuck EFnet.', self.channel_bot)
elif not self.http2irc_url and re.search(command_message_prefix_pattern, rawmessage):
if re.search(command_message_prefix_pattern + ' .*', rawmessage):
command = re.search(command_message_prefix_pattern + ' (.*)', rawmessage) \
.group(1).strip().split(' ')
command = [s.strip() for s in command if len(s.strip()) != 0]
user = re.search(r'^:([^!]+)!', rawmessage).group(1)
channel = re.search(r'^:[^#]+(#[^ :]+) ?:', rawmessage).group(1)
self.commands_received.append({'command': command,
'user': user,
'channel': channel})
self.command(command, user, channel)
settings.logger.log('COMMAND - Received in channel {channel} - {command[0]}'.format(**locals()))
elif re.search(command_message_prefix_pattern + r'\: .*', rawmessage):
command = re.search(command_message_prefix_pattern + r'\: (.*)', rawmessage) \
.group(1).strip().split(' ')
command = [s.strip() for s in command if len(s.strip()) != 0]
user = re.search(r'^:([^!]+)!', rawmessage).group(1)
channel = re.search(r'^:[^#]+(#[^ :]+) ?:', rawmessage).group(1)
self.commands_received.append({'command': command,
'user': user,
'channel': channel})
self.command(command, user, channel)
settings.logger.log('COMMAND - Received in channel {channel} - {command[0]}'.format(**locals()))
self.incoming_buffer = self.incoming_buffer.rsplit(b'\r\n', 1)[1]
def check_admin(self, user):
return False
# change to db
if str(user) == "Igloo":
return True
else:
return False
def getjobid(self, user):
sha_1 = hashlib.sha1()
time = str(datetime.datetime.now()) + '-' + user
sha_1.update(time.encode('utf-8'))
jobid = sha_1.hexdigest()
return jobid
def _snscrape(self, command, filename, channel, user, jobid):
settings.logger.log("SNSCRAPE - " + jobid + " - Running: " + command + " >" + filename)
try:
subprocess.run(command + " >" + filename, shell=True, check=True)
except subprocess.CalledProcessError as error:
settings.logger.log("SNSCRAPE - snscrape for {jobid} failed with exit status {returncode}".format(jobid=jobid, returncode=error.returncode))
self.send('PRIVMSG', '{user}: Sorry, snscrape failed for {jobid}: exit status {returncode}'.format(user=user, jobid=jobid, returncode=error.returncode), channel)
return False
if os.stat(filename).st_size == 0:
settings.logger.log("SNSCRAPE - snscrape for {jobid} produced no results".format(jobid = jobid))
self.send('PRIVMSG', '{user}: Sorry, snscrape failed for {jobid}: no scrape results (user might not exist or be search-banned)'.format(user=user, jobid=jobid), channel)
return False
return True
def _compress(self, filename, channel, user, jobid):
settings.logger.log('ZSTD - ' + jobid)
try:
subprocess.run(['zstd', '-8', '--quiet', '--rm', filename], check = True)
except subprocess.CalledProcessError as error:
settings.logger.log('ZSTD - zstd for {jobid} failed with exit status {returncode}'.format(jobid=jobid, returncode=error.returncode))
self.send('PRIVMSG', '{user}: Sorry, zstd failed for {jobid}: exit status {returncode}'.format(user=user, jobid=jobid, returncode=error.returncode), channel)
return False
return True
def _queue_tweets(self, filename, channel, user, jobid):
settings.logger.log('QUEUETWEETS - {filename}'.format(filename=filename))
try:
subprocess.run('queue-tweets ' + filename, shell = True, check = True)
except subprocess.CalledProcessError as error:
settings.logger.log('QUEUETWEETS - Queueing for {filename} failed with exit status {returncode}'.format(filename=filename, returncode=error.returncode))
self.send('PRIVMSG', '{user}: Sorry, queueing tweets failed for {jobid}: exit status {returncode}'.format(user=user, jobid=jobid, returncode=error.returncode), channel)
return False
return True
def _upload(self, localfile, remotename, channel, user, jobid):
settings.logger.log('SNSCRAPE - Uploading ' + localfile + ' as ' + remotename)
try:
uploadedurl = subprocess.check_output('curl --fail -s --upload-file ' + localfile + ' https://transfer.archivete.am/' + remotename, shell=True).decode("utf-8")
except subprocess.CalledProcessError as error:
settings.logger.log('SNSCRAPE - Upload of {localfile} to {remotename} failed with exit status {error.returncode}'.format(**locals()))
self.send('PRIVMSG', '{user}: Sorry, upload failed for {jobid}: exit status {returncode}'.format(user=user, jobid=jobid, returncode=error.returncode), channel)
return None
if not uploadedurl.startswith('https://transfer.archivete.am/') or ' ' in uploadedurl or '\n' in uploadedurl or '\r' in uploadedurl:
settings.logger.log('SNSCRAPE - Upload of {localfile} to {remotename} failed: response {uploadedurl!r}'.format(**locals()))
self.send('PRIVMSG', '{user}: Sorry, upload failed for {jobid}: unexpected response'.format(user=user, jobid=jobid), channel)
return None
if uploadedurl.endswith('.zst'):
uploadedurl = uploadedurl[:-4]
uploadedurl = uploadedurl.replace('%40','@')
return uploadedurl
def run_snscrape(self, channel, user, module, target, **kwargs):
jobid = self.getjobid(user + '-' + module + '-' + target)
settings.logger.log('SNSCRAPE - Job ID ' + jobid)
self.send('PRIVMSG', '{user}: {jobid} has been queued.' .format(user=user, jobid=jobid), channel)
settings.logger.log('SNSCRAPE - Trying to run snscrape with the following arguments - {module} - {target}' \
.format(**locals()))
sanityregex = re.compile('([\"\'\@\#])')
command = None
uploadedurl = None
options = None
chromeboturls = []
if str(module).startswith("twitter"):
if module in ('twitter-user', 'twitter-profile'):
settings.logger.log('SNSCRAPE - Checking username capitalisation for user ' + sanityregex.sub(r'',target))
try:
newtarget = subprocess.check_output("snscrape --max-results 1 " + module + " " + quote(sanityregex.sub(r'',target)) + " | grep -Po '^https?://twitter\.com/\K[^/]+'", shell=True).decode("utf-8").strip()
except subprocess.CalledProcessError as error:
newtarget = None
if newtarget is None:
settings.logger.log("SNSCRAPE - Twitter user " + quote(sanityregex.sub(r'',target)) + " not found")
self.send('PRIVMSG', '{user}: Sorry, No results found for {jobid}' .format(user=user, jobid=jobid), channel)
elif newtarget.lower() != target.lower():
settings.logger.log(f'SNSCRAPE - Twitter user {target!r} resolved to non-matching {newtarget!r}')
self.send('PRIVMSG', f'{user}: Sorry, {jobid} resolved {target!r} to {newtarget!r}, refusing to continue', channel)
else:
localfile = 'jobs/twitter-' + ('@' if module == 'twitter-user' else 'profile-@') + jobid
success = self._snscrape("snscrape --format '{url} {tcooutlinksss} {outlinksss}' " + quote(module) + " " + newtarget, localfile, channel, user, jobid)
if not success:
return
settings.logger.log('SNSCRAPE - Finished ' + jobid)
#Insert the profile as per JAA's request :-)
profileline = "https://twitter.com/" + newtarget
lines = [profileline + "\n"]
with open(localfile, "r") as outfile:
for line in outfile.read().split():
lines.append(line + "\n")
with open(localfile, "w") as outfile:
outfile.writelines(lines)
success = self._compress(localfile, channel, user, jobid)
if not success:
return
localfile += '.zst'
uploadedurl = self._upload(localfile, 'twitter-' + ('@' if module == 'twitter-user' else 'profile-@') + newtarget + '.zst', channel, user, jobid)
chromeboturls = [profileline]
elif str(module).startswith("twitter-hashtag"):
localfile = 'jobs/twitter-#' + jobid
success = self._snscrape("snscrape --format '{url} {tcooutlinksss} {outlinksss}' " + quote(module) + " " + quote(sanityregex.sub(r'',target)), localfile, channel, user, jobid)
if not success:
return
settings.logger.log('SNSCRAPE - Finished ' + jobid)
lines = []
lines.append("https://twitter.com/hashtag/" + target + "\n")
lines.append("https://twitter.com/hashtag/" + target + "?src=hash\n")
lines.append("https://twitter.com/hashtag/" + target + "?f=tweets&vertical=default\n")
lines.append("https://twitter.com/hashtag/" + target + "?f=tweets&vertical=default&src=hash\n")
with open(localfile, "r") as outfile:
for line in outfile.read().split():
lines.append(line + "\n")
with open(localfile, "w") as outfile:
outfile.writelines(lines)
success = self._compress(localfile, channel, user, jobid)
if not success:
return
localfile += '.zst'
uploadedurl = self._upload(localfile, 'twitter-%23' + quote(sanityregex.sub(r'',target)) + '.zst', channel, user, jobid)
newtarget = sanityregex.sub(r'',target)
chromeboturls = [x.strip() for x in lines[:4]]
elif str(module).startswith("twitter-search"):
localfile = 'jobs/twitter-search-' + jobid
success = self._snscrape("snscrape twitter-search " + quote(sanityregex.sub(r'',target)), localfile, channel, user, jobid)
if not success:
return
settings.logger.log('SNSCRAPE - Finished ' + jobid)
success = self._compress(localfile, channel, user, jobid)
if not success:
return
localfile += '.zst'
uploadedurl = self._upload(localfile, 'twitter-search-' + quote(sanityregex.sub(r'',target)) + '.zst', channel, user, jobid)
newtarget = sanityregex.sub(r'',target)
command = '!ao <'
options = '--igset badvideos,notweets --explain "For {user} - socialscrape job {jobid}"'.format(user=user, jobid=jobid)
if uploadedurl is not None:
if not self._queue_tweets(localfile, channel, user, jobid):
return
if str(module).startswith("facebook"):
if str(module).startswith("facebook-user"):
settings.logger.log('SNSCRAPE - Checking username capitalisation for user ' + sanityregex.sub(r'',target))
try:
print("curl -s -A 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36' -H 'Accept-Language: en-US,en;q=0.5' \"https://www.facebook.com/" + sanityregex.sub(r'',target) + "/\" | grep -Po '<div\s[^>]*(?<=\s)data-key\s*=\s*\"tab_home\".*?</div>' | grep -Po '<a\s[^>]*(?<=\s)href=\"/\K[^/]+'")
newtarget = subprocess.check_output("curl -s -A 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36' -H 'Accept-Language: en-US,en;q=0.5' \"https://www.facebook.com/" + sanityregex.sub(r'',target) + "/\" | grep -Po '<div\s[^>]*(?<=\s)data-key\s*=\s*\"tab_home\".*?</div>' | grep -Po '<a\s[^>]*(?<=\s)href=\"/\K[^/]+'", shell=True).decode("utf-8").strip()
print(newtarget)
except subprocess.CalledProcessError as error:
newtarget = None
if newtarget is None:
settings.logger.log("SNSCRAPE - Facebook user " + quote(sanityregex.sub(r'',target)) + " not found")
self.send('PRIVMSG', '{user}: Sorry, No results found for {jobid} - User does not exist / profile is set to private' .format(user=user, jobid=jobid), channel)
else:
settings.logger.log("SNSCRAPE - Running with updated settings - snscrape " + quote(module) + " " + newtarget + " >jobs/facebook-@" + jobid)
success = self._snscrape("snscrape --format '{cleanUrl} {outlinksss}' " + quote(module) + " " + newtarget, "jobs/facebook-@" + jobid, channel, user, jobid)
if not success:
return
settings.logger.log('SNSCRAPE - Finished ' + jobid)
profileline = "https://www.facebook.com/" + newtarget + "/"
lines = [profileline + "\n"]
with open("jobs/facebook-@" + jobid, "r") as outfile:
for line in outfile.read().split():
lines.append(line + "\n")
with open("jobs/facebook-@" + jobid, "w") as outfile:
outfile.writelines(lines)
success = self._compress('jobs/facebook-@' + jobid, channel, user, jobid)
if not success:
return
uploadedurl = self._upload('jobs/facebook-@' + jobid + '.zst', 'facebook-@' + newtarget + '.zst', channel, user, jobid)
command = '!ao <'
options = '--useragent firefox --igset badvideos --explain "For {user} - socialscrape job {jobid}"'.format(user=user, jobid=jobid)
chromeboturls = ['https://www.facebook.com/{target}/'.format(target=newtarget)]
if str(module).startswith("instagram"):
while os.path.isfile('Instagram_run'):
settings.logger.log('SNSCRAPE - instagram scrape already in progress, sleeping for 5 seconds')
time.sleep(5)
open('Instagram_run', 'w').close()
if str(module).startswith("instagram-user"):
settings.logger.log("snscrape --format '{dirtyUrl}' " + module + " " + sanityregex.sub(r'',target) + " >jobs/instagram-@" + jobid)
success = self._snscrape("snscrape --format '{dirtyUrl}' " + quote(module) + " " + quote(sanityregex.sub(r'',target)), "jobs/instagram-@" + jobid, channel, user, jobid)
if not success:
return
if not os.stat("jobs/instagram-@" + jobid).st_size == 0:
settings.logger.log('SNSCRAPE - Finished ' + jobid)
#Insert the profile as per JAA's request :-)
outfile = open("jobs/instagram-@" + jobid, "r")
profileline = "https://www.instagram.com/" + target + "/\n"
lines = outfile.readlines()
lines.insert(0,profileline)
outfile.close()
outfile=open("jobs/instagram-@" + jobid, "w")
outfile.writelines(lines)
outfile.close()
success = self._compress('jobs/instagram-@' + jobid, channel, user, jobid)
if not success:
return
uploadedurl = self._upload('jobs/instagram-@' + jobid + '.zst', 'instagram-@' + quote(sanityregex.sub(r'',target)) + '.zst', channel, user, jobid)
jobfile = "jobs/instagram-@" + jobid + ".zst"
else:
self.send('PRIVMSG', '{user}: Sorry, No results returned for {jobid} - User does not exist'.format(user=user,jobid=jobid),channel)
jobfile = "jobs/instagram-@" + jobid
os.remove('Instagram_run')
elif str(module).startswith("instagram-hashtag"):
success = self._snscrape("snscrape --format '{dirtyUrl}' " + quote(module) + " " + quote(sanityregex.sub(r'',target)), "jobs/instagram-#" + jobid, channel, user, jobid)
if not success:
return
if not os.stat("jobs/instagram-#" + jobid).st_size == 0:
settings.logger.log('SNSCRAPE - Finished ' + jobid)
success = self._compress('jobs/instagram-#' + jobid, channel, user, jobid)
if not success:
return
uploadedurl = self._upload('jobs/instagram-#' + jobid + '.zst', 'instagram-%23' + quote(sanityregex.sub(r'',target)) + '.zst', channel, user, jobid)
jobfile = "jobs/instagram-#" + jobid + ".zst"
else:
self.send('PRIVMSG', '{user}: Sorry, No results returned for {jobid} - Hashtag does not exist'.format(user=user,jobid=jobid),channel)
jobfile = "jobs/instagram-#" + jobid
os.remove('Instagram_run')
if not os.stat(jobfile).st_size == 0:
command = '!a <'
options = '--explain "For {user} - socialscrape job {jobid}" --igset instagram'.format(user=user, jobid=jobid)
os.remove('Instagram_run')
if str(module).startswith("vkontakte"):
if str(module).startswith("vkontakte-user"):
newtarget = target
settings.logger.log("SNSCRAPE - Running with updated settings - snscrape " + quote(module) + " " + newtarget.strip() + " >jobs/vkontakte-@" + jobid)
success = self._snscrape("snscrape " + quote(module) + " " + newtarget.strip(), "jobs/vkontakte-@" + jobid, channel, user, jobid)
if not success:
return
settings.logger.log('SNSCRAPE - Finished ' + jobid)
profileline = "https://vk.com/" + newtarget.strip()
lines = [profileline + "\n"]
with open("jobs/vkontakte-@" + jobid, "r") as outfile:
for line in outfile.read().split():
lines.append(line + "\n")
with open("jobs/vkontakte-@" + jobid, "w") as outfile:
outfile.writelines(lines)
success = self._compress('jobs/vkontakte-@' + jobid, channel, user, jobid)
if not success:
return
uploadedurl = self._upload('jobs/vkontakte-@' + jobid + '.zst', 'vkontakte-@' + newtarget + '.zst', channel, user, jobid)
command = '!ao <'
options = '--igset badvideos --explain "For {user} - socialscrape job {jobid}"'.format(user=user, jobid=jobid)
chromeboturls = ['https://vk.com/{target}'.format(target=newtarget)]
if command is not None and uploadedurl is not None and options is not None:
self.send('PRIVMSG', '{command} {uploadedurl} {options}'.format(**locals()), channel)
#for url in chromeboturls:
# self.send('PRIVMSG', 'chromebot: a {url}'.format(url=url), channel)
def command(self, command, user, channel):
if command[0] == 'help':
self.send('PRIVMSG', '{user}: For IRC commands can be found at - '
'https://github.com/JustAnotherArchivist/socialscrape-bot/blob/master/commands.md'
.format(**locals()), channel)
elif command[0] == 'stop' and self.check_admin(user) == True:
settings.logger.log('EMERGENCY: {user} has requested I stop'.format(**locals()))
settings.run_services.stop()
self.send('PRIVMSG', '{user}: Stopped.'
.format(**locals()), channel)
settings.running = False
elif command[0] == 'update' and self.check_admin(user) == True:
open('STOP', 'w').close()
settings.logger.log('WARNING: {user} has requested I update'.format(**locals()))
self.server.close()
settings.run_services.stop()
elif command[0] == 'version':
try:
snscrapeversion = subprocess.check_output(['snscrape', '--version'], encoding = 'utf-8').strip()
except subprocess.CalledProcessError:
self.send('PRIVMSG', '{user}: Error trying to get snscrape version.', channel)
return
self.send('PRIVMSG', '{user}: Version {version}, {snscrapeversion}.'
.format(user=user, version=settings.version, snscrapeversion=snscrapeversion), channel)
elif command[0] == 'snsupdate' and self.check_admin(user) == True:
# Do the git pull and reload the module here
settings.logger.log('WARNING: {user} has requested I update snscrape'.format(**locals()))
self.send('PRIVMSG','Starting snscrape update')
subprocess.run(["updatesnscrape.sh"])
self.send('PRIVMSG','snscrape update complete')
elif command[0] == 'snscrape':
if any('/' in x for x in command):
self.send('PRIVMSG', '{user}: Command must not contain slashes.'.format(user=user), channel)
return
# Get the site to scrape
try:
function = command[1]
if function not in ('twitter-user', 'twitter-profile', 'twitter-hashtag', 'twitter-search', 'facebook-user', 'instagram-user', 'instagram-hashtag', 'vkontakte-user'):
settings.logger.log('SNSCRAPE - Command not found')
self.send('PRIVMSG', '{user}: Command not found'.format(user=user), channel)
return
queue = Queue()
if function.startswith('twitter-'):
module = command[1]
target = command[2]
if len(command) != 3:
self.send('PRIVMSG', f'{user}: Invalid command', channel)
return
runsnscrape = Process(target=self.run_snscrape, args=(channel, user, module, target))
runsnscrape.start()
if function.startswith('instagram-'):
self.send('PRIVMSG', user + ': Sorry, Instagram scraping is currently disabled.', channel)
return
# sendnudez
module = command[1]
target = command[2]
try:
args = command[3]
runsnscrape = Process(target=self.run_snscrape, args=(channel, user, module, target))
runsnscrape.start()
# runsnscrape = self.processpool.apply_async(self.run_snscrape, args=(channel, user, module, target))
# self.running_instagram.append(runsnscrape)
except IndexError:
runsnscrape = Process(target=self.run_snscrape, args=(channel, user, module, target))
runsnscrape.start()
# runsnscrape = self.processpool.apply_async(self.run_snscrape, args=(channel, user, module, target))
# self.running_instagram.append(runsnscrape)
if function == 'gab':
# whatevenisgab?
settings.logger.log('gab')
if function == 'vkontakte-user':
module = command[1]
target = command[2]
runsnscrape = Process(target=self.run_snscrape, args=(channel, user, module, target))
runsnscrape.start()
if function.startswith('facebook'):
# faceballs
module = command[1]
target = command[2]
runsnscrape = Process(target=self.run_snscrape, args=(channel, user, module, target))
runsnscrape.start()
except IndexError:
self.send('PRIVMSG', user + ': Missing site; try ' + self.nick + ' snscrape instagram-user,instagram-hashtag'\
+ ',twitter-user,twitter-hashtag,twitter-search'.format(user=user), channel)
elif command[0] in ('instagram', 'instagram-user'):
if len(command) != 2 or not re.match(r'^https?://(www\.)?(instagram\.com|instagr\.am)/\S*$', command[1]):
self.send('PRIVMSG', f'{user}: Invalid command; usage: "instagram [URL]"', channel)
return
if command[0] == 'instagram-user':
# Fix capitalisation and add trailing slash if missing
newUrl = command[1].lower()
if newUrl.count('/') == 3:
newUrl = f'{newUrl}/'
if not newUrl.startswith('https://'):
newUrl = f'https://{newUrl.split("//", 1)[1]}'
if not newUrl.startswith('https://www.instagram.com/'):
newUrl = f'https://www.instagram.com/{newUrl.split("/", 3)[3]}'
if newUrl != command[1]:
self.send('PRIVMSG', f'{user}: Fixed your URL from {command[1]} to {newUrl}', channel)
command[1] = newUrl
if command[1].count('/') == 3: # Would recurse through all of Instagram.
self.send('PRIVMSG', f'{user}: Refusing URL with too few slashes', channel)
return
self.send('PRIVMSG', f'!a {command[1]} --igset instagram --concurrency 1 --delay 1000 --useragent firefox --pipeline instagram --explain "For {user}"', channel)
elif command[0] == 'telegram-channel':
if len(command) != 2 or not re.match(r'^[a-zA-Z0-9_]{5,}$', command[1]):
self.send('PRIVMSG', f'{user}: Invalid command; usage: "telegram-channel [CHANNELNAME]"', channel)
return
url = f'https://t.me/{command[1]}'
jobid = abcalc.jobid(url)
self.send('PRIVMSG', f'!a {url} --explain "For {user}"', channel)
self.send('PRIVMSG', f'!ig {jobid} ^https?://t\.me/(?!(s/)?{command[1]})', channel)
self.send('PRIVMSG', f'!ig {jobid} ^https?://t\.me/s/{command[1]}/\d+\?q=%23', channel)