Created
August 22, 2019 17:50
-
-
Save bssanchez/ec37c5ab16dfb215d2ef9655fb56a297 to your computer and use it in GitHub Desktop.
simple GoogleFont downloader in python
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
| #!/usr/bin/env python | |
| # -*- coding: utf-8 -*- | |
| from os.path import basename | |
| from urllib import request | |
| import sys, re, os | |
| header = """ | |
| ################################################################################ | |
| # ___ ___ _ ___ # | |
| # / _ \ / __\__ _ __ | |_ / \_____ ___ __ # | |
| # / /_\// _\/ _ \| '_ \| __| / /\ / _ \ \ /\ / / '_ \ # | |
| # / /_\\/ / | (_) | | | | |_ / /_// (_) \ V V /| | | | # | |
| # \____/\/ \___/|_| |_|\__/___,' \___/ \_/\_/ |_| |_| # | |
| # # | |
| ################################################################################ | |
| by kid_goth | |
| =========== | |
| """ | |
| print(header) | |
| if len(sys.argv) < 2: | |
| print('Invalid arguments, use e.g.:\r\n\r\n' + sys.argv[0] + ' https://fonts.googleapis.com/css\?family\=Livvic\|Roboto') | |
| exit() | |
| try: | |
| request.urlretrieve(sys.argv[1], './fonts.css') | |
| file = open('./fonts.css', 'r') | |
| css_content = file.read() | |
| file.close() | |
| font_faces = re.findall("\@font-face[\s\S]*?(?=\})", css_content) | |
| for font_def in font_faces: | |
| name = re.findall("font-family:\ ?\'([a-zA-Z\ ]+)\'", font_def) | |
| font_urls = re.findall("url\(?([a-zA-Z0-9\.\:\/\-\_]+)(?=\))", font_def) | |
| dir_font = './fonts/' + str.lower(name[0]) | |
| os.makedirs(dir_font, exist_ok=True) | |
| for font_url in font_urls: | |
| font_name = basename(font_url) | |
| request.urlretrieve(font_url, dir_font + '/' + font_name) | |
| css_content = css_content.replace(font_url, dir_font + '/' + font_name) | |
| file = open('./fonts.css', 'w') | |
| file.write(css_content) | |
| file.close() | |
| except Exception as e: | |
| print('An error has ocurred: ', e) | |
| exit() | |
| print("Finished, enjoy :*") | |
| #EOF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment