-
-
Save Polyterative/7b6e608bd4973998bf182dc80e0149c8 to your computer and use it in GitHub Desktop.
Revisions
-
Polyterative revised this gist
May 12, 2021 . 1 changed file with 25 additions and 22 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -5,28 +5,30 @@ import requests from multiprocessing.pool import ThreadPool def build_index(): page = 1 URLs = [] while True: res = requests.get("https://undraw.co/api/illustrations?page={}".format(page)) json_body = res.json() for item in json_body['illos']: title = item['title'] url = item['image'] print("Title: %s => URL: %s" % (title, url)) URLs.append([title, url]) page = json_body['nextPage'] print("Proceeding to Page %d" % page) if not json_body['hasMore']: print("Finished Gathering JSON.") return URLs def download_from_entry(entry): title, url = entry file_name = "%s.svg" % title.lower().replace(' ', '_') @@ -35,23 +37,24 @@ def download_from_entry(entry): if not os.path.exists(file_name): res = requests.get(url, stream=True) if res.status_code == 200: path = "./images/%s" % file_name with open(path, 'wb') as f: for chunk in res: f.write(chunk) return file_name urls = build_index() print("Downloading %d files." % len(urls)) results = ThreadPool(20).imap_unordered(download_from_entry, urls) for path in results: print("Downloaded %s" % path) print("Downloaded %d files." % len(urls)) -
Phoomparin Mano created this gist
Apr 2, 2020 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,57 @@ #!/usr/bin/env python3 import os import json import requests from multiprocessing.pool import ThreadPool def build_index(): page = 1 urls = [] while True: res = requests.get("https://undraw.co/api/illustrations?page={}".format(page)) json_body = res.json() for item in json_body['illustrations']: title = item['title'] url = item['image'] print("Title: %s => URL: %s" % (title, url)) urls.append([title, url]) page = json_body['nextPage'] print("Proceeding to Page %d" % page) if not json_body['hasMore']: print("Finished Gathering JSON.") return urls def download_from_entry(entry): title, url = entry file_name = "%s.svg" % title.lower().replace(' ', '_') print("Downloading %s" % file_name) if not os.path.exists(file_name): res = requests.get(url, stream=True) if res.status_code is 200: path = "./images/%s" % file_name with open(path, 'wb') as f: for chunk in res: f.write(chunk) return file_name urls = build_index() print("Downloading %d files." % len(urls)) results = ThreadPool(20).imap_unordered(download_from_entry, urls) for path in results: print("Downloaded %s" % path) print("Downloaded %d files." % len(urls))