Skip to content

Instantly share code, notes, and snippets.

@manvari
Last active August 29, 2015 13:56
Show Gist options
  • Save manvari/8925357 to your computer and use it in GitHub Desktop.
Save manvari/8925357 to your computer and use it in GitHub Desktop.
Python script for cssminifier.com's API
#!/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)
@manvari
Copy link
Author

manvari commented Feb 10, 2014

Usage:

Minify local CSS stylesheet & save it to a file:

$ ./cssminifier.py file /path/to/stylesheet.css > stylesheet.min.css

Fetch a stylesheet online, minify it & save it to a file:

$ ./cssminifier.py url http://example.com/stylesheet.css > stylesheet.min.css

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment