This repository has been archived by the owner on Nov 29, 2022. It is now read-only.
forked from bluemutedwisdom/google-play-downloader
-
Notifications
You must be signed in to change notification settings - Fork 8
/
server.py
72 lines (58 loc) · 2.02 KB
/
server.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
import sys
import json
import re
import traceback
from compat import HTTPServer, BaseHTTPRequestHandler
from constants import defaults
from Market import Market
from OperatorModel import Operator
from AssetRequest import AssetRequest
from Util import Util
config = dict(defaults)
class ApkServerHandler(BaseHTTPRequestHandler):
def __init__(self, *args, **kwargs):
self.handlers = {
'/apps/(.*).apk': self.apps
}
# __init__ ultimately calls do_GET
BaseHTTPRequestHandler.__init__(self, *args, **kwargs)
def apps(self, appid):
request = AssetRequest(appid, market.token, config['device'], operator, config['devname'], int(config['sdklevel']))
try:
url = market.get_asset(request.encode())
except:
self.send_response(403)
self.end_headers()
self.wfile.write(traceback.format_exc().encode('utf-8'))
return
generator, size = Util.download_apk_stream(appid, url)
self.send_response(200)
self.send_header('Content-Length', size)
self.end_headers()
for data in generator:
self.wfile.write(data)
def do_GET(self):
for pattern, handler in self.handlers.items():
matches = re.match(pattern, self.path)
if matches:
handler(*matches.groups())
return
# No handler found
self.send_response(404)
self.end_headers()
if __name__ == "__main__":
try:
with open('config.json', 'rb') as f:
config.update(json.loads(f.read().decode('utf-8')))
except:
print('Unable to load config file')
sys.exit(1)
market = Market(config['email'], config['password'])
market.login()
operator = Operator(config['country'], config['operator'])
try:
server = HTTPServer(('', 5000), ApkServerHandler)
server.serve_forever()
except KeyboardInterrupt:
print('^C received, shutting down the web server')
server.socket.close()