-
Notifications
You must be signed in to change notification settings - Fork 54
/
instabot.py
138 lines (106 loc) · 4.26 KB
/
instabot.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
import re
import csv
from time import sleep
import os
import sys
import pathlib
from timeit import default_timer as timer
import datetime
import urllib3
import instaloader
# Get instance
L = instaloader.Instaloader()
# Login or load session
L.login('username', 'password') # (login)
#L.interactive_login(USER) # (ask password on terminal)
# L.load_session_from_file('dslr.lover.nepal') # (load session created w/
pathlib.Path('downloads/').mkdir(parents=True, exist_ok=True)
http = urllib3.PoolManager()
start = timer()
curr = str(datetime.datetime.now())
def wait_for_internet_connection():
while True:
try:
response = http.request('GET', 'http://ku.edu.np')
return
except:
print('No internet connection.\nTrying after 5 seconds.\n')
sleep(5)
# wait_for_internet_connection()
f = open('input.txt','r')
accounts = f.read()
p = accounts.split('\n')
with open('last.txt','r') as f:
last = f.read()
last=last.strip()
print('Last account scraped was:',last)
for profile in p:
if last in profile and len(last)>2:
print(last,profile)
p.remove(profile)
# input()
print('Resuming from:',p[0])
PROFILE = p[:]
print(PROFILE)
print('Total accounts:',len(PROFILE))
for ind in range(len(PROFILE)):
pro = PROFILE[ind]
try:
# wait_for_internet_connection()
print('\n\nGetting followers from',pro)
filename = 'downloads/'+pro+'.csv'
with open(filename,'a',newline='',encoding="utf-8") as csvf:
csv_writer = csv.writer(csvf)
csv_writer.writerow(['user_id','username','fullname','is_verified','is_private','media_count','follower_count','following_count','bio','website','emails','last_activity','scrape_of', 'scraped_at'])
profile = instaloader.Profile.from_username(L.context, pro)
main_followers = profile.followers
count = 0
total=0
# Print list of followees
for person in profile.get_followers():
try:
# wait_for_internet_connection()
total+=1
user_id = person.userid
username = person.username
fullname = person.full_name
is_verified = person.is_verified
is_private = person.is_private
media_count = person.mediacount
follower_count = person.followers
following_count = person.followees
bio = person.biography
emails = re.findall(r"[a-z0-9\.\-+_]+@[a-z0-9\.\-+_]+\.[a-z]+", bio)
website = person.external_url
#last activity
try:
follower_profile = instaloader.Profile.from_username(L.context, username)
for post in follower_profile.get_posts():
last_activity = post.date_local
break
except Exception as e:
print(e)
last_activity=''
print('Username:',username)
print('Last Activity',last_activity)
with open(filename,'a',newline='') as csvf:
csv_writer = csv.writer(csvf)
csv_writer.writerow([user_id,username,fullname,is_verified,is_private,media_count,follower_count,following_count,bio,website,emails,last_activity,pro,curr])
# os.system('clear')
# os.system('cls' if os.name == 'nt' else 'clear')
print('--------------------------------------------------------------------------------\nTotal followers scraped:',total,' out of',main_followers)
print('Time:',str(datetime.timedelta(seconds=(timer()-start))))
print('Current Account:',ind+1,'\t Remaining Accounts:',len(PROFILE)-ind-1 ,'\nAccount Name:',pro)
except Exception as e:
print(e)
#saving the last account for resume
f=open('last.txt','w+')
f.write(pro)
f.close()
#log of completed account
f=open('completed.txt','a+')
f.write(pro+'\n')
f.close()
# (likewise with profile.get_followers())
except:
print('Skipping',pro)