[charts] Add Spotify charts!
This commit is contained in:
@ -1,8 +1,9 @@
|
||||
from charts.views import ChartRecordView
|
||||
from charts.views import ChartRecordView, SpotifyTracksView
|
||||
from django.urls import path
|
||||
|
||||
app_name = "charts"
|
||||
|
||||
urlpatterns = [
|
||||
path("charts/", ChartRecordView.as_view(), name="charts-home"),
|
||||
path("charts/spotify/", SpotifyTracksView.as_view(), name="spotify-tracks"),
|
||||
]
|
||||
|
||||
@ -2,10 +2,11 @@ import calendar
|
||||
from datetime import timedelta
|
||||
|
||||
from charts.models import ChartRecord
|
||||
from django.db.models import Q
|
||||
from django.db.models import Count, Q
|
||||
from django.utils import timezone
|
||||
from django.views.generic import TemplateView
|
||||
from profiles.utils import now_user_timezone
|
||||
from scrobbles.models import Scrobble
|
||||
|
||||
MEDIA_TYPE_FILTERS = {
|
||||
"artist": Q(artist__isnull=False),
|
||||
@ -556,3 +557,40 @@ class ChartRecordView(TemplateView):
|
||||
return f"/charts/?date={available_years[idx - 1]}"
|
||||
return f"/charts/?date={year + 1}"
|
||||
return None
|
||||
|
||||
|
||||
class SpotifyTracksView(TemplateView):
|
||||
template_name = "charts/spotify_tracks.html"
|
||||
|
||||
def get_spotify_tracks(self, user, limit=50):
|
||||
track_ids = (
|
||||
Scrobble.objects.filter(
|
||||
user=user,
|
||||
track__isnull=False,
|
||||
)
|
||||
.filter(Q(source="Last.fm") | Q(log__mopidy_source="spotify"))
|
||||
.values("track")
|
||||
.annotate(count=Count("id"))
|
||||
.order_by("-count")[:limit]
|
||||
)
|
||||
from music.models import Track
|
||||
|
||||
track_id_list = [item["track"] for item in track_ids]
|
||||
tracks = Track.objects.filter(id__in=track_id_list)
|
||||
track_map = {t.id: t for t in tracks}
|
||||
return [
|
||||
{
|
||||
"track": track_map[tid],
|
||||
"count": next(
|
||||
(item["count"] for item in track_ids if item["track"] == tid), 0
|
||||
),
|
||||
}
|
||||
for tid in track_id_list
|
||||
if tid in track_map
|
||||
]
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
user = self.request.user
|
||||
context["spotify_tracks"] = self.get_spotify_tracks(user)
|
||||
return context
|
||||
|
||||
Reference in New Issue
Block a user