Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,113 +1,45 @@
|
|
| 1 |
-
import json
|
| 2 |
-
import os
|
| 3 |
-
import shutil
|
| 4 |
import subprocess
|
| 5 |
import gradio as gr
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
| 10 |
-
os.makedirs(ICON_DIR, exist_ok=True)
|
| 11 |
-
|
| 12 |
-
# Directly use the provided manifest
|
| 13 |
-
def update_manifest(app_name, icon_path):
|
| 14 |
-
# Define the manifest as provided
|
| 15 |
-
manifest = {
|
| 16 |
-
"shortName": app_name,
|
| 17 |
-
"name": app_name,
|
| 18 |
-
"startUrl": "/",
|
| 19 |
-
"display": "standalone",
|
| 20 |
-
"backgroundColor": "#FFFFFF",
|
| 21 |
-
"themeColor": "#000000",
|
| 22 |
-
"icons": [
|
| 23 |
-
{"src": "icon-192.png", "type": "image/png", "sizes": "192x192"},
|
| 24 |
-
{"src": "icon-512.png", "type": "image/png", "sizes": "512x512"}
|
| 25 |
-
]
|
| 26 |
-
}
|
| 27 |
-
|
| 28 |
-
# Save the updated manifest
|
| 29 |
-
manifest_path = os.path.join(OUTPUT_DIR, "manifest.json")
|
| 30 |
-
with open(manifest_path, "w") as f:
|
| 31 |
-
json.dump(manifest, f, indent=4)
|
| 32 |
-
|
| 33 |
-
# If an icon was provided, move it to the output directory
|
| 34 |
-
if icon_path:
|
| 35 |
-
new_icon_path_192 = os.path.join(OUTPUT_DIR, "icon-192.png")
|
| 36 |
-
new_icon_path_512 = os.path.join(OUTPUT_DIR, "icon-512.png")
|
| 37 |
-
shutil.copy(icon_path, new_icon_path_192)
|
| 38 |
-
shutil.copy(icon_path, new_icon_path_512)
|
| 39 |
-
|
| 40 |
-
return manifest_path, None
|
| 41 |
-
|
| 42 |
-
def generate_apk(url, app_name, icon_path=None):
|
| 43 |
-
"""Generates an APK from a given URL using Bubblewrap, with an optional custom icon."""
|
| 44 |
-
package_name = f"com.pwa.{app_name.lower()}"
|
| 45 |
-
app_path = os.path.join(OUTPUT_DIR, app_name)
|
| 46 |
-
|
| 47 |
-
if os.path.exists(app_path):
|
| 48 |
-
shutil.rmtree(app_path)
|
| 49 |
-
|
| 50 |
-
os.makedirs(app_path, exist_ok=True)
|
| 51 |
-
|
| 52 |
-
manifest_path, error = update_manifest(app_name, icon_path)
|
| 53 |
-
if error:
|
| 54 |
-
return error, None
|
| 55 |
-
|
| 56 |
try:
|
| 57 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
result = subprocess.run(
|
| 59 |
-
['
|
| 60 |
check=True, capture_output=True, text=True
|
| 61 |
)
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
# Bubblewrap Build
|
| 65 |
-
subprocess.run(
|
| 66 |
-
['bubblewrap', 'build', '--directory', app_path],
|
| 67 |
-
check=True
|
| 68 |
-
)
|
| 69 |
-
|
| 70 |
-
apk_path = os.path.join(app_path, "app-release.apk")
|
| 71 |
-
if os.path.exists(apk_path):
|
| 72 |
-
final_apk_path = os.path.join(OUTPUT_DIR, f"{app_name}.apk")
|
| 73 |
-
shutil.move(apk_path, final_apk_path)
|
| 74 |
-
return "✅ APK Generated Successfully! Download below.", final_apk_path
|
| 75 |
-
else:
|
| 76 |
-
return "Error: APK generation failed.", None
|
| 77 |
except subprocess.CalledProcessError as e:
|
| 78 |
-
|
| 79 |
-
print(f"Stdout: {e.stdout}")
|
| 80 |
-
print(f"Stderr: {e.stderr}")
|
| 81 |
-
return f"Error: {e}", None
|
| 82 |
|
| 83 |
-
#
|
| 84 |
-
def process_url(
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
icon_path = os.path.join(ICON_DIR, f"{app_name}.png")
|
| 88 |
-
try:
|
| 89 |
-
shutil.move(icon_file.name, icon_path)
|
| 90 |
-
except Exception as e:
|
| 91 |
-
return f"Error while moving icon: {e}", None
|
| 92 |
-
|
| 93 |
-
status, apk_path = generate_apk(url, app_name, icon_path)
|
| 94 |
-
if apk_path:
|
| 95 |
-
return status, apk_path
|
| 96 |
-
return status, None
|
| 97 |
|
| 98 |
with gr.Blocks() as demo:
|
| 99 |
-
gr.Markdown("#
|
| 100 |
|
|
|
|
| 101 |
url_input = gr.Textbox(label="Enter Website URL", placeholder="https://example.com")
|
| 102 |
-
name_input = gr.Textbox(label="App Name", placeholder="
|
| 103 |
-
icon_input = gr.File(label="Upload App Icon (PNG)", file_types=[".png"])
|
| 104 |
|
|
|
|
| 105 |
output_text = gr.Textbox(label="Status")
|
| 106 |
download_link = gr.File(label="Download APK")
|
| 107 |
-
|
|
|
|
| 108 |
submit_btn = gr.Button("Generate APK")
|
| 109 |
|
| 110 |
-
submit_btn.click(process_url, inputs=[url_input, name_input
|
| 111 |
|
|
|
|
| 112 |
demo.launch(share=True)
|
| 113 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import subprocess
|
| 2 |
import gradio as gr
|
| 3 |
+
import os
|
| 4 |
|
| 5 |
+
# Function to run Bulldozer via Node.js from Python
|
| 6 |
+
def run_bulldozer(pwa_url, app_name, output_dir="output_apk"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
try:
|
| 8 |
+
# Ensure the output directory exists
|
| 9 |
+
if not os.path.exists(output_dir):
|
| 10 |
+
os.makedirs(output_dir)
|
| 11 |
+
|
| 12 |
+
# Run Bulldozer Node.js script using subprocess
|
| 13 |
result = subprocess.run(
|
| 14 |
+
['node', 'bulldozer.js', pwa_url, app_name, output_dir],
|
| 15 |
check=True, capture_output=True, text=True
|
| 16 |
)
|
| 17 |
+
return f"APK generated successfully: {os.path.join(output_dir, f'{app_name}.apk')}", os.path.join(output_dir, f'{app_name}.apk')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
except subprocess.CalledProcessError as e:
|
| 19 |
+
return f"Error generating APK: {e.stderr}", None
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
# Create the Gradio interface
|
| 22 |
+
def process_url(pwa_url, app_name):
|
| 23 |
+
status, apk_path = run_bulldozer(pwa_url, app_name)
|
| 24 |
+
return status, apk_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
with gr.Blocks() as demo:
|
| 27 |
+
gr.Markdown("# Convert Website to Android App")
|
| 28 |
|
| 29 |
+
# User Inputs for the URL and app name
|
| 30 |
url_input = gr.Textbox(label="Enter Website URL", placeholder="https://example.com")
|
| 31 |
+
name_input = gr.Textbox(label="App Name", placeholder="MyWebApp")
|
|
|
|
| 32 |
|
| 33 |
+
# Output area for status and download link
|
| 34 |
output_text = gr.Textbox(label="Status")
|
| 35 |
download_link = gr.File(label="Download APK")
|
| 36 |
+
|
| 37 |
+
# Button to trigger the conversion
|
| 38 |
submit_btn = gr.Button("Generate APK")
|
| 39 |
|
| 40 |
+
submit_btn.click(process_url, inputs=[url_input, name_input], outputs=[output_text, download_link])
|
| 41 |
|
| 42 |
+
# Launch Gradio app
|
| 43 |
demo.launch(share=True)
|
| 44 |
|
| 45 |
+
|