-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
67 lines (53 loc) · 1.91 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from flask import Flask, request, send_from_directory
import requests
import random
import string
from unidecode import unidecode
from mutagen.mp4 import MP4Cover, MP4
import os
from flask_cors import CORS
import schedule
app = Flask(__name__)
CORS(app)
# get the app url from the environment variable
app_url = os.environ.get('APP_URL', 'https://tagwriter.musicder.net')
port = int(os.environ.get('PORT', 8080))
@app.route('/id3', methods=['GET', 'POST'])
def index():
try:
payload = request.json
filename = (unidecode(payload["name"])).replace(" ", "-") + '-' + ''.join((random.choice(string.ascii_lowercase)
for x in range(5))) + ".m4a"
r = requests.get(payload["song_url"], allow_redirects=True)
open(f"./public/{filename}", 'wb').write(r.content)
artwork = requests.get(payload["cover_url"], allow_redirects=True)
media_file = MP4(f"./public/{filename}")
media_file["\xa9nam"] = payload["name"]
media_file["\xa9ART"] = payload["artist"]
media_file["\xa9alb"] = payload["album"]
media_file["covr"] = [MP4Cover(artwork.content, MP4Cover.FORMAT_JPEG)]
media_file.save()
return {
"status": True,
"path": "/public/" + filename,
"url": app_url + "/public/" + filename
}
except:
return {
"status": False
}
@app.route('/public/<path:path>')
def send_js(path):
return send_from_directory('public', path)
def deletecache():
dir = './public'
for f in os.listdir(dir):
os.remove(os.path.join(dir, f))
schedule.every(2).hours.do(deletecache)
# if __name__ == '__main__':
# app.debug = True
# app.run(host='0.0.0.0', port=port, use_reloader=True, threaded=True)
# Production
if __name__ == '__main__':
app.debug = False
app.run(host='0.0.0.0', port=port)