Jump to content

DL'ing From API


Recommended Posts

anyone know which commands I can use in autoit to download specific files from an api without having to download the entire directory?

such as downloading from the THUMBNAILS section under each category; like this (python)

import os
import requests

PRODUCT_URL = 'www.WEBSITE.com'
SAVE_DIR = 'THUMBNAILS'

def load_products():
    return requests.get(PRODUCT_URL).json()

def get_objects(products):
    for product in products:
        if 'THUMBNAILS' in product and 'url' in product['THUMBNAILS']:
            yield {
                'url': product['THUMBNAILS']['url'],
                'name': product['sku'],
            }

def download_objects(objects):
    try:
        from tqdm import tqdm
    except ImportError:
        tqdm = None
    if tqdm:
        for obj in tqdm(objects):
            download_object(obj)
    else:
        for obj in objects:
            print('Downloading model for {name}'.format(name=obj['name']))
            download_object(obj)

def download_object(obj):
    file_name = os.path.join(SAVE_DIR, '{name}.obj'.format(name=obj['name']))
    if os.path.isfile(file_name):
        print('ERROR file already Exists')
        return
    r = requests.get(obj['url'], stream=True)
    with open(file_name, 'wb') as f:
        for block in r.iter_content(1024):
            f.write(block)

if __name__ == '__main__':
    try:
        os.mkdir(SAVE_DIR)
    except OSError:
        pass

    products = load_products()
    objects = get_objects(products)
    download_objects(objects)

 

Edited by HankHell
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...