From dce0da91b25420a9c63067b9efda91b9a59f2fc2 Mon Sep 17 00:00:00 2001 From: Ceda EI Date: Thu, 5 Nov 2020 19:42:31 +0530 Subject: [PATCH] Add create_webapp --- create_app.py | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/create_app.py b/create_app.py index b6f6170..76395fe 100755 --- a/create_app.py +++ b/create_app.py @@ -2,16 +2,21 @@ "Creates a firefox web app" import argparse +import io +import shlex +import stat import sys +import os import os.path as pt from urllib.parse import urlparse, urlunparse from collections import Counter import requests from bs4 import BeautifulSoup +from PIL import Image -REPO_DIR = pt.dirname(sys.argv[0]) +REPO_DIR = pt.abspath(pt.dirname(sys.argv[0])) BIN_DIR = f"{REPO_DIR}/bin" ICON_DIR = f"{REPO_DIR}/icons" @@ -37,6 +42,40 @@ def absolute_url(base_url, relative_url): return relative_url +# pylint: disable=W0613 +def create_webapp(name, url, exec_name, logo, profile): + "Creates the necessary files for the webapp." + local_vars = locals() + # Create a dictionary with shell-quoted version of arguments + quoted = {i: shlex.quote(local_vars[i]) for i in ("name", "url", "exec_name", + "logo", "profile")} + # Download and convert the logo + logo_pt = f"{ICON_DIR}/{exec_name}.png" + res = requests.get(logo, allow_redirects=True) + with Image.open(io.BytesIO(res.content)) as img: + img.save(logo_pt, "PNG") + + # Create the binary + script_pt = f"{BIN_DIR}/{exec_name}" + with open(script_pt, "w") as script: + script.write("#!/usr/bin/env sh\n") + script.write(f"firefox --profile {quoted['profile']} {quoted['url']}\n") + os.chmod(script_pt, os.stat(script_pt).st_mode | stat.S_IXUSR | stat.S_IXGRP) + + # Create the desktop file + desk_pt = pt.expanduser(f"~/.local/share/applications/{exec_name}.desktop") + with open(desk_pt, "w") as desktop: + desktop.write(f""" +[Desktop Entry] +Name={name} (Web App) +Exec={script_pt} +Terminal=false +Type=Application +Icon={logo_pt} +Categories=Network;X-WebApps + """.strip() + "\n") + + def extract_metadata(url): "Extract metadata using bs4" # Get and parse the page @@ -154,6 +193,13 @@ def main(): print(f"Logo URL:\t\t{args.logo}") print(f"Executable Name:\t{args.exec_name}") print(f"Firefox Profile:\t{args.firefox_profile}") + print() + print("Do you want to create the app with the above details (Y/n): ", + end=' ') + inp = input() + if not inp or inp[0].upper() != "N": + create_webapp(args.name, args.url, args.exec_name, args.logo, + args.firefox_profile) if __name__ == "__main__":