Skip to content

Instantly share code, notes, and snippets.

@chocolatkey
Created February 20, 2017 11:22
Show Gist options
  • Save chocolatkey/f73fbf17b9777a32b1894309c7add54d to your computer and use it in GitHub Desktop.
Save chocolatkey/f73fbf17b9777a32b1894309c7add54d to your computer and use it in GitHub Desktop.
Proof of concept
# Icons8 fetcher
import json
import urllib.request
import os
import sys
import argparse
import re
apihome = "https://api.icons8.com/api/iconsets/v3/search"
def main():
samount = 100 # Default amount of icons to return
namount = 1 # Default max number of icons to download
DESCRIPTION = '''Fetch the SVGs for icons from Icons8. Files will output to the current directory. Please don't exploit usage of this program :('''
QUERY_HELP = '''Search term'''
ID_HELP = '''Icon ID to filter'''
MAX_HELP = '''Max amount of icons to download. Default: ''' + str(namount)
PACK_HELP = '''Icon pack to filter. Options: win10, win8, ios7, androidL, android, color'''
AMOUNT_HELP = '''Limit number of icons in search. Default: ''' + str(samount)
parser = argparse.ArgumentParser(description = DESCRIPTION)
parser.add_argument('-q','--query', dest = 'query', help = QUERY_HELP, required=True)
parser.add_argument('-p','--pack', dest = 'pack', help = PACK_HELP)
parser.add_argument('-a','--amount', dest = 'amount', help = AMOUNT_HELP)
parser.add_argument('-m','--max', dest = 'max', help = MAX_HELP)
parser.add_argument('-i','--id', dest = 'id', help = ID_HELP)
options = parser.parse_args()
if options.amount is not None:
samount = options.amount
if options.max is not None:
namount = options.max
request = urllib.request.urlopen(apihome + "?term={}&amount={}&offset=0&language=en-US".format(options.query, samount)).read().decode('utf-8')
response = json.loads(request)
iterator = 1
for icon in response["result"]["search"]:
if options.pack is not None:
if icon["platform_code"] != options.pack:
continue
if options.id is not None:
if icon["id"] != options.id:
continue
svg_file = open("{}_{}.svg".format(icon["name"],icon["id"]), "w")
svg_file.write(icon["svg"].replace('\n', ''))
svg_file.close()
iterator += 1
if iterator >= namount:
break
if iterator == 0:
print("Nothing found")
if __name__ == '__main__':
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment