Created
August 15, 2021 07:04
-
-
Save waterrmalann/7318e61de34341a3c3bf9964e144c547 to your computer and use it in GitHub Desktop.
Simple Python Utility to Minify HTML/CSS/JS!
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
""" | |
A python script to minify the HTML/CSS/JS files of a website. | |
Minifiers by Andrew Chilton (https://twitter.com/andychilton). | |
""" | |
import os, requests | |
def minify_css(css_file, overwrite=False): | |
with open(css_file, 'r') as f: | |
css = f.read() | |
req = requests.post('https://cssminifier.com/raw', {'input': css}) | |
save_path = css_file | |
if not overwrite: | |
save_path = css_file[:-4] + '.min.css' | |
with open(save_path, 'w') as f: | |
f.write(req.text) | |
def minify_js(js_file, overwrite=False): | |
with open(js_file, 'r') as f: | |
js = f.read() | |
req = requests.post('https://javascript-minifier.com/raw', {'input': js}) | |
save_path = js_file | |
if not overwrite: | |
save_path = js_file[:-3] + '.min.js' | |
with open(save_path, 'w') as f: | |
f.write(req.text) | |
def minify_html(html_file, overwrite=False): | |
with open(html_file, 'r') as f: | |
html = f.read() | |
req = requests.post('https://html-minifier.com/raw', {'input': html}) | |
save_path = html_file | |
if not overwrite: | |
save_path = html_file[:-5] + '.min.html' | |
with open(save_path, 'w') as f: | |
f.write(req.text) | |
def main(): | |
print(r" __ __ _ _ __ _ ") | |
print(r" | \/ (_)_ __ (_)/ _(_) ___ _ __ ") | |
print(r" | |\/| | | '_ \| | |_| |/ _ \ '__|") | |
print(r" | | | | | | | | | _| | __/ | ") | |
print(r" |_| |_|_|_| |_|_|_| |_|\___|_| ") | |
print("Simple Python Utility to Minify HTML/CSS/JS!", '\n') | |
OVERWRITE = False | |
PATH = os.getcwd() | |
print("Type the folder path with all the files. Leave blank for current directory.") | |
input_path = input("Path: ").strip() | |
if input_path: | |
PATH = input_path | |
print() | |
print("Would you like to overwrite the files with the minified variants? Type 'yes' or 'no'") | |
input_overwrite = input("Overwrite: ").strip() | |
if input_overwrite in {'yes', 'yep', 'true', 'overwrite'}: | |
OVERWRITE = True | |
print() | |
files_minified = 0 | |
for r, d, f in os.walk(PATH): | |
for file_name in f: | |
file_path = os.path.join(r, file_name) | |
if file_name.endswith('.css'): | |
print(f"Minifying {file_path}...") | |
minify_css(file_path, OVERWRITE) | |
files_minified += 1 | |
elif file_name.endswith('.js'): | |
print(f"Minifying {file_path}...") | |
minify_js(file_path, OVERWRITE) | |
files_minified += 1 | |
elif file_name.endswith('.html'): | |
print(f"Minifying {file_path}...") | |
minify_html(file_path, OVERWRITE) | |
files_minified += 1 | |
print() | |
print(f"Minified {files_minified} files.") | |
input() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment