Split scrobble endpoints up
This commit is contained in:
@ -4,5 +4,6 @@ from scrobbles import views
|
|||||||
app_name = 'scrobbles'
|
app_name = 'scrobbles'
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('', views.scrobble_list, name='scrobble-list'),
|
path('', views.scrobble_endpoint, name='scrobble-list'),
|
||||||
|
path('jellyfin/', views.jellyfin_websocket, name='jellyfin-websocket'),
|
||||||
]
|
]
|
||||||
|
|||||||
@ -38,101 +38,101 @@ class RecentScrobbleList(ListView):
|
|||||||
|
|
||||||
|
|
||||||
@csrf_exempt
|
@csrf_exempt
|
||||||
@api_view(['GET', 'POST'])
|
@api_view(['GET'])
|
||||||
def scrobble_list(request):
|
def scrobble_endpoint(request):
|
||||||
"""List all Scrobbles, or create a new Scrobble"""
|
"""List all Scrobbles, or create a new Scrobble"""
|
||||||
if request.method == 'GET':
|
scrobble = Scrobble.objects.all()
|
||||||
scrobble = Scrobble.objects.all()
|
serializer = ScrobbleSerializer(scrobble, many=True)
|
||||||
serializer = ScrobbleSerializer(scrobble, many=True)
|
return Response(serializer.data)
|
||||||
return Response(serializer.data)
|
|
||||||
|
|
||||||
elif request.method == 'POST':
|
|
||||||
data_dict = request.data
|
|
||||||
media_type = data_dict["ItemType"]
|
|
||||||
# Check if it's a TV Episode
|
|
||||||
video_dict = {
|
|
||||||
"title": data_dict["Name"],
|
|
||||||
"imdb_id": data_dict["Provider_imdb"],
|
|
||||||
"video_type": Video.VideoType.MOVIE,
|
|
||||||
"year": data_dict["Year"],
|
|
||||||
}
|
|
||||||
if media_type == 'Episode':
|
|
||||||
series_name = data_dict["SeriesName"]
|
|
||||||
series, series_created = Series.objects.get_or_create(
|
|
||||||
name=series_name
|
|
||||||
)
|
|
||||||
|
|
||||||
video_dict['video_type'] = Video.VideoType.TV_EPISODE
|
@csrf_exempt
|
||||||
video_dict["tv_series_id"] = series.id
|
@api_view(['POST'])
|
||||||
video_dict["episode_number"] = data_dict["EpisodeNumber"]
|
def jellyfin_websocket(request):
|
||||||
video_dict["season_number"] = data_dict["SeasonNumber"]
|
data_dict = request.data
|
||||||
video_dict["tvdb_id"] = data_dict.get("Provider_tvdb", None)
|
media_type = data_dict["ItemType"]
|
||||||
video_dict["tvrage_id"] = data_dict.get("Provider_tvrage", None)
|
# Check if it's a TV Episode
|
||||||
|
video_dict = {
|
||||||
|
"title": data_dict["Name"],
|
||||||
|
"imdb_id": data_dict["Provider_imdb"],
|
||||||
|
"video_type": Video.VideoType.MOVIE,
|
||||||
|
"year": data_dict["Year"],
|
||||||
|
}
|
||||||
|
if media_type == 'Episode':
|
||||||
|
series_name = data_dict["SeriesName"]
|
||||||
|
series, series_created = Series.objects.get_or_create(name=series_name)
|
||||||
|
|
||||||
video, video_created = Video.objects.get_or_create(**video_dict)
|
video_dict['video_type'] = Video.VideoType.TV_EPISODE
|
||||||
|
video_dict["tv_series_id"] = series.id
|
||||||
|
video_dict["episode_number"] = data_dict["EpisodeNumber"]
|
||||||
|
video_dict["season_number"] = data_dict["SeasonNumber"]
|
||||||
|
video_dict["tvdb_id"] = data_dict.get("Provider_tvdb", None)
|
||||||
|
video_dict["tvrage_id"] = data_dict.get("Provider_tvrage", None)
|
||||||
|
|
||||||
if video_created:
|
video, video_created = Video.objects.get_or_create(**video_dict)
|
||||||
video.overview = data_dict["Overview"]
|
|
||||||
video.tagline = data_dict["Tagline"]
|
|
||||||
video.run_time_ticks = data_dict["RunTimeTicks"]
|
|
||||||
video.run_time = data_dict["RunTime"]
|
|
||||||
video.save()
|
|
||||||
|
|
||||||
# Now we run off a scrobble
|
if video_created:
|
||||||
timestamp = parse(data_dict["UtcTimestamp"])
|
video.overview = data_dict["Overview"]
|
||||||
scrobble_dict = {
|
video.tagline = data_dict["Tagline"]
|
||||||
'video_id': video.id,
|
video.run_time_ticks = data_dict["RunTimeTicks"]
|
||||||
'user_id': request.user.id,
|
video.run_time = data_dict["RunTime"]
|
||||||
'in_progress': True,
|
video.save()
|
||||||
}
|
|
||||||
|
|
||||||
existing_finished_scrobble = (
|
# Now we run off a scrobble
|
||||||
Scrobble.objects.filter(
|
timestamp = parse(data_dict["UtcTimestamp"])
|
||||||
video=video, user_id=request.user.id, in_progress=False
|
scrobble_dict = {
|
||||||
)
|
'video_id': video.id,
|
||||||
.order_by('-modified')
|
'user_id': request.user.id,
|
||||||
.first()
|
'in_progress': True,
|
||||||
|
}
|
||||||
|
|
||||||
|
existing_finished_scrobble = (
|
||||||
|
Scrobble.objects.filter(
|
||||||
|
video=video, user_id=request.user.id, in_progress=False
|
||||||
)
|
)
|
||||||
|
.order_by('-modified')
|
||||||
|
.first()
|
||||||
|
)
|
||||||
|
|
||||||
minutes_from_now = timezone.now() + timedelta(minutes=15)
|
minutes_from_now = timezone.now() + timedelta(minutes=15)
|
||||||
|
|
||||||
if (
|
if (
|
||||||
existing_finished_scrobble
|
existing_finished_scrobble
|
||||||
and existing_finished_scrobble.modified < minutes_from_now
|
and existing_finished_scrobble.modified < minutes_from_now
|
||||||
):
|
):
|
||||||
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)
|
|
||||||
|
|
||||||
scrobble, scrobble_created = Scrobble.objects.get_or_create(
|
|
||||||
**scrobble_dict
|
|
||||||
)
|
)
|
||||||
|
return Response(video_dict, status=status.HTTP_204_NO_CONTENT)
|
||||||
|
|
||||||
if scrobble_created:
|
scrobble, scrobble_created = Scrobble.objects.get_or_create(
|
||||||
# If we newly created this, capture the client we're watching from
|
**scrobble_dict
|
||||||
scrobble.source = data_dict['ClientName']
|
)
|
||||||
scrobble.source_id = data_dict['MediaSourceId']
|
|
||||||
else:
|
|
||||||
last_tick = scrobble.playback_position_ticks
|
|
||||||
|
|
||||||
# Update a found scrobble with new position and timestamp
|
if scrobble_created:
|
||||||
scrobble.playback_position_ticks = data_dict["PlaybackPositionTicks"]
|
# If we newly created this, capture the client we're watching from
|
||||||
scrobble.playback_position = data_dict["PlaybackPosition"]
|
scrobble.source = data_dict['ClientName']
|
||||||
scrobble.timestamp = parse(data_dict["UtcTimestamp"])
|
scrobble.source_id = data_dict['MediaSourceId']
|
||||||
scrobble.is_paused = data_dict["IsPaused"] in TRUTHY_VALUES
|
else:
|
||||||
|
last_tick = scrobble.playback_position_ticks
|
||||||
|
|
||||||
|
# Update a found scrobble with new position and timestamp
|
||||||
|
scrobble.playback_position_ticks = data_dict["PlaybackPositionTicks"]
|
||||||
|
scrobble.playback_position = data_dict["PlaybackPosition"]
|
||||||
|
scrobble.timestamp = parse(data_dict["UtcTimestamp"])
|
||||||
|
scrobble.is_paused = data_dict["IsPaused"] in TRUTHY_VALUES
|
||||||
|
scrobble.save()
|
||||||
|
|
||||||
|
# If we hit our completion threshold, save it and get ready
|
||||||
|
# to scrobble again if we re-watch this.
|
||||||
|
if scrobble.percent_played >= getattr(
|
||||||
|
settings, "PERCENT_FOR_COMPLETION", 95
|
||||||
|
):
|
||||||
|
scrobble.in_progress = False
|
||||||
|
scrobble.playback_position_ticks = video.run_time_ticks
|
||||||
scrobble.save()
|
scrobble.save()
|
||||||
|
|
||||||
# If we hit our completion threshold, save it and get ready
|
if scrobble.percent_played % 5 == 0:
|
||||||
# to scrobble again if we re-watch this.
|
logger.info(f"You are {scrobble.percent_played}% through {video}")
|
||||||
if scrobble.percent_played >= getattr(
|
|
||||||
settings, "PERCENT_FOR_COMPLETION", 95
|
|
||||||
):
|
|
||||||
scrobble.in_progress = False
|
|
||||||
scrobble.playback_position_ticks = video.run_time_ticks
|
|
||||||
scrobble.save()
|
|
||||||
|
|
||||||
if scrobble.percent_played % 5 == 0:
|
return Response(video_dict, status=status.HTTP_201_CREATED)
|
||||||
logger.info(f"You are {scrobble.percent_played}% through {video}")
|
|
||||||
|
|
||||||
return Response(video_dict, status=status.HTTP_201_CREATED)
|
|
||||||
|
|||||||
@ -21,10 +21,9 @@ urlpatterns = [
|
|||||||
path("admin/", admin.site.urls),
|
path("admin/", admin.site.urls),
|
||||||
path("accounts/", include("allauth.urls")),
|
path("accounts/", include("allauth.urls")),
|
||||||
# path("api-auth/", include("rest_framework.urls")),
|
# path("api-auth/", include("rest_framework.urls")),
|
||||||
# path("api/v1/", include(router.urls)),
|
|
||||||
# path("movies/", include(movies, namespace="movies")),
|
# path("movies/", include(movies, namespace="movies")),
|
||||||
# path("shows/", include(shows, namespace="shows")),
|
# path("shows/", include(shows, namespace="shows")),
|
||||||
path("scrobbles/", include(scrobble_urls, namespace="scrobbles")),
|
path("api/v1/scrobbles/", include(scrobble_urls, namespace="scrobbles")),
|
||||||
path("", RecentScrobbleList.as_view(), name="home"),
|
path("", RecentScrobbleList.as_view(), name="home"),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user