-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflask_app.py
More file actions
64 lines (56 loc) · 2.37 KB
/
flask_app.py
File metadata and controls
64 lines (56 loc) · 2.37 KB
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
from flask import Flask, url_for,render_template,request,flash
import shutil
import os
import random
import time
from datetime import datetime
app = Flask(__name__)
app.secret_key = os.urandom(24)
path2 = os.path.dirname(os.path.abspath(__file__))
print(path2)
@app.route("/")
def start():
return render_template('start.html')
@app.route('/index')
def index():
current_date = str(time.strftime("%Y%m%d", time.localtime()))
path = path2+'/static/voices/'+current_date
if not os.path.exists(path):
os.makedirs(path)
files = [f for f in os.listdir(path) if os.path.isfile(os.path.join(path, f))]
files.sort(key=lambda x: os.path.getmtime(os.path.join(path, x)), reverse=True)
if len(files)==0:
mp3_url=None
mp3isim="Başka sipariş gelmedi."
else:
latest_file = files[0]
mp3_url = url_for('static', filename=f'voices/{current_date}/{latest_file}')
mp3isim=mp3_url.split(".ogg")[0].split("/static/voices/")[1]
path = path2+'/static/played'
files = [f for f in os.listdir(path) if os.path.isfile(os.path.join(path, f))]
files.sort(key=lambda x: os.path.getmtime(os.path.join(path, x)), reverse=True)
latest_files = files[:5] # Get the latest 5 files
mp3_urls = []
mp3isimler=[]
for file in latest_files:
mp3_urls.append(url_for('static', filename=f'played/{file}'))
mp3isimler.append(url_for('static', filename=f'played/{file}').split(".ogg")[0].split("/static/played/")[1])
try:
with open(path2+"/static/texts/"+current_date+'_messages.txt', "r") as f:
lines = f.readlines()
last_10_lines = lines[-10:]
for i, line in enumerate(reversed(last_10_lines), start=1):
flash(f"Sipariş {i}: {line}")
except:
with open(path2+"/static/texts/"+current_date+'_messages.txt', 'w', encoding="utf-8") as f:
f.write('Güü naay dıın\n')
return render_template('index.html', mp3_url=mp3_url, mp3_urls=mp3_urls,mp3isim=mp3isim,mp3isimler=mp3isimler)
@app.route('/move_mp3_file', methods=['POST'])
def move_mp3_file():
current_date = str(time.strftime("%Y%m%d", time.localtime()))
mp3_url = path2+"/"+request.form['mp3_url']
mp3_new_path = mp3_url.replace("voices/"+current_date, "played")
os.rename(mp3_url, mp3_new_path)
return "OK"
if __name__ == "__main__":
app.run(host = '0.0.0.0', port='8080')