add access timestamp and count updates with session commits

not sure how well I'm doing the session commits, I need help lol
This commit is contained in:
Xevion
2019-12-24 03:17:11 -06:00
parent 5136af6e65
commit 250b39b462
2 changed files with 13 additions and 2 deletions

View File

@@ -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/<service>/<mediaid>')
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/<service>/<mediaid>')
def duration(service, mediaid):
if service == 'youtube':

View File

@@ -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)