Created
August 28, 2017 13:35
-
-
Save Phocacius/5319c3c300e82d05549f8b871639026c to your computer and use it in GitHub Desktop.
Python script to download Google material icons and add them to an Android project automatically in several densities.
This file contains 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
#! /usr/bin/env python | |
# Simple command script to download and add Google Material | |
# Design Icons ( https://material.io/icons/ ) to | |
# to your Android project. | |
import sys | |
import os | |
# Constants | |
FILE_PATTERN = 'ic_{}_{}_{}' | |
BASE_URL_PATTERN = "https://storage.googleapis.com/material-icons/external-assets/v4/icons/zip/" | |
DEFAULT_COLOR = 'white' | |
DEFAULT_SIZE = '24dp' | |
DEFAULT_RES_PATH = './app/src/main/res' | |
def generate_url (name, color, size): | |
return BASE_URL_PATTERN + FILE_PATTERN.format(name, color, size) + '.zip' | |
args = sys.argv[1:] | |
# Show usage instructions | |
if (len(args) == 0): | |
print 'Command line tool to download Google Material Icons ( https://material.io/icons/ )' | |
print '' | |
print 'Usage (in project root):' | |
print '$ chmod +x download_icon.py' | |
print '$ ./download_icon.py icon_name [color=white] [size=24dp] [resources_path=./app/src/main/res]' | |
print '' | |
print 'Examples :' | |
print '$ ./download_icon.py copyright' | |
print '$ ./download_icon.py content_copy black 48dp' | |
sys.exit() | |
# Get the arguments | |
name = args[0] | |
color = args[1] if len(args) > 1 else DEFAULT_COLOR | |
size = args[2] if len(args) > 2 else DEFAULT_SIZE | |
res_path = args[3] if len(args) > 3 else DEFAULT_RES_PATH | |
# More constants | |
SUPPORTED_SIZES = [ '18', '24', '36', '48' ] | |
SUPPORTED_SIZES_DP = [ s + 'dp' for s in SUPPORTED_SIZES ] | |
SUPPORTED_COLORS = [ 'white', 'black' ] | |
CURL_COMMAND = 'curl -s {} -o /tmp/gmid/{}.zip' | |
UNZIP_COMMAND = 'unzip -q -o /tmp/gmid/{}.zip -d /tmp/gmid/' | |
MV_COMMAND = 'mv /tmp/gmid/{}/android/{}/{}.png {}/{}/{}.png' | |
# Download and unzip the icons file to /tmp/gmid | |
url = generate_url(name, color, size) | |
print 'Fetching icons from:', url, "\n" | |
fname = FILE_PATTERN.format(name, color, size) | |
os.system('mkdir -p /tmp/gmid') | |
os.system(CURL_COMMAND.format(url, fname)) | |
os.system(UNZIP_COMMAND.format(fname)) | |
# Get the size-based directory names | |
(_, dirnames, _) = os.walk('/tmp/gmid/' + fname + '/android/').next() | |
def make_mv_command(dirname, fname): | |
return MV_COMMAND.format(fname, dirname, fname, res_path, dirname, fname) | |
def make_mkdir_command(dirname): | |
return "mkdir -p {}/{}".format(res_path, dirname) | |
# Move the icons to the right location | |
for dir in dirnames: | |
os.system(make_mkdir_command(dir)) | |
os.system(make_mv_command(dir, fname)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment