-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #112 from bralbral/kick
kick
- Loading branch information
Showing
9 changed files
with
250 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 6 additions & 1 deletion
7
src/scheduler/jobs/telegram_notify_job/data_fetcher/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
from .kick import async_kick_fetch_livestreams | ||
from .twitch import async_twitch_fetch_livestreams | ||
from .youtube import async_youtube_fetch_livestreams | ||
|
||
__all__ = ["async_twitch_fetch_livestreams", "async_youtube_fetch_livestreams"] | ||
__all__ = [ | ||
"async_kick_fetch_livestreams", | ||
"async_twitch_fetch_livestreams", | ||
"async_youtube_fetch_livestreams", | ||
] |
4 changes: 4 additions & 0 deletions
4
src/scheduler/jobs/telegram_notify_job/data_fetcher/kick/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from .fetcher import async_kick_fetch_livestreams | ||
|
||
|
||
__all__ = ["async_kick_fetch_livestreams"] |
4 changes: 4 additions & 0 deletions
4
src/scheduler/jobs/telegram_notify_job/data_fetcher/kick/constants.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
KICK_API_URL = "https://kick.com/api/v2/channels/" | ||
|
||
|
||
__all__ = ["KICK_API_URL"] |
97 changes: 97 additions & 0 deletions
97
src/scheduler/jobs/telegram_notify_job/data_fetcher/kick/fetcher.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import asyncio | ||
import operator | ||
from datetime import datetime | ||
from typing import Optional | ||
|
||
from aiohttp import ClientSession | ||
from dateutil.tz import tzutc | ||
|
||
from .constants import KICK_API_URL | ||
from src.db.models import ChannelModel | ||
from src.logger import logger | ||
from src.scheduler.jobs.telegram_notify_job.data_fetcher.utils import make_time_readable | ||
from src.scheduler.jobs.telegram_notify_job.dto import ErrorVideoInfo | ||
from src.scheduler.jobs.telegram_notify_job.dto import VideoInfo | ||
from src.utils import extract_kick_username | ||
|
||
|
||
async def async_fetch_livestream( | ||
channel: ChannelModel, session: ClientSession | ||
) -> Optional[VideoInfo] | ErrorVideoInfo: | ||
""" | ||
:param session: | ||
:param channel: | ||
:return: | ||
""" | ||
await logger.ainfo(channel.model_dump_json()) | ||
|
||
live_stream = None | ||
try: | ||
|
||
username = extract_kick_username(channel.url) | ||
if not username: | ||
raise Exception(f"Cannot extract username for {channel.url}") | ||
|
||
async with session.get(f"{KICK_API_URL}{username}") as resp: | ||
|
||
raw_data = await resp.json() | ||
|
||
livestream: Optional[dict] = raw_data.get("livestream", None) | ||
if livestream: | ||
is_live = livestream.get("is_live", None) | ||
|
||
if is_live: | ||
|
||
concurrent_view_count = livestream["viewer_count"] | ||
|
||
duration = make_time_readable( | ||
( | ||
datetime.now(tz=tzutc()) | ||
- datetime.strptime( | ||
livestream["start_time"], "%Y-%m-%d %H:%M:%S" | ||
) | ||
).seconds | ||
) | ||
|
||
live_stream = VideoInfo( | ||
url=channel.url, | ||
label=channel.label, | ||
concurrent_view_count=concurrent_view_count, | ||
duration=duration, | ||
) | ||
|
||
except Exception as ex: | ||
await logger.aerror(f"Fetching info error: {channel.url} {ex}") | ||
return ErrorVideoInfo(channel=channel.model_dump(), ex_message=str(ex)) | ||
|
||
return live_stream | ||
|
||
|
||
async def async_kick_fetch_livestreams( | ||
channels: list[ChannelModel], | ||
) -> tuple[list[VideoInfo], list[ErrorVideoInfo]]: | ||
""" | ||
:param channels: | ||
:return: | ||
""" | ||
|
||
async with ClientSession() as client: | ||
tasks = [ | ||
async_fetch_livestream(channel=channel, session=client) | ||
for channel in channels | ||
] | ||
|
||
data = await asyncio.gather(*tasks) | ||
|
||
errors = [stream for stream in data if isinstance(stream, ErrorVideoInfo)] | ||
|
||
live_streams = [stream for stream in data if isinstance(stream, VideoInfo)] | ||
|
||
live_streams = sorted( | ||
live_streams, key=operator.attrgetter("concurrent_view_count"), reverse=True | ||
) | ||
|
||
return live_streams, errors | ||
|
||
|
||
__all__ = ["async_kick_fetch_livestreams"] |
Oops, something went wrong.