See #170: now record downloads count on tracks/uploads

This commit is contained in:
Eliot Berriot 2020-01-20 12:13:02 +01:00
parent 3674d1235d
commit b22b9c83b0
No known key found for this signature in database
GPG key ID: 6B501DFD73514E14
12 changed files with 161 additions and 18 deletions

View file

@ -4,6 +4,11 @@ import magic
import mutagen
import pydub
from django.conf import settings
from django.core.cache import cache
from django.db.models import F
from funkwhale_api.common import throttling
from funkwhale_api.common.search import get_fts_query # noqa
from funkwhale_api.common.search import get_query # noqa
from funkwhale_api.common.search import normalize_query # noqa
@ -91,3 +96,25 @@ def transcode_file(input, output, input_format, output_format, **kwargs):
def transcode_audio(audio, output, output_format, **kwargs):
with output.open("wb"):
return audio.export(output, format=output_format, **kwargs)
def increment_downloads_count(upload, user, wsgi_request):
ident = throttling.get_ident(user=user, request=wsgi_request)
cache_key = "downloads_count:upload-{}:{}-{}".format(
upload.pk, ident["type"], ident["id"]
)
value = cache.get(cache_key)
if value:
# download already tracked
return
upload.downloads_count = F("downloads_count") + 1
upload.track.downloads_count = F("downloads_count") + 1
upload.save(update_fields=["downloads_count"])
upload.track.save(update_fields=["downloads_count"])
duration = max(upload.duration or 0, settings.MIN_DELAY_BETWEEN_DOWNLOADS_COUNT)
cache.set(cache_key, 1, duration)