-
Notifications
You must be signed in to change notification settings - Fork 31
/
bot.py
118 lines (98 loc) · 5.92 KB
/
bot.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
from ratelimiter import RateLimiter
from instagram_private_api import Client, ClientCompatPatch
from dotenv import load_dotenv, find_dotenv
import json
import requests
import os
from time import sleep
from random import randint, choice
from color_it import *
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
rate_limiter = RateLimiter(max_calls=1, period=5.0)
clients = []
feed = []
contains_comment = False
comments = [
'Hi, please dont use the blacklivesmatter tag as it is currently blocking important info from being shared. '
'Please delete and repost with #BlackoutTuesday instead (Editing the caption wont work). '
'If you want other ways to help please check out @blm_tag_bot. Thank you :)',
'Please use the #blackouttuesday instead of blacklivesmatter if you' 're posting a black square. '
'Please delete and repost with #BlackoutTuesday instead (Editing the caption wont work). '
'If you want other ways to help please check out @blm_tag_bot. Thank you :)',
'It appears you have posted a black square in the wrong hashtag blacklivesmatter is used to spread critical information. '
'Please delete and repost with #BlackoutTuesday instead (Editing the caption wont work). '
'If you want other ways to help please check out @blm_tag_bot. Thank you :)',
'Posting black screens is hiding critical information please delete your image and repost it with the #BlackoutTuesday instead. '
'If you want other ways to help please check out @blm_tag_bot. Thank you :)',
#using uppercase I's instead of L's. except the last line
'Hi, please dont use the bIackIivesmatter tag as it is currently bIocking important info from being shared. '
'PIease deIete and repost with #BlackoutTuesday instead (Editing the caption wont work). '
'If you want other ways to help please check out @blm_tag_bot. Thank you :)',
'PIease use the #blackouttuesday instead of blackIivesmatter if you' 're posting a bIack square. '
'Please deIete and repost with #BlackoutTuesday instead (Editing the caption wont work). '
'If you want other ways to help please check out @blm_tag_bot. Thank you :)',
'It appears you have posted a bIack square in the wrong hashtag bIackIivesmatter. The hashtag is cluttered with black tiles and it is making it difficult for people to reach critical information.'
'PIease deIete and repost with #BlackoutTuesday instead (Editing the caption wont work). '
'If you want other ways to help please check out @blm_tag_bot. Thank you :)',
'Posting black screens is hiding critical information on the BlackLivesMatter tag, please deIete your image and repost it with the #BlackoutTuesday instead. IMPORTANT: Editing will not work'
'If you want other ways to help please check out @blm_tag_bot. Thank you :)'
]
load_dotenv(find_dotenv())
init_color_it()
# login to instagram
with open('./accounts.json') as f:
data = json.load(f)
for acc in data:
print('Logging in with username %s' % acc['username'] + '\n')
client = Client(acc['username'], acc['password'])
clients.append(client)
feed = client.feed_tag('blacklivesmatter', client.generate_uuid())
# Goes over the pictures in the black lives matter hashtag
while len(feed) != 0:
for client in clients:
with rate_limiter:
print('Looking for an image... \n')
post = feed['items'].pop(0)
print('Found ' + str(feed['num_results']) + ' images. \n')
wait_time = randint(10, 30)
print(color('Waiting ' + str(wait_time) + ' sec. \n', colors.YELLOW))
sleep(wait_time)
print('Analyzing post ' + post['code'] + ' ...\n')
if 'image_versions2' in post:
try:
url = post['image_versions2']['candidates'][0]['url']
res = requests.post(os.getenv("CLOUD_FUNCTION_URL"), data={'img_url': url})
json_res = res.json()
# check if the image is a black square
if json_res['solid']:
code = post['code']
if 'comments_disabled' in post:
print('Bot cannot comment on post due to disabled comments: %s' % code)
continue
if 'comment_count' in post and post['comment_count'] > 0:
for comment in post['preview_comments']:
if comment['text'] in comments:
contains_comment = True
continue
if not contains_comment:
print(color('Solid image found. Informing user on post %s' % code + '\n', colors.ORANGE))
client.post_comment(post['id'], choice(comments))
print(color('commented successfully. \n', colors.GREEN))
else:
print('Bot has already commented on post: %s' % code)
contains_comment = False
else:
print(color('Solid image found. Informing user on post %s' % code + '\n', colors.ORANGE))
client.post_comment(post['id'], choice(comments))
print(color('commented successfully. \n', colors.GREEN))
else:
print('Image isn''t a black square.. moving on the next..')
except Exception as e:
if hasattr(e, 'error_response') and 'spam": true,' in e.error_response:
print(color("Error : Commented too many times. \n", colors.RED))
else:
print(color(repr(e) + '\n', colors.RED))
continue
if len(feed) == 1:
feed = client.feed_tag('blacklivesmatter', client.generate_uuid())