Last active
August 29, 2015 13:56
-
-
Save manvari/8925357 to your computer and use it in GitHub Desktop.
Python script for cssminifier.com's API
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 | |
import requests | |
import argparse | |
import os | |
def css_minifier(input): | |
payload = {"input": input} | |
headers = {'content-type': 'application/x-www-form-urlencoded', | |
'content-length': input.__len__()} | |
cssmin = requests.post("http://cssminifier.com/raw", data=payload, | |
headers=headers) | |
cssmin.raise_for_status() | |
print(cssmin.content) | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(prog="CSS Minifier (cssminifier.com)") | |
parser.add_argument("request", nargs=1, choices=("file", "url"), default="file", | |
help="Get input from a file or an URL.") | |
parser.add_argument("location", help="Location of the input (file or URL).") | |
args = parser.parse_args() | |
if args.request == ['file']: | |
location = open(os.path.expanduser(args.location), 'r') | |
input_content = location.read() | |
css_minifier(input_content) | |
elif args.request == ['url']: | |
input_url = requests.get(args.location) | |
input_content = input_url.content | |
cssmin.raise_for_status() | |
css_minifier(input_content) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
Minify local CSS stylesheet & save it to a file:
Fetch a stylesheet online, minify it & save it to a file: