-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathKeepMain.py
203 lines (155 loc) · 4.79 KB
/
KeepMain.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
#! usr/bin/python
#coding=utf-8
'''
Author : Lip/yeyimilk
Date : 2017/03/22
Email : [email protected]
Please feel free to contact me if you have any questions. :)
'''
import json
import os
import time
import requests
import urllib2
keepHost = "https://api.gotokeep.com"
homeSpotlight = "/v1.1/home/spotlight"
people = "/v2/people"
def httpGet(url):
headers = {
'Content-Type':'application/json; charset=utf-8',
'Server':'openresty'
}
responseData = requests.get(url)
if responseData.status_code == 200:
result = responseData.text
return result
else:
print "-----SO SAD, httpGet error, url = " + url
return ''
'''
get user id from discovery tab view
'''
def getEntriesIds():
homeSpotlightUrl = keepHost + homeSpotlight
content = httpGet(homeSpotlightUrl)
if content == "":
return []
responseJson = json.loads(content)
try:
errorCode = responseJson['errorCode']
if errorCode != 0:
return []
resultEntriesIds = []
data = responseJson['data']
for dataContent in data:
if 'entries' in dataContent['schema']:
resultEntriesIds.append(dataContent['_id'])
return resultEntriesIds
except Exception,e:
print '-----SO SAD, getEntriesIds fail ----'
print Exception, ":", e
return []
return []
'''
to known this user's gender
'''
def getUserIdWithRightGender(entryId, wantedGender):
entryUrl = keepHost + '/v1.1/entries/' + entryId + "?limit=20&reverse=true"
content = httpGet(entryUrl)
if content == "":
return False
responseJson = json.loads(content)
try:
errorCode = responseJson['errorCode']
if errorCode != 0:
return False
author = responseJson['data']['author']
if author:
gender = author['gender']
if gender.lower() == wantedGender:
rid = author['_id']
if rid:
return rid
return ''
except Exception,e:
print '-----SO SAD, isUserRightGender error ----'
print Exception, ':', e
return ''
return ''
'''
'''
def getUserPhotoUrlList(userId):
timelineUrl = keepHost + people + '/' + userId + '/timeline?type=all%2Cphoto'
content = httpGet(timelineUrl)
if content == "":
return []
responseJson = json.loads(content)
try:
errorCode = responseJson['errorCode']
if errorCode != 0:
return []
contentPhotolist = responseJson['data']['photo']
if contentPhotolist.count == 0:
return []
photoUrlList = []
for photoIndex in contentPhotolist:
realUrl = photoIndex['photo']
if realUrl:
photoUrlList.append(realUrl)
return photoUrlList
except Exception, e:
print '-----SO SAD, getUserPhotoUrlList error ----'
print Exception, ':', e
return []
return []
def checkDocuments(path):
if os.path.exists(path) == False:
os.mkdir(path)
def getImageName(imageUrl) :
iamgeName = os.path.basename(imageUrl)
if '.jpg' in iamgeName:
return iamgeName
return 'error.jpg'
def downloadUserPictures(userId, urllist):
path = 'picture'
checkDocuments(path)
path = path + '/' +userId
checkDocuments(path)
for imageUrl in urllist:
content = urllib2.urlopen(imageUrl).read()
with open(path + '/' + getImageName(imageUrl), 'wb') as code:
code.write(content)
print '------ One of you loves has been downloaded to your picture----:) ---'
return
def getWhatYouWant(userIds):
for uid in userIds:
urllist = getUserPhotoUrlList(uid)
downloadUserPictures(uid, urllist)
# protect keep server, pls do not comment this line
# protect keep server, pls do not comment this line
# protect keep server, pls do not comment this line
time.sleep(5)
return
def extractRigthUserIds(entriesIds, genderWanted):
resultUserIds = []
for eId in entriesIds:
rightId = getUserIdWithRightGender(eId, genderWanted)
if rightId != '':
resultUserIds.append(rightId)
return resultUserIds
def start():
allEntriesIds = getEntriesIds()
if allEntriesIds.count == 0:
print "------SO SAD, empty user id -----"
return
genderWanted = "f"
rightUserIds = extractRigthUserIds(allEntriesIds, genderWanted)
if rightUserIds.count == 0:
print "------SO SAD, empty right user id -----"
return
getWhatYouWant(rightUserIds)
if __name__ == '__main__':
print "-------- Let's go ----------------"
start()
getWhatYouWant(['55f0bbaa30ec054d2ae3fa47'])
print "----All End! Enjoy your photos----"