Major refactor of codebase.

This commit is contained in:
2020-04-11 00:30:24 +03:00
parent fd5d05232f
commit 1022ce353c
33 changed files with 1408 additions and 963 deletions

View File

@ -55,3 +55,22 @@ def bisect_left(a, x, lo=0, hi=None, key=None):
# Create aliases
bisect = bisect_right
def group_by(data, key):
"""
Groups the given data into a dictionary matching the structure { key : [values] }
:param data: Iterable data to be grouped
:param key: Key used to group the data
:return: A dictionary containing the grouped data
"""
result = {}
for entry in data:
entry_key = key(entry)
if entry_key not in result:
result[entry_key] = [entry]
else:
result[entry_key].append(entry)
return result

View File

@ -1,49 +0,0 @@
from django.conf import settings
from external.pytaw.pytaw.youtube import YouTube, Channel, Playlist, PlaylistItem, Thumbnail, InvalidURL, Resource, Video
from typing import Optional
class YoutubeAPI(YouTube):
@staticmethod
def build_public() -> 'YoutubeAPI':
from YtManagerApp.management.appconfig import appconfig
return YoutubeAPI(key=appconfig.youtube_api_key)
# @staticmethod
# def build_oauth() -> 'YoutubeAPI':
# flow =
# credentials =
# service = build(API_SERVICE_NAME, API_VERSION, credentials)
def default_thumbnail(resource: Resource) -> Optional[Thumbnail]:
"""
Gets the default thumbnail for a resource.
Searches in the list of thumbnails for one with the label 'default', or takes the first one.
:param resource:
:return:
"""
thumbs = getattr(resource, 'thumbnails', None)
if thumbs is None or len(thumbs) <= 0:
return None
return next(
(i for i in thumbs if i.id == 'default'),
thumbs[0]
)
def best_thumbnail(resource: Resource) -> Optional[Thumbnail]:
"""
Gets the best thumbnail available for a resource.
:param resource:
:return:
"""
thumbs = getattr(resource, 'thumbnails', None)
if thumbs is None or len(thumbs) <= 0:
return None
return max(thumbs, key=lambda t: t.width * t.height)