refactor helpers and utils, extract SearchMixin from BrowsingMixin

This commit is contained in:
sigma67 2022-05-05 14:26:45 +02:00
commit f4ff4ba287
No known key found for this signature in database
GPG key ID: 1545A01D4474BAF0
19 changed files with 400 additions and 378 deletions

View file

@ -3,39 +3,10 @@ import json
from http.cookies import SimpleCookie
from hashlib import sha1
import time
from datetime import date
from functools import wraps
import locale
from ytmusicapi.constants import *
def prepare_like_endpoint(rating):
if rating == 'LIKE':
return 'like/like'
elif rating == 'DISLIKE':
return 'like/dislike'
elif rating == 'INDIFFERENT':
return 'like/removelike'
else:
return None
def validate_order_parameter(order):
orders = ['a_to_z', 'z_to_a', 'recently_added']
if order and order not in orders:
raise Exception(
"Invalid order provided. Please use one of the following orders or leave out the parameter: "
+ ', '.join(orders))
def prepare_order_params(order):
orders = ['a_to_z', 'z_to_a', 'recently_added']
if order is not None:
# determine order_params via `.contents.singleColumnBrowseResultsRenderer.tabs[0].tabRenderer.content.sectionListRenderer.contents[1].itemSectionRenderer.header.itemSectionTabbedHeaderRenderer.endItems[1].dropdownRenderer.entries[].dropdownItemRenderer.onSelectCommand.browseEndpoint.params` of `/youtubei/v1/browse` response
order_params = ['ggMGKgQIARAA', 'ggMGKgQIARAB', 'ggMGKgQIABAB']
return order_params[orders.index(order)]
def initialize_headers():
return {
"user-agent": USER_AGENT,
@ -69,13 +40,6 @@ def get_visitor_id(request_func):
return {'X-Goog-Visitor-Id': visitor_id}
def html_to_txt(html_text):
tags = re.findall("<[^>]+>", html_text)
for tag in tags:
html_text = html_text.replace(tag, '')
return html_text
def sapisid_from_cookie(raw_cookie):
cookie = SimpleCookie()
cookie.load(raw_cookie)
@ -91,10 +55,6 @@ def get_authorization(auth):
return "SAPISIDHASH " + unix_timestamp + "_" + sha_1.hexdigest()
def get_datestamp():
return (date.today() - date.fromtimestamp(0)).days
def to_int(string):
number_string = re.sub('[^\\d]', '', string)
try:
@ -105,24 +65,7 @@ def to_int(string):
return int_value
def parse_duration(duration):
if duration is None:
return duration
mapped_increments = zip([1, 60, 3600], reversed(duration.split(":")))
seconds = sum(multiplier * int(time) for multiplier, time in mapped_increments)
return seconds
def sum_total_duration(item):
return sum(
[track['duration_seconds'] if 'duration_seconds' in track else 0 for track in item['tracks']]
)
def i18n(method):
@wraps(method)
def _impl(self, *method_args, **method_kwargs):
method.__globals__['_'] = self.lang.gettext
return method(self, *method_args, **method_kwargs)
return _impl
return sum([
track['duration_seconds'] if 'duration_seconds' in track else 0 for track in item['tracks']
])