[videos] Add preliminary Twitch video support

This commit is contained in:
2026-04-02 11:10:39 -04:00
parent 1bf558938d
commit b6af201ba3
10 changed files with 430 additions and 24 deletions

View File

@ -35,8 +35,8 @@ from scrobbles.notifications import ScrobbleNtfyNotification
from scrobbles.utils import (
convert_to_seconds,
extract_domain,
remove_last_part,
next_url_if_exists,
remove_last_part,
)
from sports.models import SportEvent
from sports.thesportsdb import lookup_event_from_thesportsdb
@ -188,6 +188,15 @@ def web_scrobbler_scrobble_media(
return video.scrobble_for_user(user_id, status, source="Web Scrobbler")
def twitch_scrobble_channel(
twitch_handle: str, user_id: int, status: str = "started"
) -> Optional[Scrobble]:
from videos.models import Channel
channel = Channel.find_or_create(twitch_handle)
return channel.scrobble_for_user(user_id, status, source="Twitch")
def manual_scrobble_video(
video_id: str,
user_id: int,
@ -629,6 +638,8 @@ def manual_scrobble_from_url(
item_id = url
elif content_key == "-i" and "title/tt" in url:
item_id = "tt" + str(item_id)
elif content_key == "-h" and "twitch.tv" in url:
item_id = url
scrobble_fn = MANUAL_SCROBBLE_FNS[content_key]
return eval(scrobble_fn)(item_id, user_id, source=source, action=action)
@ -984,6 +995,44 @@ def manual_scrobble_webpage(
return scrobble
def manual_scrobble_twitch_channel(
url: str,
user_id: int,
source: str = "Manual",
action: Optional[str] = None,
):
from videos.models import Channel
handle = (
url.replace("https://www.twitch.tv/", "")
.replace("https://twitch.tv/", "")
.rstrip("/")
)
channel = Channel.find_or_create(handle)
scrobble_dict = {
"user_id": user_id,
"timestamp": timezone.now(),
"playback_position_seconds": 0,
"source": source,
}
logger.info(
"[vrobbler-scrobble] twitch channel scrobble request received",
extra={
"channel_id": channel.id,
"handle": handle,
"user_id": user_id,
"scrobble_dict": scrobble_dict,
"media_type": Scrobble.MediaType.CHANNEL,
},
)
scrobble = Scrobble.create_or_update(channel, user_id, scrobble_dict)
return scrobble
def gpslogger_scrobble_location(data_dict: dict, user_id: int) -> Scrobble:
location = GeoLocation.find_or_create(data_dict)