Refactor jellyfin scobbler view

This commit is contained in:
2023-01-05 15:45:49 -05:00
parent f274aa7d0e
commit 2badc6de54

View File

@ -54,12 +54,18 @@ def scrobble_endpoint(request):
def jellyfin_websocket(request): def jellyfin_websocket(request):
data_dict = request.data data_dict = request.data
media_type = data_dict["ItemType"] media_type = data_dict["ItemType"]
imdb_id = data_dict.get("Provider_imdb", None)
if not imdb_id:
logger.error(
"No IMDB ID received. This is likely because all metadata is bad, not scrobbling"
)
return Response({}, status=status.HTTP_400_BAD_REQUEST)
# Check if it's a TV Episode # Check if it's a TV Episode
video_dict = { video_dict = {
"title": data_dict["Name"], "title": data_dict.get("Name", ""),
"imdb_id": data_dict["Provider_imdb"], "imdb_id": imdb_id,
"video_type": Video.VideoType.MOVIE, "video_type": Video.VideoType.MOVIE,
"year": data_dict["Year"], "year": data_dict.get("Year", ""),
} }
if media_type == 'Episode': if media_type == 'Episode':
series_name = data_dict["SeriesName"] series_name = data_dict["SeriesName"]
@ -67,8 +73,8 @@ def jellyfin_websocket(request):
video_dict['video_type'] = Video.VideoType.TV_EPISODE video_dict['video_type'] = Video.VideoType.TV_EPISODE
video_dict["tv_series_id"] = series.id video_dict["tv_series_id"] = series.id
video_dict["episode_number"] = data_dict["EpisodeNumber"] video_dict["episode_number"] = data_dict.get("EpisodeNumber", "")
video_dict["season_number"] = data_dict["SeasonNumber"] video_dict["season_number"] = data_dict.get("SeasonNumber", "")
video_dict["tvdb_id"] = data_dict.get("Provider_tvdb", None) video_dict["tvdb_id"] = data_dict.get("Provider_tvdb", None)
video_dict["tvrage_id"] = data_dict.get("Provider_tvrage", None) video_dict["tvrage_id"] = data_dict.get("Provider_tvrage", None)
@ -89,17 +95,8 @@ def jellyfin_websocket(request):
'in_progress': True, 'in_progress': True,
} }
existing_finished_scrobble = ( existing_scrobble = (
Scrobble.objects.filter( Scrobble.objects.filter(video=video, user_id=request.user.id)
video=video, user_id=request.user.id, in_progress=False
)
.order_by('-modified')
.first()
)
existing_in_progress_scrobble = (
Scrobble.objects.filter(
video=video, user_id=request.user.id, in_progress=True
)
.order_by('-modified') .order_by('-modified')
.first() .first()
) )
@ -107,30 +104,33 @@ def jellyfin_websocket(request):
minutes_from_now = timezone.now() + timedelta(minutes=15) minutes_from_now = timezone.now() + timedelta(minutes=15)
a_day_from_now = timezone.now() + timedelta(days=1) a_day_from_now = timezone.now() + timedelta(days=1)
if ( existing_finished_scrobble = (
existing_finished_scrobble existing_scrobble
and existing_finished_scrobble.modified < minutes_from_now and not existing_scrobble.in_progress
): and existing_scrobble.modified < minutes_from_now
)
existing_in_progress_scrobble = (
existing_scrobble
and existing_scrobble.in_progress
and existing_scrobble.modified > a_day_from_now
)
delete_stale_scrobbles = getattr(settings, "DELETE_STALE_SCROBBLES", True)
if existing_finished_scrobble:
logger.info( logger.info(
'Found a scrobble for this video less than 15 minutes ago, holding off scrobbling again' 'Found a scrobble for this video less than 15 minutes ago, holding off scrobbling again'
) )
return Response(video_dict, status=status.HTTP_204_NO_CONTENT) return Response(video_dict, status=status.HTTP_204_NO_CONTENT)
existing_scrobble_more_than_a_day_old = (
existing_in_progress_scrobble
and existing_in_progress_scrobble.modified > a_day_from_now
)
delete_stale_scrobbles = getattr(settings, "DELETE_STALE_SCROBBLES", True)
# Check if found in progress scrobble is more than a day old # Check if found in progress scrobble is more than a day old
if existing_scrobble_more_than_a_day_old: if existing_in_progress_scrobble:
logger.info( logger.info(
'Found a scrobble for this video more than a day old, creating a new scrobble' 'Found a scrobble for this video more than a day old, creating a new scrobble'
) )
scrobble = existing_in_progress_scrobble scrobble = existing_in_progress_scrobble
scrobble_created = False scrobble_created = False
else: else:
if existing_scrobble_more_than_a_day_old and delete_stale_scrobbles: if existing_in_progress_scrobble and delete_stale_scrobbles:
existing_in_progress_scrobble.delete() existing_in_progress_scrobble.delete()
scrobble, scrobble_created = Scrobble.objects.get_or_create( scrobble, scrobble_created = Scrobble.objects.get_or_create(
**scrobble_dict **scrobble_dict