diff --git a/app/models.py b/app/models.py index e67adb8..e8897cf 100644 --- a/app/models.py +++ b/app/models.py @@ -3,6 +3,7 @@ from flask_login import UserMixin from datetime import datetime from app import db, login from werkzeug.security import generate_password_hash, check_password_hash +import os # Just a note, my role system is really quite terrible, but I've implemented as good as a system as I can for a simple RBAC without Hierarchy. # Once could create a complex system, but it would be better to properly work with SQLAlchemy to create proper permissions, hierarchy, parent/child etc. rather than to work with simple strings. @@ -111,22 +112,6 @@ class Post(db.Model): def __repr__(self): return ''.format(self.body) -class YouTubeAudio(db.Model): - id = db.Column(db.String(11), primary_key=True) # 11 char id, presumed to stay the same for the long haul. Should be able to change to 12 chars. - url = db.Column(db.String(64)) # 43 -> 64 - title = db.Column(db.String(128)) # 120 > 128 - creator = db.Column(db.String(32)) # 20 -> 32 - filename = db.Column(db.String(156)) # 128 + 11 + 1 -> 156 - duration = db.Column(db.Integer) - -class SoundcloudAudio(db.Model): - id = db.Column(db.Integer, primary_key=True) # hidden API-accessible only ID - url = db.Column(db.String(256)) - title = db.Column(db.String(128)) - creator = db.Column(db.String(64)) - filename = db.Column(db.String(156)) - duration = db.Column(db.Integer) - @login.user_loader def load_user(id): return User.query.get(int(id)) \ No newline at end of file diff --git a/app/sound_models.py b/app/sound_models.py new file mode 100644 index 0000000..895af34 --- /dev/null +++ b/app/sound_models.py @@ -0,0 +1,40 @@ +from datetime import datetime +from app import db +import subprocess +import json +import os + +class YouTubeAudio(db.Model): + id = db.Column(db.String(11), primary_key=True) # 11 char id, presumed to stay the same for the long haul. Should be able to change to 12 chars. + url = db.Column(db.String(64)) # 43 -> 64 + title = db.Column(db.String(128)) # 120 > 128 + creator = db.Column(db.String(128)) # Seems to be Uploader set, so be careful with this + uploader = db.Column(db.String(32)) # 20 -> 32 + filename = db.Column(db.String(156)) # 128 + 11 + 1 -> 156 + duration = db.Column(db.Integer) + download_timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow) + last_access_timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow) + + def file_exists(self): + return os.path.exists(os.path.join('app', 'sounds', 'youtube', self.filename)) + + def fill_metadata(self): + self.url = f'https://www.youtube.com/watch?v={self.id}' + processFilename = subprocess.Popen(['youtube-dl', '-x', '--audio-format', 'mp3', '--restrict-filenames', '--get-filename', self.url], + encoding='utf-8', stdout=subprocess.PIPE) + self.filename = processFilename.communicate()[0].split('.')[0] + 'mp3' + processJSON = subprocess.Popen(['youtube-dl', '-x', '--audio-format', 'mp3', '--restrict-filenames', '--dump-json', self.url], + encoding='utf-8', stdout=subprocess.PIPE) + data = json.loads(processJSON.communicate()[0]) + self.duration = data['duration'] + self.creator = data['creator'] or data['uploader'] + self.uploader = data['uploader'] or data['creator'] + self.title = data['title'] or data['alt_title'] # Do not trust alt-title ; it is volatile and uploader set, e.x. https://i.imgur.com/Tgff4rI.png + +class SoundcloudAudio(db.Model): + id = db.Column(db.Integer, primary_key=True) # hidden API-accessible only ID + url = db.Column(db.String(256)) + title = db.Column(db.String(128)) + creator = db.Column(db.String(64)) + filename = db.Column(db.String(156)) + duration = db.Column(db.Integer) \ No newline at end of file