Skip to content

Commit

Permalink
[Sites] Use _NAME attribute and improve class structure
Browse files Browse the repository at this point in the history
  • Loading branch information
xenova committed Feb 11, 2021
1 parent b7a37ab commit ea6d732
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 45 deletions.
6 changes: 4 additions & 2 deletions chat_downloader/chat_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ def get_chat(self, url=None,
for k, v in original_params.items():
params[k] = self.sessions[site.__name__].get_site_value(v)

log('info', 'Site: {}'.format(self.sessions[site.__name__]))
log('info', 'Site: {}'.format(
self.sessions[site.__name__]._NAME))
log('debug', 'Parameters: {}'.format(params))
info = self.sessions[site.__name__].get_chat(**params)
if isinstance(max_messages, int):
Expand All @@ -138,7 +139,8 @@ def get_chat(self, url=None,
info.site = self.sessions[site.__name__]

formatter = ItemFormatter(params['format_file'])
info.format = lambda x: formatter.format(x, format_name=params['format'])
info.format = lambda x: formatter.format(
x, format_name=params['format'])

return info

Expand Down
41 changes: 19 additions & 22 deletions chat_downloader/sites/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
timed_input
)


class Remapper():
def __init__(self, new_key=None, remap_function=None, to_unpack=False):
if new_key is not None and to_unpack:
Expand Down Expand Up @@ -243,16 +244,29 @@ class BaseChatDownloader:
# is_stackable
# sub_message

_NAME = None
_VALID_URL = None

_SITE_DEFAULT_PARAMS = {
# MAY NOT specify message_types. must always be empty
'message_groups': ['messages'],
'format': 'default',
}

_NAME = None
# For general tests (non-site specific)
_TESTS = [
{
'name': 'Get a certain number of messages from a livestream.',
'params': {
'url': 'https://www.youtube.com/watch?v=5qap5aO4i9A',
'max_messages': 10
},

def __str__(self):
return ''
'expected_result': {
'messages_condition': lambda messages: len(messages) <= 10,
}
}
]

@staticmethod
def must_add_item(item, message_groups_dict, messages_groups_to_add, messages_types_to_add):
Expand Down Expand Up @@ -290,7 +304,7 @@ def remap(info, remapping_dict, remap_key, remap_input, keep_unknown_keys=False,

if remap: # A matching 'remapping' has been found, apply this remapping
if isinstance(remap, Remapper):
new_key = remap.new_key# or remap_key
new_key = remap.new_key # or remap_key

# Perform transformation
if remap.remap_function: # Has a remap function
Expand Down Expand Up @@ -396,23 +410,6 @@ def _session_get_json(self, url, **kwargs):
webpage_title = get_title_of_webpage(s.text)
raise UnexpectedHTML(webpage_title, s.text)

_VALID_URL = None

# For general tests (non-site specific)
_TESTS = [
{
'name': 'Get a certain number of messages from a livestream.',
'params': {
'url': 'https://www.youtube.com/watch?v=5qap5aO4i9A',
'max_messages': 10
},

'expected_result': {
'messages_condition': lambda messages: len(messages) <= 10,
}
}
]

def get_site_value(self, v):
if isinstance(v, SiteDefault):
return self._SITE_DEFAULT_PARAMS.get(
Expand Down Expand Up @@ -466,7 +463,7 @@ def move_to_dict(info, dict_name, replace_key=None, create_when_empty=False, *in

if dict_name in info:
info[dict_name].update(new_dict)
elif create_when_empty or new_dict != {}: # dict_name not in info
elif create_when_empty or new_dict != {}: # dict_name not in info
info[dict_name] = new_dict

return new_dict
Expand Down
7 changes: 2 additions & 5 deletions chat_downloader/sites/facebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,7 @@ def __init__(self, **kwargs):
'lsd': lsd,
}

def __str__(self):
return 'facebook.com'

_NAME = 'facebook.com'
# Regex provided by youtube-dl
_VALID_URL = r'''(?x)
(?:
Expand All @@ -106,10 +104,9 @@ def __str__(self):

_VIDEO_PAGE_TAHOE_TEMPLATE = _FB_HOMEPAGE + \
'/video/tahoe/async/{}/?chain=true&isvideo=true&payloadtype=primary'
_STRIP_TEXT = 'for (;;);'

def _parse_fb_json(self, response):
text_to_parse = remove_prefixes(response.text, self._STRIP_TEXT)
text_to_parse = remove_prefixes(response.text, 'for (;;);')
return json.loads(text_to_parse)

_VOD_COMMENTS_API = _FB_HOMEPAGE + '/videos/vodcomments/'
Expand Down
22 changes: 11 additions & 11 deletions chat_downloader/sites/twitch.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,15 @@ class TwitchChatDownloader(BaseChatDownloader):
_BADGE_INFO_URL = 'https://badges.twitch.tv/v1/badges/global/display'
# TODO add local version of badge list?

_SUBSCRIBER_BADGE_INFO = {} # local cache for subscriber badge info
_SUBSCRIBER_BADGE_URL = 'https://badges.twitch.tv/v1/badges/channels/{}/display'

_NAME = 'Twitch.tv'
_NAME = 'twitch.tv'

def __init__(self, **kwargs):
super().__init__(**kwargs)

TwitchChatDownloader._BADGE_INFO = self._session_get_json(
self._BADGE_INFO_URL).get('badge_sets') or {}

def __str__(self):
return 'twitch.tv'
# Only get badge info if not already retrieved
if not TwitchChatDownloader._BADGE_INFO:
TwitchChatDownloader._BADGE_INFO = self._session_get_json(
self._BADGE_INFO_URL).get('badge_sets') or {}

_TESTS = [
# Live
Expand Down Expand Up @@ -285,14 +281,15 @@ def parse_message_info(message):
)

for emote_id in message_emotes:
message_emotes[emote_id]['locations'] = ','.join(locations[emote_id])
message_emotes[emote_id]['locations'] = ','.join(
locations[emote_id])

message_info = {
'message': message_text,
'author_colour': message.get('user_color'),
'author_badges': message.get('user_badges') or [],
'user_notice_params': message.get('user_notice_params') or {},
}
}

if message_emotes:
message_info['emotes'] = list(message_emotes.values())
Expand Down Expand Up @@ -783,6 +780,9 @@ def parse_emotes(text):
_MESSAGE_GROUPS[message_group] = []
_MESSAGE_GROUPS[message_group] += list(value.values())

_SUBSCRIBER_BADGE_INFO = {} # local cache for subscriber badge info
_SUBSCRIBER_BADGE_URL = 'https://badges.twitch.tv/v1/badges/channels/{}/display'

def _update_subscriber_badge_info(self, channel_id):
# print('updated sub badges')
url = self._SUBSCRIBER_BADGE_URL.format(channel_id)
Expand Down
6 changes: 1 addition & 5 deletions chat_downloader/sites/youtube.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,7 @@ class YouTubeChatDownloader(BaseChatDownloader):
def __init__(self, **kwargs):
super().__init__(**kwargs)

_NAME = 'YouTube'

def __str__(self):
return 'youtube.com'
# return 'youtube.com'
_NAME = 'youtube.com'

_SITE_DEFAULT_PARAMS = {
'format': 'youtube',
Expand Down

0 comments on commit ea6d732

Please sign in to comment.