-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
49 lines (41 loc) · 1.66 KB
/
app.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
import os
import json
from flask import Flask, render_template, request, send_file, jsonify
import subprocess
import requests
app = Flask(__name__, static_folder='static', static_url_path="/static")
@app.route('/manifest.json')
def serve_manifest():
return send_file('manifest.json', mimetype='application/manifest+json')
@app.route('/sw.js')
def serve_sw():
return send_file('sw.js', mimetype='application/javascript')
@app.route('/', defaults={'path': ''})
@app.route('/execute', methods=['GET'])
@app.route('/<path:path>')
def index(path):
return render_template('index.html')
@app.route('/execute', methods=['POST'])
def execute():
short_url = json.loads(request.data)
short_url = short_url['textfield']
if(short_url == ''):
result = 'no url'
return jsonify({'result': result})
try:
# Resolve the short URL to get the final URL
response = requests.get(short_url, allow_redirects=True)
long_url = response.url
# Extract the last path segment of the long URL
last_path_segment = long_url.split('/')[-1]
last_path_segment = last_path_segment.split('?')[0]
# Run the terminal command
result = subprocess.check_output(['kptncook', 'search-by-id', last_path_segment], universal_newlines=True)
result = subprocess.check_output(['kptncook', 'sync-with-mealie'], universal_newlines=True)
result = 'successfully imported '
except subprocess.CalledProcessError as e:
result = f"An error occurred: {e.output}"
result = result + last_path_segment
return jsonify({'result': result})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)