Special characters handling
* Fix README for python version * Add discord-friendly unidecode function for channel names (avoiding special characted) * Check if "items" is present before accessing in channel id request response
This commit is contained in:
parent
8d76596ebf
commit
d015927861
10 changed files with 315 additions and 13 deletions
|
|
@ -2,6 +2,9 @@ from __future__ import annotations
|
|||
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import datetime
|
||||
from typing import NewType
|
||||
|
||||
from breadtube_bot.unidecode import unidecode
|
||||
|
||||
|
||||
@dataclass
|
||||
|
|
@ -51,19 +54,27 @@ class ChannelInfo:
|
|||
url=info['url'])
|
||||
|
||||
|
||||
SubscriptionName = NewType('SubscriptionName', str)
|
||||
|
||||
|
||||
@dataclass
|
||||
class SubscriptionInfo:
|
||||
name: str
|
||||
name: SubscriptionName
|
||||
channel_id: str
|
||||
last_update: float
|
||||
channel_info: ChannelInfo | None = None
|
||||
video_list: list[VideoInfo] = field(default_factory=list)
|
||||
|
||||
@staticmethod
|
||||
def discord_compatible_name(text: str) -> SubscriptionName:
|
||||
assert text, 'Channel name cannot be empty'
|
||||
return SubscriptionName(unidecode(text[:100]))
|
||||
|
||||
@staticmethod
|
||||
def from_dict(info: dict) -> SubscriptionInfo:
|
||||
channel_info: dict | None = info.get('channel_info')
|
||||
return SubscriptionInfo(
|
||||
name=info['name'],
|
||||
name=SubscriptionInfo.discord_compatible_name(info['name']),
|
||||
channel_id=info['channel_id'],
|
||||
last_update=info['last_update'],
|
||||
channel_info=ChannelInfo.from_dict(channel_info) if channel_info is not None else None,
|
||||
|
|
@ -91,7 +102,8 @@ class SubscriptionHelper:
|
|||
f', got {len(values)} but expected {len(SUBSCRIPTION_FILE_COLUMNS)}')
|
||||
name = values[0].decode()
|
||||
channel_id = values[1].decode()
|
||||
subscriptions[channel_id] = SubscriptionInfo(name=name, channel_id=channel_id, last_update=0)
|
||||
subscriptions[channel_id] = SubscriptionInfo(
|
||||
name=SubscriptionInfo.discord_compatible_name(name), channel_id=channel_id, last_update=0)
|
||||
return subscriptions
|
||||
|
||||
@staticmethod
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue