-
Notifications
You must be signed in to change notification settings - Fork 0
/
instaOneTimeCompare.py
183 lines (146 loc) · 6.4 KB
/
instaOneTimeCompare.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
from instaclient import InstaClient
from instaclient.errors import *
from credentials import *
import os
from datetime import datetime
from discord_webhook import DiscordWebhook, DiscordEmbed
import time
import gspread
# Initialise Time
now = datetime.now()
init_time = now.strftime("%H:%M:%S")
init_time_with_day = now.strftime("%Y-%m-%d %H:%M:%S.%f")
# Webhook Config
webhook = DiscordWebhook(url=discord_webhook_url, avatar_url="https://i.imgur.com/IpIG5TP.png", username="Instagram Statistics Tracker")
footer_text = "Silverarmor's Instagram tracking of " + scrape_username
embed = DiscordEmbed(title="Initialised", description="Initialised at " + init_time_with_day, color=0xFEFEFE)
# Add embed object to webhook
webhook.add_embed(embed)
# Send Webhook
response = webhook.execute()
webhook.remove_embed(0)
# # Uncomment if you want to prompt user for account to scrape. Else will use credentials.py's version
# scrape_username = input("Enter an Instagram account's username to scrape it's data: ")
# CREDIT
# https://medium.com/scrape-instagram-followers/scrape-instagram-followers-with-python-eba64e84048
print("Initialised. Starting login & scrape")
# ! SCRAPING
# Create a instaclient object. Place as driver_path argument the path that leads to where you saved the chromedriver.exe file
client = InstaClient(driver_path=driver_path, localhost_headless=True)
# # Backup where headless is not desired.
# client = InstaClient(driver_path=driver_path)
try:
# Login
client.login(username=username, password=password)
except VerificationCodeNecessary:
# This error is raised if the user has 2FA turned on.
code = input('Enter the 2FA security code generated by your Authenticator App or sent to you by SMS')
client.input_verification_code(code)
except SuspisciousLoginAttemptError as error:
# This error is raised by Instagram's anti-bot measures
if error.mode == SuspisciousLoginAttemptError.EMAIL:
code = input('Enter the security code that was sent to you via email: ')
else:
code = input('Enter the security code that was sent to you via SMS: ')
client.input_security_code(code)
print("Logged in")
time.sleep(5) # Sleep 5 seconds to not be a bot
# Scrape Instagram followers
try:
# Try to get the users following the scrape user, as aTuple
followers = client.get_followers(user=scrape_username, count=None, use_api=False, callback_frequency=100)
# Changing from Tuple to List (Taking only the first item in Tuple)
followers = followers[0]
except InvalidUserError:
# Exception raised if the username is not valid
print('The username is not valid')
except PrivateAccountError:
# Exception raised if the account you are trying to scrape is private
print('{} is a private account'.format(scrape_username))
except:
client.disconnect()
time.sleep(5) # Sleep 5 seconds to chill out
# Scrape Instagram following
try:
# Try to get the users following the scrape user, as a Tuple
following = client.get_following(user=scrape_username, count=None, use_api=False, callback_frequency=100)
# Changing from Tuple to List (Taking only the first item in Tuple)
following = following[0]
except InvalidUserError:
# Exception raised if the username is not valid
print('The username is not valid')
except PrivateAccountError:
# Exception raised if the account you are trying to scrape is private
print('{} is a private account'.format(scrape_username))
except:
client.disconnect()
time.sleep(5) # Sleep 5 seconds to chill out
# Closing the client to prevent memory leaks
client.disconnect()
print("Scraping complete & client disconnected. Starting data processing...")
# DATA PROCESSING & READING/SAVING TO FILE
# Processing Data (finding differences)
following_me_only = list((set(followers) - set(following)))
following_them_only = list((set(following) - set(followers)))
# Converting into comma separated string for readability
"""NOTE THIS ALSO CHANGES VARS FROM LIST to STR"""
new_following_me_only = (', '.join(following_me_only))
new_following_them_only = (', '.join(following_them_only))
# Escaping any underscores - to prevent discord formatting
new_following_me_only = new_following_me_only.replace("_", "\\_")
new_following_them_only = new_following_them_only.replace("_", "\\_")
# Splitting string into 1000 characters per list, since webhooks' embed description are limited to 1024 characters
# Maxmimum message length? Will split message(s) into this number if required.
# Split Function
def string_divide(string, div):
list = []
for i in range(0, len(string), div):
list.append(string[i:i+div])
return list
# How many chars in each string in the list?
split_length = 1000
# Splitting
new_following_me_only = string_divide(new_following_me_only, split_length)
new_following_them_only = string_divide(new_following_them_only, split_length)
# WEBHOOK SENDING
# # Webhook Data Message
# VARS
data_description = "**Ran Successfully**\nTracking " + scrape_username
# Colours
color_data = 0x7289da
color_nolonger_following_me = 0xFF0000
color_new_following_me = 0x00FF00
color_nolonger_following_them = 0x0000FF
color_new_following_them = 0xFF8C00
color_no_change = 0xFFFFFF
# General Data Webhook
embed = DiscordEmbed(title="Silverarmor's Instagram Tracker", description=data_description, color=color_data)
embed.set_timestamp()
embed.set_footer(text="Initialised at " + str(init_time))
embed.set_thumbnail(url='https://i.imgur.com/IpIG5TP.png')
# Send General Data Webhook
webhook.add_embed(embed)
response = webhook.execute()
webhook.remove_embed(0)
time.sleep(0.5) # Sleep .5 secs to prevent ratelimiting
# new_following_me_only
for msg in new_following_me_only:
# Create embed object for webhook
embed = DiscordEmbed(title="Users who started following you", description=msg, color=color_new_following_me)
# Add embed object to webhook
webhook.add_embed(embed)
# Send Webhook
response = webhook.execute()
webhook.remove_embed(0)
time.sleep(0.5) # Sleep .5 secs to prevent ratelimiting
# new_following_them_only
for msg in new_following_them_only:
# Create embed object for webhook
embed = DiscordEmbed(title="Users you started following", description=msg, color=color_new_following_them)
# Add embed object to webhook
webhook.add_embed(embed)
# Send Webhook
response = webhook.execute()
webhook.remove_embed(0)
time.sleep(0.5) # Sleep .5 secs to prevent ratelimiting
print("Webhooks Completed")