-
-
Save PierceLBrooks/7a29d53f4915f847b82e41e2954ca000 to your computer and use it in GitHub Desktop.
Javascript Beautifier with Python
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
import sys | |
import logging | |
import traceback | |
try: | |
import jsbeautifier | |
import requests | |
import requests_file | |
except Exception as e: | |
sys.exit(print("{0}.. please download this module/s".format(e))) | |
def beauty(content:str)->str: | |
return jsbeautifier.beautify(content.decode()) | |
def getjs(url:str)->dict: | |
try: | |
session = requests.Session() | |
session.mount("file://", requests_file.FileAdapter()) | |
r = session.get(url) | |
if r.status_code == 200: | |
return {"content":r.content,"status_code":r.status_code} | |
except Exception as e: | |
logging.error(traceback.format_exc()) | |
return {"content":"","status_code":500} | |
def main()->None: | |
try: | |
url = sys.argv[1] | |
output = sys.argv[2] | |
except: | |
sys.exit(print("\nUsage:\tpython3 {0} <url> <output>\n".format(sys.argv[0]))) | |
r = getjs(url) | |
if r["status_code"] == 200: | |
js = beauty(r["content"]) | |
if output: | |
_file = open(sys.argv[2],"w") | |
_file.write(js) | |
_file.close() | |
print("Done! file saved here -> \"{0}\"".format(_file.name)) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment