diff --git a/vrobbler/apps/charts/templates/charts/chart_index.html b/vrobbler/apps/charts/templates/charts/chart_index.html index a4489f0..19893f9 100644 --- a/vrobbler/apps/charts/templates/charts/chart_index.html +++ b/vrobbler/apps/charts/templates/charts/chart_index.html @@ -83,6 +83,12 @@ {% endfor %} + +
+
+ 🎵 Spotify Tracks + 🎵 Bandcamp Tracks +
+
{% if chart_type == "maloja" %} {% include "scrobbles/_top_charts.html" %} {% else %} diff --git a/vrobbler/apps/charts/urls.py b/vrobbler/apps/charts/urls.py index be147a4..fe05a7b 100644 --- a/vrobbler/apps/charts/urls.py +++ b/vrobbler/apps/charts/urls.py @@ -1,4 +1,10 @@ -from charts.views import BirdsChartView, ChartDetailView, ChartRecordView, SpotifyTracksView +from charts.views import ( + BandcampTracksView, + BirdsChartView, + ChartDetailView, + ChartRecordView, + SpotifyTracksView, +) from django.urls import path app_name = "charts" @@ -6,6 +12,7 @@ app_name = "charts" urlpatterns = [ path("charts/", ChartRecordView.as_view(), name="charts-home"), path("charts/spotify/", SpotifyTracksView.as_view(), name="spotify-tracks"), + path("charts/bandcamp/", BandcampTracksView.as_view(), name="bandcamp-tracks"), path("charts/birds/", BirdsChartView.as_view(), name="birds-chart"), path("charts//", ChartDetailView.as_view(), name="chart-detail"), ] diff --git a/vrobbler/apps/charts/views.py b/vrobbler/apps/charts/views.py index e5743ea..a4e9e8a 100644 --- a/vrobbler/apps/charts/views.py +++ b/vrobbler/apps/charts/views.py @@ -221,6 +221,7 @@ class ChartRecordView(TemplateView): context["period"] = "current" context["year"] = current_year context["month"] = current_month + context["month_name"] = calendar.month_name[current_month] context["week"] = current_week context["day"] = current_day @@ -301,6 +302,7 @@ class ChartRecordView(TemplateView): context["period"] = "historical" context["year"] = year context["month"] = month + context["month_name"] = calendar.month_name[month] if month else None context["week"] = week context["day"] = day @@ -604,9 +606,9 @@ class ChartRecordView(TemplateView): if bird_id: bird_counts[bird_id] = bird_counts.get(bird_id, 0) + quantity - sorted_birds = sorted( - bird_counts.items(), key=lambda x: x[1], reverse=True - )[:limit] + sorted_birds = sorted(bird_counts.items(), key=lambda x: x[1], reverse=True)[ + :limit + ] bird_ids = [bid for bid, _ in sorted_birds] birds = Bird.objects.filter(id__in=bird_ids) @@ -727,12 +729,22 @@ class SpotifyTracksView(TemplateView): template_name = "charts/spotify_tracks.html" def get_spotify_tracks(self, user, limit=50): + non_spotify_mopidy_tracks = ( + Scrobble.objects.filter( + user=user, + source="Mopidy", + track__isnull=False, + ) + .exclude(log__mopidy_source="spotify") + .values("track") + ) track_ids = ( Scrobble.objects.filter( user=user, track__isnull=False, ) .filter(Q(source="Last.fm") | Q(log__mopidy_source="spotify")) + .exclude(track__in=non_spotify_mopidy_tracks) .values("track") .annotate(count=Count("id")) .order_by("-count")[:limit] @@ -760,6 +772,53 @@ class SpotifyTracksView(TemplateView): return context +class BandcampTracksView(TemplateView): + template_name = "charts/bandcamp_tracks.html" + + def get_bandcamp_tracks(self, user, limit=50): + non_bandcamp_mopidy_tracks = ( + Scrobble.objects.filter( + user=user, + source="Mopidy", + track__isnull=False, + ) + .exclude(log__mopidy_source="bandcamp") + .values("track") + ) + track_ids = ( + Scrobble.objects.filter( + user=user, + track__isnull=False, + log__mopidy_source="bandcamp", + ) + .exclude(track__in=non_bandcamp_mopidy_tracks) + .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["bandcamp_tracks"] = self.get_bandcamp_tracks(user) + return context + + class BirdsChartView(TemplateView): template_name = "charts/birds_chart.html" @@ -792,9 +851,9 @@ class BirdsChartView(TemplateView): if bird_id: bird_counts[bird_id] = bird_counts.get(bird_id, 0) + quantity - sorted_birds = sorted( - bird_counts.items(), key=lambda x: x[1], reverse=True - )[:limit] + sorted_birds = sorted(bird_counts.items(), key=lambda x: x[1], reverse=True)[ + :limit + ] bird_ids = [bid for bid, _ in sorted_birds] birds = Bird.objects.filter(id__in=bird_ids) diff --git a/vrobbler/templates/base_list.html b/vrobbler/templates/base_list.html index 3df1bb6..a3af28d 100644 --- a/vrobbler/templates/base_list.html +++ b/vrobbler/templates/base_list.html @@ -13,7 +13,6 @@
Charts - Spotify Tracks
{% endif %}
diff --git a/vrobbler/templates/charts/bandcamp_tracks.html b/vrobbler/templates/charts/bandcamp_tracks.html new file mode 100644 index 0000000..afc3801 --- /dev/null +++ b/vrobbler/templates/charts/bandcamp_tracks.html @@ -0,0 +1,34 @@ +{% extends "base_list.html" %} + +{% block title %}Bandcamp Tracks{% endblock %} + +{% block head_extra %} + +{% endblock %} + +{% block lists %} +
+
+

🎵 Top Bandcamp Tracks

+

Shows tracks scrobbled from Bandcamp via Mopidy

+
+
+ +
+
+
    + {% for item in bandcamp_tracks %} +
  • + #{{ forloop.counter }} + {{ item.track.title }} + {{ item.count }} +
  • + {% empty %} +
  • No Bandcamp tracks found.
  • + {% endfor %} +
+
+
+{% endblock %} diff --git a/vrobbler/templates/charts/chart_index.html b/vrobbler/templates/charts/chart_index.html index f0e5b9d..1c880ba 100644 --- a/vrobbler/templates/charts/chart_index.html +++ b/vrobbler/templates/charts/chart_index.html @@ -17,6 +17,13 @@ {% block lists %} + + {% if chart_type == "maloja" %} {% include "scrobbles/_top_charts.html" %} {% else %} @@ -24,9 +31,16 @@
- {% if year %}{{year}}{% if month %} {{month|date:"F"}}{% endif %}{% endif %} - {% if week %}Week {{week}}{% endif %} - {% if day %}{{day}}{% endif %} + {{ year }} + {% if month %} + {{ month_name }} + {% endif %} + {% if week %} + Week {{ week }} + {% endif %} + {% if day %} + {{ day }} + {% endif %}
{% if period_type == "day" %}