Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add icon per folder #37

Open
sdfjsfjaei-hans opened this issue Apr 22, 2021 · 0 comments
Open

Add icon per folder #37

sdfjsfjaei-hans opened this issue Apr 22, 2021 · 0 comments

Comments

@sdfjsfjaei-hans
Copy link

Here's the script I used (all but your parts are public domain). It does more than just save images though. It creates a sanatized version of orders.json (which is a dict of order_id to order):

import json
from urllib.parse import urlparse
from urllib.request import urlretrieve
import os.path


j = json.load(open("orders.json"))
library_path = ""


def _clean_name(dirty_str):
    allowed_chars = (' ', '_', '.', '-', '[', ']')
    clean = []
    for c in dirty_str.replace('+', '_').replace(':', ' -'):
        if c.isalpha() or c.isdigit() or c in allowed_chars:
            clean.append(c)

    return "".join(clean).strip().rstrip('.')


ids_to_delete = []

for id, values in j.items():
    #values.pop("amount_spent", None)
    values.pop('gamekey', None)
    values.pop('uid', None)
    values.pop('all_coupon_data', None)
    values.pop('tpkd_dict', None)
    values.pop('path_ids', None)
    
    bundle_title = _clean_name(values['product']['human_name'])
    
    subs_to_delete = []
    
    subproducts = values['subproducts']
    for sub in subproducts:
        product_title = _clean_name(sub['human_name'])
        product_folder = os.path.join(library_path, bundle_title, product_title)
        
        if os.path.exists(product_folder):
            sub['path'] = product_folder
            
            icon_url = sub['icon']
            
            if icon_url is not None:
                ext = urlparse(icon_url).path.rsplit('.')[-1]
                icon_path = os.path.join(product_folder, 'folder.' + ext)
                if not os.path.exists(target_path):
                    urlretrieve(url = icon_url, filename=icon_path)
        
        sub.pop('icon', None)
        
        dl_counts = 0
        for dl in sub['downloads']:
            to_remove = []
            dl_struct = dl['download_struct']
            for dls in dl_struct:
                if 'url' not in dls:
                    if 'filename' not in dls:
                        to_remove.append(dls)
                    continue
                    
                p = urlparse(dls['url']['web'])
                fname = p.path.rsplit("/", maxsplit=1)[-1]
                dls['filename'] = fname
                
                del dls['url']
            for t in to_remove:
                dl_struct.remove(t)
            
            if dl_struct:
                dl_counts += 1
        
        if not dl_counts:
            subs_to_delete.append(sub)
    
    for sub in subs_to_delete:
        subproducts.remove(sub)
    
    if not subproducts:
        ids_to_delete.append(id)

for id in ids_to_delete:
    del j[id]

json.dump(j, open("all-orders-sanatized.json", "w+"), indent=4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant