Last active
October 9, 2018 17:33
-
-
Save gsw945/1d4f10585ba9640c19a9f009f47b03af to your computer and use it in GitHub Desktop.
cdnjs downloader
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
| # -*- coding: utf-8 -*- | |
| import json | |
| import requests | |
| from prompt_toolkit.completion import WordCompleter | |
| from prompt_toolkit import prompt | |
| from prompt_toolkit.formatted_text import ANSI | |
| from prompt_toolkit import print_formatted_text | |
| kw = input('library keyword: ') | |
| resp = requests.get('https://api.cdnjs.com/libraries?search={0}'.format(kw)) | |
| if resp.ok: | |
| data = json.loads(resp.text) | |
| if isinstance(data, dict) and 'results' in data: | |
| results = data['results'] | |
| results = sorted(list(map(lambda item: item['name'], results)), reverse=False) | |
| completer = WordCompleter(results, ignore_case=True, match_middle=True) | |
| selected = prompt('choose library: ', completer=completer, complete_while_typing=True) | |
| if selected in results: | |
| print('your choice is:', end=' ') | |
| print_formatted_text(ANSI('\x1b[91m{0}'.format(selected))) | |
| else: | |
| print('canceled') |
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
| # -*- coding: utf-8 -*- | |
| import json | |
| import requests | |
| from prompt_toolkit.application import Application | |
| from prompt_toolkit.layout import Layout | |
| from prompt_toolkit.key_binding.defaults import load_key_bindings | |
| from prompt_toolkit.key_binding.key_bindings import KeyBindings, merge_key_bindings | |
| from prompt_toolkit.formatted_text import ANSI | |
| from prompt_toolkit import print_formatted_text | |
| # tip: [new_radio_list.py] file is in gist(https://gist.github.com/gsw945/3f41738e9641a330c64bf2f5a9a97fdf) | |
| from new_radio_list import NewRadioList | |
| library = 'jqueryui' | |
| resp = requests.get('https://api.cdnjs.com/libraries/{0}?fields=assets'.format(library)) | |
| if resp.ok: | |
| data = json.loads(resp.text) | |
| if isinstance(data, dict) and 'assets' in data: | |
| assets = data['assets'] | |
| versions = list(map(lambda item: item['version'], assets)) | |
| print(versions) | |
| values = list(map(lambda item: (item, item), versions)) | |
| rdo = NewRadioList(values) | |
| def do_exit(event): | |
| # get_app().exit() | |
| event.app.exit(result=rdo.current_value) | |
| def do_up_down(event): | |
| print(event) | |
| pass | |
| bindings = KeyBindings() | |
| bindings.add('enter')(do_exit) | |
| app_bindings = merge_key_bindings([ | |
| load_key_bindings(), | |
| bindings | |
| ]) | |
| selected = Application(layout=Layout(rdo), key_bindings=app_bindings).run() | |
| print('your choice is:', end=' ') | |
| # refer: https://github.com/jonathanslenders/python-prompt-toolkit/blob/master/examples/print-text/ansi.py | |
| print_formatted_text(ANSI('\x1b[91m{0}'.format(selected))) |
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
| # -*- coding: utf-8 -*- | |
| import os | |
| import json | |
| import requests | |
| # root_folder = os.path.dirname(__file__) | |
| root_folder = os.getcwd() | |
| library = 'jqueryui' | |
| resp = requests.get('https://api.cdnjs.com/libraries/{0}?fields=assets'.format(library)) | |
| if resp.ok: | |
| data = json.loads(resp.text) | |
| if isinstance(data, dict) and 'assets' in data: | |
| assets = data['assets'] | |
| version = '1.12.1' | |
| selected = list(filter(lambda item: item['version'] == version, assets)) | |
| files = selected[0]['files'] | |
| # begin to download | |
| folder = os.path.join(root_folder, library, version) | |
| # if not os.path.exists(folder) or not os.path.isdir(folder): | |
| # os.makedirs(folder) | |
| print('store to [{0}]'.format(folder)) | |
| base_url = 'https://cdnjs.cloudflare.com/ajax/libs/{0}/{1}/'.format(library, version) | |
| list_len = len(files) | |
| for idx, item in enumerate(files): | |
| url = base_url + item | |
| # from: http://docs.python-requests.org/zh_CN/latest/user/quickstart.html#id4 | |
| resp = requests.get(url) | |
| if resp.ok: | |
| item_path = item.replace('/', os.sep) | |
| full_path = os.path.join(folder, item_path) | |
| file_folder = os.path.dirname(full_path) | |
| if not os.path.exists(file_folder) or not os.path.isdir(file_folder): | |
| os.makedirs(file_folder) | |
| with open(full_path, 'wb') as fw: | |
| fw.write(resp.content) | |
| print('{0:2}/{1:2}'.format(idx + 1, list_len), end=' -> ') | |
| print(item) | |
| else: | |
| print(resp.status_code, end=' -> ') | |
| print(url) |
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
| # -*- coding: utf-8 -*- | |
| import os | |
| import time | |
| from multiprocessing.dummy import Pool as ThreadPool | |
| from multiprocessing import Manager, freeze_support | |
| import requests | |
| def do_request(args): | |
| root_folder = os.getcwd() | |
| (library, version, url, share, data_len) = args | |
| folder = os.path.join(root_folder, library, version) | |
| base_url = 'https://cdnjs.cloudflare.com/ajax/libs/{0}/{1}/'.format(library, version) | |
| item_url = base_url + url | |
| # from: http://docs.python-requests.org/zh_CN/latest/user/quickstart.html#id4 | |
| resp = requests.get(item_url) | |
| if resp.ok: | |
| item_path = url.replace('/', os.sep) | |
| full_path = os.path.join(folder, item_path) | |
| file_folder = os.path.dirname(full_path) | |
| if not os.path.exists(file_folder) or not os.path.isdir(file_folder): | |
| os.makedirs(file_folder) | |
| with open(full_path, 'wb') as fw: | |
| fw.write(resp.content) | |
| share[url] = True | |
| else: | |
| print(resp.status_code, end=' -> ') | |
| print(item_url) | |
| share[url] = False | |
| time.sleep(0.2) | |
| # print(share) | |
| print('{0:2}/{1:2}'.format(len(share), data_len), end=' -> ') | |
| print(url) | |
| if __name__ == '__main__': | |
| freeze_support() | |
| manager = Manager() | |
| share = manager.dict() | |
| N = 3 | |
| tpool = ThreadPool(N) | |
| library = 'iframe-resizer' | |
| version = '3.6.2' | |
| data = [ | |
| 'ie8.polyfils.map', | |
| 'ie8.polyfils.min.js', | |
| 'iframeResizer.contentWindow.js', | |
| 'iframeResizer.contentWindow.map', | |
| 'iframeResizer.contentWindow.min.js', | |
| 'iframeResizer.js', | |
| 'iframeResizer.map', | |
| 'iframeResizer.min.js' | |
| ] | |
| data_len = len(data) | |
| data = list(map(lambda item: (library, version, item, share, data_len), data)) | |
| tresult_proxy = tpool.map_async(do_request, data) | |
| tpool.close() | |
| tpool.join() | |
| # result = tresult_proxy.get() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment