Skip to content

Instantly share code, notes, and snippets.

@undirectlookable
Created February 29, 2016 03:39
Show Gist options
  • Save undirectlookable/3d724fa53069981ceb23 to your computer and use it in GitHub Desktop.
Save undirectlookable/3d724fa53069981ceb23 to your computer and use it in GitHub Desktop.
Download apk file from APKPure.com
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
__version__ = '1.0.0'
import re
import os
import sys
import argparse
import urllib
import urllib2
import urlparse
def download_file(url, directory, file_name):
# create folder if not exists
if not os.path.isdir(directory):
os.mkdir(directory)
# download code from http://stackoverflow.com/a/22776
response = urllib2.urlopen(url)
file = open(directory + '/' + file_name, 'wb')
meta = response.info()
file_size = int(meta.getheaders("Content-Length")[0])
print "Downloading: %s (%s Bytes)" % (file_name, file_size)
file_size_dl = 0
block_sz = 8192
while True:
buffer = response.read(block_sz)
if not buffer:
break
file_size_dl += len(buffer)
file.write(buffer)
status = r"%10d [%3.2f%%]" % (file_size_dl, file_size_dl * 100. / file_size)
status = status + chr(8)*(len(status)+1)
print status,
file.close()
def fetch_html(url, headers={}):
req = urllib2.Request(url, None, headers)
response = urllib2.urlopen(req)
html = response.read()
return html
def main():
parser = argparse.ArgumentParser()
parser.add_argument('app_id', help='the app id from google play')
parser.add_argument('-d', '--dir', help='dir to store download files')
parser.add_argument('-n', '--name', help='rename the apk file')
parser.add_argument('-v', '--version', action='version', version='%(prog)s ' + __version__)
args = parser.parse_args()
# step 1: get app page html
url = 'https://m.apkpure.com/store/apps/details?id=' + args.app_id
html = fetch_html(url)
# step 2: find download link, get file name
download_link = re.search('https:\/\/download\.apkpure\.com[^"]*', html).group(0)
query = urlparse.urlparse(download_link).query
file_name = urlparse.parse_qs(query)['fn'][0]
# step 3: prepare optional arguments
directory = args.dir if args.dir else './apks'
file_name = args.name if args.name else file_name
# step 4: download it
print 'Download link Found, prepare to download...'
download_file(download_link, directory, file_name)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment