Update main.py
Browse files
main.py
CHANGED
|
@@ -1,17 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from kivy.app import App
|
| 2 |
from kivy.uix.boxlayout import BoxLayout
|
| 3 |
from kivy.lang import Builder
|
| 4 |
-
|
| 5 |
-
# It is necessary to import the webview for the buildozer to include it
|
| 6 |
from kivy.uix.webview import WebView
|
| 7 |
|
| 8 |
-
Builder.load_string(
|
| 9 |
<WebViewLayout>:
|
| 10 |
orientation: 'vertical'
|
| 11 |
WebView:
|
| 12 |
id: webview
|
| 13 |
-
url: '
|
| 14 |
-
|
| 15 |
|
| 16 |
class WebViewLayout(BoxLayout):
|
| 17 |
pass
|
|
@@ -21,4 +54,62 @@ class SiteToApkApp(App):
|
|
| 21 |
return WebViewLayout()
|
| 22 |
|
| 23 |
if __name__ == '__main__':
|
| 24 |
-
SiteToApkApp().run()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import subprocess
|
| 3 |
+
from flask import Flask, request, render_template, send_from_directory, flash, redirect, url_for
|
| 4 |
+
from werkzeug.utils import secure_filename
|
| 5 |
+
|
| 6 |
+
app = Flask(__name__)
|
| 7 |
+
app.secret_key = 'supersecretkey'
|
| 8 |
+
UPLOAD_FOLDER = 'uploads'
|
| 9 |
+
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
| 10 |
+
|
| 11 |
+
if not os.path.exists(UPLOAD_FOLDER):
|
| 12 |
+
os.makedirs(UPLOAD_FOLDER)
|
| 13 |
+
|
| 14 |
+
@app.route('/')
|
| 15 |
+
def index():
|
| 16 |
+
"""Renders the main page with the URL input form."""
|
| 17 |
+
return render_template('index.html')
|
| 18 |
+
|
| 19 |
+
@app.route('/build', methods=['POST'])
|
| 20 |
+
def build_apk():
|
| 21 |
+
"""Handles the form submission and initiates the APK build."""
|
| 22 |
+
url = request.form.get('url')
|
| 23 |
+
if not url:
|
| 24 |
+
flash('Please provide a URL.')
|
| 25 |
+
return redirect(url_for('index'))
|
| 26 |
+
|
| 27 |
+
# Sanitize the URL to create a valid project name
|
| 28 |
+
project_name = secure_filename(url.replace('https://', '').replace('http://', '').replace('.', '_'))
|
| 29 |
+
project_path = os.path.join(app.config['UPLOAD_FOLDER'], project_name)
|
| 30 |
+
|
| 31 |
+
if not os.path.exists(project_path):
|
| 32 |
+
os.makedirs(project_path)
|
| 33 |
+
|
| 34 |
+
# Create the main.py for the Android app
|
| 35 |
+
main_py_content = f"""
|
| 36 |
from kivy.app import App
|
| 37 |
from kivy.uix.boxlayout import BoxLayout
|
| 38 |
from kivy.lang import Builder
|
|
|
|
|
|
|
| 39 |
from kivy.uix.webview import WebView
|
| 40 |
|
| 41 |
+
Builder.load_string('''
|
| 42 |
<WebViewLayout>:
|
| 43 |
orientation: 'vertical'
|
| 44 |
WebView:
|
| 45 |
id: webview
|
| 46 |
+
url: '{url}'
|
| 47 |
+
''')
|
| 48 |
|
| 49 |
class WebViewLayout(BoxLayout):
|
| 50 |
pass
|
|
|
|
| 54 |
return WebViewLayout()
|
| 55 |
|
| 56 |
if __name__ == '__main__':
|
| 57 |
+
SiteToApkApp().run()
|
| 58 |
+
"""
|
| 59 |
+
with open(os.path.join(project_path, 'main.py'), 'w') as f:
|
| 60 |
+
f.write(main_py_content)
|
| 61 |
+
|
| 62 |
+
# Create the requirements.txt for the Android app
|
| 63 |
+
with open(os.path.join(project_path, 'requirements.txt'), 'w') as f:
|
| 64 |
+
f.write('kivy\n')
|
| 65 |
+
f.write('kivy-webview\n')
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
# Initiate the build process using a separate script or a task queue
|
| 69 |
+
# For simplicity, we'll call buildozer directly here.
|
| 70 |
+
# In a real-world application, you would use a task queue like Celery.
|
| 71 |
+
try:
|
| 72 |
+
# Initialize buildozer
|
| 73 |
+
subprocess.run(['buildozer', '-v', 'init'], cwd=project_path, check=True)
|
| 74 |
+
|
| 75 |
+
# Update buildozer.spec for webview and permissions
|
| 76 |
+
spec_path = os.path.join(project_path, 'buildozer.spec')
|
| 77 |
+
with open(spec_path, 'r') as f:
|
| 78 |
+
spec_content = f.read()
|
| 79 |
+
|
| 80 |
+
spec_content = spec_content.replace(
|
| 81 |
+
'# (list) Requirements to be installed (in order separated by commas)\nrequirements = python3,kivy',
|
| 82 |
+
'requirements = python3,kivy,kivy-webview'
|
| 83 |
+
)
|
| 84 |
+
spec_content += '\nandroid.permissions = INTERNET'
|
| 85 |
+
|
| 86 |
+
with open(spec_path, 'w') as f:
|
| 87 |
+
f.write(spec_content)
|
| 88 |
+
|
| 89 |
+
# Run the build
|
| 90 |
+
subprocess.run(['buildozer', '-v', 'android', 'debug'], cwd=project_path, check=True)
|
| 91 |
+
|
| 92 |
+
# Find the generated APK
|
| 93 |
+
bin_dir = os.path.join(project_path, 'bin')
|
| 94 |
+
apk_files = [f for f in os.listdir(bin_dir) if f.endswith('.apk')]
|
| 95 |
+
if apk_files:
|
| 96 |
+
return redirect(url_for('download_file', project_name=project_name, filename=apk_files[0]))
|
| 97 |
+
else:
|
| 98 |
+
flash('Build failed: APK not found.')
|
| 99 |
+
return redirect(url_for('index'))
|
| 100 |
+
|
| 101 |
+
except subprocess.CalledProcessError as e:
|
| 102 |
+
flash(f'An error occurred during the build process: {e}')
|
| 103 |
+
return redirect(url_for('index'))
|
| 104 |
+
except FileNotFoundError:
|
| 105 |
+
flash('Buildozer not found. Make sure it is installed and in your PATH.')
|
| 106 |
+
return redirect(url_for('index'))
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
@app.route('/uploads/<project_name>/<filename>')
|
| 110 |
+
def download_file(project_name, filename):
|
| 111 |
+
"""Provides a download link for the generated APK."""
|
| 112 |
+
return send_from_directory(os.path.join(app.config['UPLOAD_FOLDER'], project_name, 'bin'), filename, as_attachment=True)
|
| 113 |
+
|
| 114 |
+
if __name__ == '__main__':
|
| 115 |
+
app.run(host='0.0.0.0', port=5000, debug=True)```
|