Last active
August 4, 2018 22:55
-
-
Save thurask/524a2e7d74bd1955719f9de0749ee9a3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import os | |
import requests | |
def prep_download(lfname, output_directory=None): | |
if output_directory is None: | |
output_directory = os.getcwd() | |
fname = os.path.join(output_directory, lfname) | |
sess = requests.Session() | |
return fname, sess | |
def download_write(req, lfname, file): | |
print("DOWNLOADING {0}".format(lfname)) | |
for chunk in req.iter_content(chunk_size=1024): | |
file.write(chunk) | |
def download(url, lfname, output_directory=None): | |
fname, sess = prep_download(lfname, output_directory) | |
with open(fname, "wb") as file: | |
req = sess.get(url, stream=True) | |
if req.status_code == 200: # 200 OK | |
download_write(req, lfname, file) | |
else: | |
print("ERROR: HTTP {0} IN {1}".format(req.status_code, lfname)) | |
def prep_filename(jsonentry): | |
return "{0}_{1}.apk".format(jsonentry["apkPackName"], jsonentry["versionName"]) | |
def load_json(): | |
with open("apps.json") as afile: | |
data = json.load(afile) | |
return data | |
def generate_json(): | |
if not os.path.exists("apps.json"): | |
jarray = {} | |
else: | |
with open("apps.json") as afile: | |
jarray = json.load(afile) | |
for x in range(3700, 4100): | |
appurl = "https://aota.tclclouds.com/api/app/appDetail/{0}/en".format(str(x)) | |
if str(x) not in jarray: | |
print("NOW SCANNING ID: {0}".format(str(x)), end="\r") | |
req = requests.get(appurl) | |
if req.content: | |
rjson = req.json() | |
print("ID: {0} PACKAGE: {1} VERSION: {2} URL: {3}".format(rjson["id"], rjson["apkPackName"], rjson["versionName"], rjson["appUrl"])) | |
jarray[rjson["id"]] = {"apkPackName": rjson["apkPackName"], "versionName": rjson["versionName"], "appUrl": rjson["appUrl"]} | |
with open("apps.json", "w") as afile: | |
json.dump(jarray, afile) | |
def main(): | |
generate_json() | |
data = load_json() | |
if not os.path.exists("tclapks"): | |
os.makedirs("tclapks") | |
for entry in data.keys(): | |
item = data[entry] | |
fname = prep_filename(item) | |
if fname not in os.listdir("tclapks"): | |
url = item["appUrl"].replace("https://", "http://") | |
download(url, fname, "tclapks") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment