diff --git a/app/sound.py b/app/sound.py index 78d0847..fa61168 100644 --- a/app/sound.py +++ b/app/sound.py @@ -8,11 +8,14 @@ import re import json import subprocess +# Retrieves the YouTubeAudio object relevant to the mediaid if available. If not, it facilitiates the creation and writing of one. +# Also helps with access times. def get_youtube(mediaid): audio = YouTubeAudio.query.filter_by(id=mediaid).first() if audio is not None: - return audio + return audio.access() +# Returns the duration of a specificed media @app.route('/stream//') def stream(service, mediaid): if service == 'youtube': @@ -24,7 +27,8 @@ def stream(service, mediaid): return Response('Not implemented', status=501, mimetype='application/json') else: return Response('Bad request', status=400, mimetype='application/json') -# Prepares a URL for download, returning the duration it should play for if streamed + +# Returns the duration of a specific media @app.route('/duration//') def duration(service, mediaid): if service == 'youtube': diff --git a/app/sound_models.py b/app/sound_models.py index cda2eda..80313b0 100644 --- a/app/sound_models.py +++ b/app/sound_models.py @@ -12,9 +12,16 @@ class YouTubeAudio(db.Model): uploader = db.Column(db.String(32)) # 20 -> 32 filename = db.Column(db.String(156)) # 128 + 11 + 1 -> 156 duration = db.Column(db.Integer) + access_count = 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 access(self): + self.access_count += 1 + self.last_access_timestamp = datetime.utcnow() + db.session.commit() + return self + def getPath(self): return os.path.join('app', 'sounds', 'youtube', self.filename)