-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
108 lines (95 loc) · 3.99 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import os
import ssl
import json
import logging
import platform
import favicon
from urllib.request import urlopen
from bs4 import BeautifulSoup
from PIL import Image
logging.basicConfig(level=logging.ERROR)
ssl._create_default_https_context = ssl._create_unverified_context
def main(url):
validate = False
url = url.strip()
try:
urlopen(url)
validate = True
except:
validate = False
if validate:
with open('package.json', 'r') as file:
data = json.load(file)
data['url'] = url
with open('package.json', 'w') as file:
json.dump(data, file, indent=4)
try:
soup = BeautifulSoup(urlopen(url), features="html.parser")
page = urlopen(url)
soup = BeautifulSoup(page, features="html.parser")
icon_link = soup.find("link", rel="shortcut icon")
name = input("Would you like to automatically set the name of the app to the website's title? (y/n): ")
if name.lower() == "y":
name = soup.find("title").text
else:
name = input("Enter the name of the app: ")
if name.strip() == "":
name = "WebApp"
raise ValueError("Please enter a valid name!")
elif name.isalpha() == False:
name = "WebApp"
raise ValueError("Please enter a valid name!")
elif name.isdigit() == True:
name = "WebApp"
raise ValueError("Please enter a valid name!")
else:
pass
packagejson = "package.json"
data = {}
with open(packagejson, "r") as f:
data = json.load(f)
name = "".join(e for e in name if e.isalnum() or e.isspace()).replace(" ", "-")
data["name"] = name
with open(packagejson, "w") as f:
json.dump(data, f, indent=4)
if icon_link:
icon_url = icon_link['href']
else:
icon_url = "https://static-00.iconduck.com/assets.00/electron-icon-472x512-8swdbwbh.png"
print("Failed to retrieve icon. Trying method 2, if this fails, the default icon will be used.")
icons = favicon.get(url)
icon_url = icons[0].url
if urlopen(icon_url).status != 200:
icon_url = "https://static-00.iconduck.com/assets.00/electron-icon-472x512-8swdbwbh.png"
print("Failed to retrieve icon. Default icon will be used.")
else:
print("Icon retrieved successfully.")
try:
icon = urlopen(icon_url)
with open("icon.ico", "wb") as f:
f.write(icon.read())
img = Image.open("icon.ico")
img.save("icon.ico", "ICO")
img.save("icon.png", "PNG")
img = Image.open("icon.ico")
img.resize((256, 256)).save("icon.ico")
imgu = Image.open("icon.png")
imgu.resize((512, 512)).save("icon.png")
except Exception as e:
print(e)
except Exception as e:
print(e)
print("\n\nThe desktop app for that website is about to run. Do not end the script.\n\n")
os.system("npm start")
yn = input("Would you like to build the app now? (y/n): ")
if yn.lower() == "y":
if platform.system() == "Windows":
os.system("npm run build-win")
elif platform.system() == "Darwin":
os.system("npm run build-mac")
return "The app has been built. Check the 'dist' folder for the installer."
else:
return "To build, run ```npm run build-win``` for Windows and ```npm run build-mac``` for MacOS."
else:
return "Please enter a valid URL!"
print(main(url=input("Enter a url: ")))