forked from shane-tw/500px-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path500px.py
340 lines (297 loc) · 12.7 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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#!/usr/bin/env python
import requests, time, json, os
from bs4 import BeautifulSoup
from random import randint
scriptDirectory = os.path.abspath(os.path.dirname(__file__))
logDate = time.strftime('%Y-%m-%d')
userSession = requests.Session()
userSession.headers.update({
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'
})
pendingFileName = 'pendingUsers.txt'
acceptedFileName = 'acceptedUsers.txt'
ignoredFileName = 'ignoredUsers.txt'
pendingFilePath = scriptDirectory + '/' + pendingFileName
acceptedFilePath = scriptDirectory + '/' + acceptedFileName
ignoredFilePath = scriptDirectory + '/' + ignoredFileName
logFileName = logDate + '_log.txt'
logFilePath = scriptDirectory + '/logs/'
loginParams = {
'authenticity_token': '',
'session[email]': '',
'session[password]': ''
}
csrfHeaders = {
'X-CSRF-Token': '',
'X-Requested-With': 'XMLHttpRequest'
}
pendingFollowList = []
acceptedFollowList = []
ignoredFollowList = []
def printToLog(string):
global logFilePath, logFileName
logTime = time.strftime('%H:%M')
if not os.path.exists(logFilePath):
os.makedirs(logFilePath)
with open(logFilePath + logFileName, 'a+') as f:
f.write(logTime + ' - ' + string + '\n')
print(logTime + ' - ' + string)
def retrieveLists():
global pendingFilePath, acceptedFilePath, ignoredFilePath
global pendingFollowList, acceptedFollowList, ignoredFollowList
if os.path.exists(pendingFilePath):
with open(pendingFilePath, 'r') as f:
pendingFollowList = json.loads(f.read())
if os.path.exists(acceptedFilePath):
with open(acceptedFilePath, 'r') as f:
acceptedFollowList = json.loads(f.read())
if os.path.exists(ignoredFilePath):
with open(ignoredFilePath, '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 = csrfHeaders)
if followResp.status_code == 200:
printToLog('Followed ' + targetUserName + '.')
numFollowsDone += 1
addUserToPendingList(targetUserName)
continueLoop = False
elif followResp.status_code == 404:
printToLog('User ' + targetUserName + ' no longer exists. Skipped follow.')
continueLoop = False
elif followResp.status_code == 403:
printToLog('Already followed ' + targetUserName + '.')
continueLoop = False
else:
printToLog('A server error (' + str(followResp.status_code) + ') occured. Retrying...')
printToLog('Error page: ' + followResp.url)
time.sleep(5)
except requests.exceptions.RequestException:
printToLog('Web page timed out. Retrying...')
time.sleep(5)
time.sleep(20)
def addUserToPendingList(targetUserName):
global pendingFollowList, pendingFilePath
pendingFollowList.append({'name': targetUserName, 'time_followed': time.time()})
with open(pendingFilePath, 'w') as f:
f.write(json.dumps(pendingFollowList))
def addUserToAcceptedList(targetUserName):
global acceptedFollowList, acceptedFilePath
acceptedFollowList.append({'name': targetUserName, 'time_followed': time.time()})
with open(acceptedFilePath, 'w') as f:
f.write(json.dumps(acceptedFollowList))
def addUserToIgnoredList(targetUserName):
global ignoredFollowList, ignoredFilePath
ignoredFollowList.append({'name': targetUserName, 'time_followed': time.time()})
with open(ignoredFilePath, '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)
if userResp.status_code == 200:
return True
elif userResp.status_code == 404:
return False
else:
printToLog('A server error (' + str(userResp.status_code) + ') occured. Retrying...')
printToLog('Error page: ' + userResp.url)
time.sleep(5)
except requests.exceptions.RequestException:
printToLog('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 = csrfHeaders)
if unfollowResp.status_code == 200:
printToLog('Unfollowed ' + targetUserName + '.')
continueLoop = False
elif unfollowResp.status_code == 404:
printToLog('User ' + targetUserName + ' no longer exists. Skipped unfollow.')
continueLoop = False
else:
printToLog('A server error (' + str(unfollowResp.status_code) + ') occured. Retrying...')
printToLog('Error page: ' + unfollowResp.url)
time.sleep(5)
except requests.exceptions.RequestException:
printToLog('Web page timed out. Retrying...')
time.sleep(5)
time.sleep(20)
def removeUserFromPendingList(targetUserName):
global pendingFollowList, pendingFilePath
for i, v in enumerate(list(pendingFollowList)):
if v['name'] == targetUserName:
pendingFollowList.remove(v)
break
with open(pendingFilePath, 'w') as f:
f.write(json.dumps(pendingFollowList))
def removeUserFromAcceptedList(targetUserName):
global acceptedFollowList, acceptedFilePath
for i, v in enumerate(list(acceptedFollowList)):
if v['name'] == targetUserName:
acceptedFollowList.remove(v)
break
with open(acceptedFilePath, 'w') as f:
f.write(json.dumps(acceptedFollowList))
def removeUserFromIgnoredList(targetUserName):
global ignoredFollowList, ignoredFilePath
for i, v in enumerate(list(ignoredFollowList)):
if v['name'] == targetUserName:
ignoredFollowList.remove(v)
break
with open(ignoredFilePath, '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(acceptedFilePath, '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(ignoredFilePath, '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)
if loginPage.status_code == 200:
printToLog('Retrieved login page.')
continueLoop = False
else:
printToLog('A server error (' + str(loginPage.status_code) + ') occured. Retrying...')
printToLog('Error page: ' + loginPage.url)
time.sleep(5)
except requests.exceptions.RequestException:
printToLog('Web page timed out. Retrying...')
time.sleep(5)
time.sleep(20)
loginPage_soup = BeautifulSoup(loginPage.text, 'html.parser')
loginParams['authenticity_token'] = loginPage_soup.find('meta', {'name': 'csrf-token'}).get('content')
csrfHeaders['X-CSRF-Token'] = loginParams['authenticity_token']
# This is the actual login request.
continueLoop = True
while continueLoop:
try:
userLogin = userSession.post('https://api.500px.com/v1/session', data = loginParams, timeout = 5)
if userLogin.status_code == 200:
printToLog('Logged in successfully.')
continueLoop = False
else:
printToLog('A server error (' + str(userLogin.status_code) + ') occured. Retrying...')
printToLog('Error page: ' + userLogin.url)
time.sleep(5)
except requests.exceptions.RequestException:
printToLog('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 # Do not change
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)
if myFollowers.status_code != 200:
printToLog('A server error (' + str(myFollowers.status_code) + ') occured. Retrying...')
printToLog('Error page: ' + myFollowers.url)
time.sleep(5)
continue
except requests.exceptions.RequestException:
printToLog('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'])
printToLog(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']): # Commented this out, so should save some time when unfollowing.
unfollowUser(follower['name'])
addUserToIgnoredList(follower['name'])
pendingUserNames.remove(follower['name'])
printToLog(follower['name'] + ' didn\'t follow you. Ignored and unfollowed.')
printToLog('Review of followed users finished.')
# Time to view the up-and-coming and follow more people :)
pageNum = 1 # Do not change.
numFollowsWanted = 101 # If I recall, 101 is the daily limit, and any more than this fails.
numFollowsDone = 0 # Do not change.
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 = csrfHeaders)
if upcomingPage.status_code != 200:
printToLog('A server error (' + str(upcomingPage.status_code) + ') occured. Retrying...')
printToLog('Error page: ' + upcomingPage.url)
time.sleep(5)
continue
except requests.exceptions.RequestException:
printToLog('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)
printToLog('Finished. No more users left to follow.')