forked from simyy/googleplay-crawler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crawlAPI.py
137 lines (126 loc) · 4.38 KB
/
crawlAPI.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
#!/usr/bin/env python
# coding=utf-8
import os
import time
import random
from googleplay_api import config
from googleplay_api.googleplay import GooglePlayAPI
from googleplay_api.helpers import sizeof_fmt, print_header_line, print_result_line
class CrawlAPI:
'''
craw interface
'''
def __init__(self):
self._android_id = config.ANDROID_ID
self._user = config.GOOGLE_LOGIN
self._passwd = config.GOOGLE_PASSWORD
self._auth_token = config.AUTH_TOKEN
self._savePath = config.SAVE_PATH
self._api = self._login()
def _configTest(self):
if any([each == None for each in [self._android_id, self._user, self._passwd]]):
raise Exception('config item for login is not correct...')
if self._savePath[-1] != '/':
self._savePath += '/'
def _login(self):
'''
user android_id, user name and password or AUTH_TOKEN to login
'''
self._configTest()
api = GooglePlayAPI(self._android_id)
api.login(self._user, self._passwd, self._auth_token)
return api
def getCategories(self):
'''
get categories from googleplay
@return: {'category_name','url_params'}
'''
res = self._api.browse()
if res == False:
return False
category_dict = {}
for c in res.category:
category_dict[c.name] = c.dataUrl[c.dataUrl.rfind('=')+1:]
return category_dict
def getApps(self, category, subCategory, nb_result=20, offset=20):
'''
get apps from a subcategory in categories
'''
apps = {}
res = self._api.list(category, subCategory, str(offset), 0)
doc = res.doc[0]
nextPageUrl = doc.containerMetadata.nextPageUrl
#print nextPageUrl
for c in doc.child:
#print c.title, c.details.appDetails.packageName
apps[c.details.appDetails.packageName] = c.title
stop = False
i = 0
i += int(offset)
time.sleep(random.randint(1, 5))
print 'wait...have get: %s'%i
while (not stop):
res = self._api.getNextPage(nextPageUrl)
if res == False:
print 'no more app'
break
doc = res.doc[0]
nextPageUrl = doc.containerMetadata.nextPageUrl
#print nextPageUrl
for c in doc.child:
#print c.title, c.details.appDetails.packageName
apps[c.details.appDetails.packageName] = c.title
i += int(offset)
time.sleep(random.randint(1, 5))
print 'wait...'
if i > nb_result:
stop = True
return apps
def search(self, name, offset=None, num=None):
'''
search app
@name: app name
@offset: start position
@num: app num
@return: search result
'''
try:
message = self._api.search(name, num, offset)
except Exception as e:
print e
return False
if message == False:
return False
return message
def getDetails(self, packageName):
message = self._api.details(packageName)
#print message
if message == False:
return '', '', ''
doc = message.docV2
#versionString = doc.details.appDetails.versionString
version = doc.details.appDetails.versionCode
offerType = doc.offer[0].offerType
size = sizeof_fmt(doc.details.appDetails.installationSize)
return version, offerType, size
def download(self, packageName):
vc, ot, sz = self.getDetails(packageName)
if vc == '' or ot == '':
print 'can\'t get details info ...'
return False
saveName = packageName + '_' + str(vc)
if saveName not in os.listdir(self._savePath):
print 'start download ... size: %s'%sz
data = self._api.download(packageName, vc, ot)
open(self._savePath + saveName, 'wb').write(data)
print 'complated ...'
else:
print 'replicate app ... next one ...'
if __name__ == '__main__':
app = CrawlAPI()
#res =app.search('angrybird', '1', '1')
#res = app.getCategories()
#print res
#app.getApps('GAME', 'apps_topselling_free', '300', '20')
#app.getDetails('com.facebook.katana')
app.download('com.facebook.katana')