-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_following_ids.py
121 lines (99 loc) · 4.02 KB
/
get_following_ids.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
from keys_api1 import *
import sys
import tweepy
import time
from random import randint
from functools import reduce
import pandas as pd
import csv
#Get ids from csv file
test = pd.read_csv('output.csv')
test = test.drop(test.columns[0], axis=1)
#Preparing the output csv file
# assign header columns
headerList = ['Account_ID', 'Following_ids']
# open CSV file and assign header
with open("./ids/output_ids.csv", 'w') as file:
dw = csv.DictWriter(file, delimiter=',',
fieldnames=headerList)
dw.writeheader()
error_count = 0
#iterate over ids
for index, item in test.iterrows():
t = time.localtime()
current_time = time.strftime("%H:%M:%S", t)
print("Processing of ID : ", item['twitter_ids'], " TIMESTAMP : ", current_time)
while True:
try:
auth = tweepy.OAuthHandler(api_key, api_key_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth, wait_on_rate_limit=True)
id = item['twitter_ids']
c = tweepy.Cursor(api.get_friend_ids, user_id = id, count = 4990)
ids = []
for page in c.pages():
ids.append(page)
#2D list to 1D and saving to a csv output
ids = reduce(lambda x,y :x+y ,ids)
dict = { "id" : id, "twitter_ids" : ids}
df = pd.DataFrame(dict)
df.to_csv("./ids/output_ids.csv", mode='a', index = False, header= False)
#terminate the processus
t = time.localtime()
current_time = time.strftime("%H:%M:%S", t)
print("Processus finished -- ", current_time, " --")
print("Summary : ")
print("Index : ", index, " | Errors_count : ", error_count)
break
#Handling exception by displaying them and resuming the process
except tweepy.TwitterServerError:
t = time.localtime()
current_time = time.strftime("%H:%M:%S", t)
print("Tweepy HTTP Exception detected -- ", current_time, " --")
print ("tweepy.HTTPException = ", tweepy.TwitterServerError)
continue
except tweepy.HTTPException:
t = time.localtime()
current_time = time.strftime("%H:%M:%S", t)
print("Tweepy HTTP Exception detected -- ", current_time, " --")
print ("tweepy.HTTPException = ", tweepy.HTTPException)
#terminate the processus
error_count += 1
t = time.localtime()
current_time = time.strftime("%H:%M:%S", t)
print("Processus finished with an error -- ", current_time, " -- ")
print("Summary : ")
print("Index : ", index, " | Errors_count : ", error_count)
break
except tweepy.TweepyException:
t = time.localtime()
current_time = time.strftime("%H:%M:%S", t)
print("Tweepy Exception detected -- ", current_time, " --")
print ("tweepy.TweepyException = ", tweepy.TweepyException)
continue
except KeyboardInterrupt:
t = time.localtime()
current_time = time.strftime("%H:%M:%S", t)
print("Raising SystemExit -- ", current_time)
#terminate the processus
t = time.localtime()
current_time = time.strftime("%H:%M:%S", t)
print("Processus finished with an error -- ", current_time, " --")
save = []
save.append(index)
dict = {"index : " : save}
index_df = pd.DataFrame(dict)
index_df.to_csv('index.csv')
raise SystemExit
except:
e = sys.exc_info()[0]
t = time.localtime()
current_time = time.strftime("%H:%M:%S", t)
print("Unknown Exception detected -- ", current_time, " --")
print("Error: %s" % e)
continue
save = []
save.append(index)
dict = {"index : " : save}
index_df = pd.DataFrame(dict)
index_df.to_csv('index_final.csv')