# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python [conda env:dsc180]
#     language: python
#     name: conda-env-dsc180-py
# ---

# +
import urllib
import requests
import os
import subprocess
import shutil

# # !pip install beautifulsoup4
from bs4 import BeautifulSoup


# -

def get_app(app_url):
    app_url += '/download?from=details'
    download_page = requests.get(app_url)
    soup = BeautifulSoup(download_page.content)
    apk_url = soup.select('#iframe_download')[0].attrs['src']
    return requests.get(apk_url).content


# +
# k = get_app('https://apkpure.com/wallpaper-for-iphone-x-8-8/com.wallpapersforiphoneX.themeapplock88plus')
# -

def get_data(**config):
    data_dir = config['data_dir']
    if not os.path.exists(data_dir):
        os.mkdir(data_dir)
        
    apps_dir = os.path.join(data_dir, 'apps')
    if not os.path.exists(apps_dir):
        os.mkdir(apps_dir)
    
    for url in config['urls']:
        app_slug, app_dev = url.split('/')[-2:]
        app_slug = urllib.parse.unquote(app_slug)  # include or not
        dev_dir = os.path.join(apps_dir, app_dev)
        if not os.path.exists(dev_dir):
            os.mkdir(dev_dir)
        
        apk = get_app(url)
        
        apk_fn = os.path.join(dev_dir, app_slug + '.apk')
        apk_fp = open(apk_fn, 'wb')
        apk_fp.write(apk)
        print(apk_fn)
        
        code_dir = os.path.join(dev_dir, app_slug)
        if os.path.exists(code_dir):
            shutil.rmtree(code_dir)
            
        print('Decompiling apk...')
        command = subprocess.run([
            'apktool', 'd',  # decode
#             '-r',            # no resources, but it seems to corrupt the AndroidManifest.xml file, idk why
            apk_fn,          # apk filename
            '-o', code_dir   # decompiled out path
        ], capture_output=True)
        print(command.stdout.decode())


get_data(**cfg)





cfg = {
    'data_dir': '../data',
    'urls': [
        'https://apkpure.com/wallpaper-for-iphone-x-8-8/com.wallpapersforiphoneX.themeapplock88plus',
        'https://apkpure.com/instagram/com.instagram.android'
    ]
}



StarcoderdataJupyterScriptsDedupFiltered