forked from shane-tw/500px-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
500px.py
308 lines (270 loc) · 10 KB
/
500px.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import requests, time, json, os
from bs4 import BeautifulSoup
from random import randint
userSession = requests.Session()
login_params = {
'authenticity_token': '',
'session[email]': input('Whats your email?: '),
'session[password]': input('Whats your password?: ')
}
generic_headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36'
}
csrf_headers = {
'X-CSRF-Token': '',
'X-Requested-With': 'XMLHttpRequest',
'User-Agent': generic_headers['User-Agent']
}
pendingFollowList = []
acceptedFollowList = []
ignoredFollowList = []
def retrieveLists():
global pendingFollowList, acceptedFollowList, ignoredFollowList
if os.path.exists('pendingUsers.txt'):
with open('pendingUsers.txt', 'r') as f:
pendingFollowList = json.loads(f.read())
if os.path.exists('acceptedUsers.txt'):
with open('acceptedUsers.txt', 'r') as f:
acceptedFollowList = json.loads(f.read())
if os.path.exists('ignoredUsers.txt'):
with open('ignoredUsers.txt', 'r') as f:
ignoredFollowList = json.loads(f.read())
def isUserPending(targetUserName):
global pendingFollowList
userPending = False
for i, v in enumerate(pendingFollowList):
if v['name'] == targetUserName:
userPending = True
break
return userPending
def isUserAccepted(targetUserName):
global acceptedFollowList
userAccepted = False
for i, v in enumerate(acceptedFollowList):
if v['name'] == targetUserName:
userAccepted = True
break
return userAccepted
def isUserIgnored(targetUserName):
global ignoredFollowList
userIgnored = False
for i, v in enumerate(ignoredFollowList):
if v['name'] == targetUserName:
userIgnored = True
break
return userIgnored
def followUser(targetUserName):
global userSession, numFollowsDone
continueLoop = True
while continueLoop:
try:
followResp = userSession.post('https://500px.com/' + targetUserName + '/follow', timeout = 5, headers = csrf_headers)
if followResp.status_code == 200:
print('Followed ' + targetUserName + '.')
numFollowsDone += 1
addUserToPendingList(targetUserName)
continueLoop = False
elif followResp.status_code == 403:
print('Already followed ' + targetUserName + '.')
continueLoop = False
else:
print('A server error (' + str(followResp.status_code) + ') occured. Retrying...')
print('Error page: ' + followResp.url)
time.sleep(5)
except requests.exceptions.RequestException:
print('Web page timed out. Retrying...')
time.sleep(5)
time.sleep(20)
def addUserToPendingList(targetUserName):
global pendingFollowList
pendingFollowList.append({'name': targetUserName, 'time_followed': time.time()})
with open('pendingUsers.txt', 'w') as f:
f.write(json.dumps(pendingFollowList))
def addUserToAcceptedList(targetUserName):
global acceptedFollowList
acceptedFollowList.append({'name': targetUserName, 'time_followed': time.time()})
with open('acceptedUsers.txt', 'w') as f:
f.write(json.dumps(acceptedFollowList))
def addUserToIgnoredList(targetUserName):
global ignoredFollowList
ignoredFollowList.append({'name': targetUserName, 'time_followed': time.time()})
with open('ignoredUsers.txt', 'w') as f:
f.write(json.dumps(ignoredFollowList))
def userExists(targetUserName):
global userSession
continueLoop = True
while continueLoop:
try:
userResp = userSession.get('https://500px.com/' + targetUserName, timeout = 5, headers = generic_headers)
if userResp.status_code == 200:
return True
elif userResp.status_code == 404:
return False
else:
print('A server error (' + str(userResp.status_code) + ') occured. Retrying...')
print('Error page: ' + userResp.url)
time.sleep(5)
except requests.exceptions.RequestException:
print('Web page timed out. Retrying...')
time.sleep(5)
time.sleep(20)
def unfollowUser(targetUserName):
global userSession
continueLoop = True
while continueLoop:
try:
unfollowResp = userSession.post('https://500px.com/' + targetUserName + '/unfollow', timeout = 5, headers = csrf_headers)
if unfollowResp.status_code == 200:
print('Unfollowed ' + targetUserName + '.')
continueLoop = False
else:
print('A server error (' + str(unfollowResp.status_code) + ') occured. Retrying...')
print('Error page: ' + unfollowResp.url)
time.sleep(5)
except requests.exceptions.RequestException:
print('Web page timed out. Retrying...')
time.sleep(5)
time.sleep(20)
def removeUserFromPendingList(targetUserName):
global pendingFollowList
for i, v in enumerate(list(pendingFollowList)):
if v['name'] == targetUserName:
pendingFollowList.remove(v)
break
with open('pendingUsers.txt', 'w') as f:
f.write(json.dumps(pendingFollowList))
def removeUserFromAcceptedList(targetUserName):
global acceptedFollowList
for i, v in enumerate(list(acceptedFollowList)):
if v['name'] == targetUserName:
acceptedFollowList.remove(v)
break
with open('acceptedUsers.txt', 'w') as f:
f.write(json.dumps(acceptedFollowList))
def removeUserFromIgnoredList(targetUserName):
global ignoredFollowList
for i, v in enumerate(list(ignoredFollowList)):
if v['name'] == targetUserName:
ignoredFollowList.remove(v)
break
with open('ignoredUsers.txt', 'w') as f:
f.write(json.dumps(ignoredFollowList))
# Retrieving the list of currently pending followed users by the bot.
retrieveLists()
# Time to remove anyone in any list for more than a week.
currentTime = time.time()
for i, v in enumerate(list(acceptedFollowList)):
if currentTime - v['time_followed'] > 604800:
acceptedFollowList.remove(v)
with open('acceptedUsers.txt', 'w') as f:
f.write(json.dumps(acceptedFollowList))
for i, v in enumerate(list(ignoredFollowList)):
if currentTime - v['time_followed'] > 604800:
ignoredFollowList.remove(v)
with open('ignoredUsers.txt', 'w') as f:
f.write(json.dumps(ignoredFollowList))
# This is used in order to obtain the authenticity token required for logging in.
continueLoop = True
while continueLoop:
try:
loginPage = userSession.get('https://500px.com/login', timeout = 5, headers = generic_headers)
if loginPage.status_code == 200:
print('Retrieved login page.')
continueLoop = False
else:
print('A server error (' + str(loginPage.status_code) + ') occured. Retrying...')
print('Error page: ' + loginPage.url)
time.sleep(5)
except requests.exceptions.RequestException:
print('Web page timed out. Retrying...')
time.sleep(5)
time.sleep(20)
loginPage_soup = BeautifulSoup(loginPage.text, 'html.parser')
login_params['authenticity_token'] = loginPage_soup.find('meta', {'name': 'csrf-token'}).get('content')
csrf_headers['X-CSRF-Token'] = login_params['authenticity_token']
# This is the actual login request.
continueLoop = True
while continueLoop:
try:
userLogin = userSession.post('https://api.500px.com/v1/session', data = login_params, timeout = 5, headers = generic_headers)
if userLogin.status_code == 200:
print('Logged in successfully.')
continueLoop = False
else:
print('A server error (' + str(userLogin.status_code) + ') occured. Retrying...')
print('Error page: ' + userLogin.url)
time.sleep(5)
except requests.exceptions.RequestException:
print('Web page timed out. Retrying...')
time.sleep(5)
time.sleep(20)
# Getting my user info from login response.
myUserInfo = json.loads(userLogin.text)['user']
# Time to see who has actually bothered following us.
pageNum = 1
pendingUserNames = [pendingFollowUser['name'] for pendingFollowUser in pendingFollowList]
myFollowers_json = []
while True:
try:
myFollowers = userSession.get('https://api.500px.com/v1/users/' + str(myUserInfo['id']) + '/followers?fullformat=0&page=' + str(pageNum) + '&rpp=50', timeout = 5, headers = generic_headers)
if myFollowers.status_code != 200:
print('A server error (' + str(myFollowers.status_code) + ') occured. Retrying...')
print('Error page: ' + myFollowers.url)
time.sleep(5)
continue
except requests.exceptions.RequestException:
print('Web page timed out. Retrying...')
time.sleep(5)
continue
tmpFollowers_json = json.loads(myFollowers.text)
myFollowers_json += tmpFollowers_json['followers']
if pageNum == tmpFollowers_json['followers_pages']:
break
pageNum += 1
time.sleep(20)
for follower in list(myFollowers_json):
currentTime = time.time()
if not follower['username'] in pendingUserNames:
myFollowers_json.remove(follower)
continue
removeUserFromPendingList(follower['username'])
addUserToAcceptedList(follower['username'])
print(follower['username'] + ' followed you. Accepted.')
pendingUserNames.remove(follower['username'])
myFollowers_json.remove(follower)
for follower in list(pendingFollowList):
currentTime = time.time()
if currentTime - follower['time_followed'] <= 172800:
continue
removeUserFromPendingList(follower['name'])
if userExists(follower['name']):
unfollowUser(follower['name'])
addUserToIgnoredList(follower['name'])
pendingUserNames.remove(follower['name'])
print(follower['name'] + ' didn\'t follow you. Ignored and unfollowed.')
print('Review of followed users finished.')
# Time to view the up-and-coming and follow more people :)
pageNum = 1
numFollowsWanted = 101
numFollowsDone = 0
while numFollowsDone < numFollowsWanted:
try:
upcomingPage = userSession.get('https://api.500px.com/v1/photos?feature=upcoming&include_states=false&page=' + str(pageNum) + '&rpp=50', timeout = 5, headers = csrf_headers)
if upcomingPage.status_code != 200:
print('A server error (' + str(upcomingPage.status_code) + ') occured. Retrying...')
print('Error page: ' + upcomingPage.url)
time.sleep(5)
continue
except requests.exceptions.RequestException:
print('Web page timed out. Retrying...')
time.sleep(5)
continue
upcomingPage_json = json.loads(upcomingPage.text)
for upcomingPhoto in upcomingPage_json['photos']:
if not isUserPending(myUserInfo['username']) and not isUserAccepted(myUserInfo['username']) and not isUserIgnored(myUserInfo['username']) and numFollowsDone < numFollowsWanted:
followUser(upcomingPhoto['user']['username'])
elif numFollowsDone >= numFollowsWanted:
break
pageNum += 1
time.sleep(20)
print('Finished. No more users left to follow.')