-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
116 lines (102 loc) · 3.58 KB
/
main.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
import time
import pymysql
from twython import Twython
userStart = 'laurebretton'
def loginTwitter():
try:
twitter = Twython('###','###','###','###')
except:
return False
return twitter
def getFollowers(db, twitter, user, i):
next_cursor = -1
while(next_cursor):
print("Iterations : ",i)
print("Utilisateur :",user)
get_friends = twitter.get_friends_list(screen_name=user,count=200,cursor=next_cursor)
for friend in get_friends["users"]:
if logToDb(db,user,friend["screen_name"]) == False:
exit()
next_cursor = get_friends["next_cursor"]
time.sleep(60)
if next_cursor == -1:
next_cursor = 0
i += 1
userId = getUserIdWithDb(db,user)
addInDbFollowingCheck(db, userId)
nextUser = getNextUser(db)
getFollowers(db,twitter,nextUser,i)
def connectToDb():
connection = pymysql.connect('localhost','###','###',db='###')
return connection
def closeDb(db):
db.close()
def getNextUser(db):
with db.cursor() as cursor:
sql = "SELECT screenName FROM work.users WHERE idUsers NOT IN (SELECT fkIdUsers FROM followingcheck) LIMIT 1;"
cursor.execute(sql)
db.commit()
result = cursor.fetchone()
return result[0]
def addInDbFollowingCheck(db, userId):
with db.cursor() as cursor:
sql = "INSERT INTO followingcheck (fkIdUsers) VALUES (%s)"
cursor.execute(sql,(userId))
db.commit()
def logToDb(db,user,following):
try:
with db.cursor() as cursor:
idUser = getUserIdWithCursor(cursor,user)
if not idUser:
sql = "INSERT INTO users (screenName) VALUES (%s)"
cursor.execute(sql,(user))
db.commit()
idUser = getUserIdWithCursor(cursor,user)
idFollow = getUserIdWithCursor(cursor,following)
if not idFollow:
sql = "INSERT INTO users (screenName) VALUES (%s)"
cursor.execute(sql,(following))
db.commit()
idFollow = getUserIdWithCursor(cursor,following)
sql = "INSERT INTO following (fkIdUsers,fkFollowingThisUsers) VALUES (%s,%s)"
cursor.execute(sql,(idUser,idFollow))
db.commit()
except:
return False
def getUserIdWithDb(db,screenName):
with db.cursor() as cursor:
sql = "SELECT idUsers FROM users WHERE screenName = %s"
cursor.execute(sql,(screenName))
result = cursor.fetchone()
if not result:
return []
return result[0]
def getUserIdWithCursor(cursor,screenName):
sql = "SELECT idUsers FROM users WHERE screenName = %s"
cursor.execute(sql,(screenName))
result = cursor.fetchone()
if not result:
return []
return result[0]
def main():
twitter = loginTwitter()
if twitter == False:
exit()
else:
print("Connexion valide !")
try:
db = connectToDb()
with db.cursor() as cursor:
id = getUserIdWithCursor(cursor,userStart)
if not id:
sql = "INSERT INTO users (screenName) VALUES (%s)"
cursor.execute(sql,(userStart))
db.commit()
getFollowers(db,twitter, userStart, 1492)
except:
print("MARCHE PAS BORDEL DE FION")
print("Fini")
main()
#1241443850524254213-zmc3ZMYQNiQIeSiNP9LOibN3teRKEw
#secret
#OQfLfkIplMWF2dJOxgDGxayqJKgwKPYGNYa0FmTNs2Tgx