[charts] Add TV charts

This commit is contained in:
2026-03-11 23:53:08 -04:00
parent 118e208a36
commit 0e5d8e6b2f
4 changed files with 313 additions and 2 deletions

View File

@ -21,7 +21,12 @@ from django.views.generic import DetailView, FormView, TemplateView
from django.views.generic.edit import CreateView
from django.views.generic.list import ListView
from moods.models import Mood
from music.aggregators import live_charts, scrobble_counts, week_of_scrobbles
from music.aggregators import (
live_charts,
live_tv_charts,
scrobble_counts,
week_of_scrobbles,
)
from pendulum.parsing.exceptions import ParserError
from rest_framework import status
from rest_framework.decorators import (
@ -59,6 +64,7 @@ from scrobbles.utils import (
get_long_plays_completed,
get_long_plays_in_progress,
)
from profiles.utils import now_user_timezone
logger = logging.getLogger(__name__)
@ -822,6 +828,68 @@ class ChartRecordView(TemplateView):
"year": list(live_charts(**track, chart_period="year")),
"all": list(live_charts(**track)),
}
now = timezone.now()
tzinfo = now.tzinfo
if user.is_authenticated:
now = now_user_timezone(user.profile)
tzinfo = now.tzinfo
now = now.date()
start_of_today = datetime.combine(now, datetime.min.time(), tzinfo)
start_day_of_week = start_of_today - timedelta(
days=now.isoweekday() % 7
)
start_day_of_month = now.replace(day=1)
# TV Series Scrobbles
series_scrobbles = Scrobble.objects.filter(
user=user,
video__video_type=Video.VideoType.TV_EPISODE,
played_to_completion=True,
)
context_data["series_this_week"] = series_scrobbles.filter(
timestamp__gte=start_day_of_week
).order_by("-timestamp")
context_data["series_this_month"] = series_scrobbles.filter(
timestamp__gte=start_day_of_month
).order_by("-timestamp")
# Movie Scrobbles
movie_scrobbles = Scrobble.objects.filter(
user=user,
video__video_type=Video.VideoType.MOVIE,
played_to_completion=True,
)
context_data["videos_this_week"] = movie_scrobbles.filter(
timestamp__gte=start_day_of_week
).order_by("-timestamp")
context_data["videos_this_month"] = movie_scrobbles.filter(
timestamp__gte=start_day_of_month
).order_by("-timestamp")
# YouTube Scrobbles
youtube_scrobbles = Scrobble.objects.filter(
user=user,
video__video_type=Video.VideoType.YOUTUBE,
played_to_completion=True,
)
context_data["youtube_this_week"] = youtube_scrobbles.filter(
timestamp__gte=start_day_of_week
).order_by("-timestamp")
context_data["youtube_this_month"] = youtube_scrobbles.filter(
timestamp__gte=start_day_of_month
).order_by("-timestamp")
context_data["tv_show_charts"] = {
"today": list(live_tv_charts(user=user, chart_period="today")),
"last7": list(live_tv_charts(user=user, chart_period="last7")),
"last30": list(
live_tv_charts(user=user, chart_period="last30")
),
"year": list(live_tv_charts(user=user, chart_period="year")),
"all": list(live_tv_charts(user=user, chart_period="all")),
}
return context_data
# Date provided, lookup past charts, returning nothing if it's now or in the future.