feature: add_history_item (closes #295) (OzymandiasTheGreat/mopidy-ytmusic/issues/58)

This commit is contained in:
sigma67 2022-12-07 18:07:47 +01:00
commit d0ec2bbf72
No known key found for this signature in database
GPG key ID: 1C314227DF0103D0
6 changed files with 30 additions and 12 deletions

View file

@ -53,6 +53,7 @@ Library
.. automethod:: YTMusic.get_library_subscriptions
.. automethod:: YTMusic.get_liked_songs
.. automethod:: YTMusic.get_history
.. automethod:: YTMusic.add_history_item
.. automethod:: YTMusic.remove_history_items
.. automethod:: YTMusic.rate_song
.. automethod:: YTMusic.edit_song_library_status
@ -77,4 +78,4 @@ Uploads
.. automethod:: YTMusic.get_library_upload_artist
.. automethod:: YTMusic.get_library_upload_album
.. automethod:: YTMusic.upload_song
.. automethod:: YTMusic.delete_upload_entity
.. automethod:: YTMusic.delete_upload_entity

View file

@ -277,11 +277,13 @@ class TestYTMusic(unittest.TestCase):
songs = self.yt_auth.get_history()
self.assertGreater(len(songs), 0)
@unittest.skip
def test_remove_history_items(self):
def test_manipulate_history_items(self):
song = self.yt_auth.get_song(sample_video)
response = self.yt_auth.add_history_item(song)
self.assertEqual(response.status_code, 204)
songs = self.yt_auth.get_history()
response = self.yt_auth.remove_history_items(
[songs[0]['feedbackToken'], songs[1]['feedbackToken']])
self.assertGreater(len(songs), 0)
response = self.yt_auth.remove_history_items([songs[0]['feedbackToken']])
self.assertIn('feedbackResponses', response)
def test_rate_song(self):

View file

@ -32,7 +32,7 @@ def initialize_context():
def get_visitor_id(request_func):
response = request_func(YTM_DOMAIN)
matches = re.findall(r'ytcfg\.set\s*\(\s*({.+?})\s*\)\s*;', response)
matches = re.findall(r'ytcfg\.set\s*\(\s*({.+?})\s*\)\s*;', response.text)
visitor_id = ""
if len(matches) > 0:
ytcfg = json.loads(matches[0])

View file

@ -338,7 +338,7 @@ class BrowsingMixin:
"""
params = {"list": audioPlaylistId}
response = self._send_get_request(YTM_DOMAIN + "/playlist", params)
matches = re.findall(r"\"MPRE.+?\"", response)
matches = re.findall(r"\"MPRE.+?\"", response.text)
browse_id = None
if len(matches) > 0:
browse_id = matches[0].encode('utf8').decode('unicode-escape').strip('"')
@ -728,7 +728,7 @@ class BrowsingMixin:
:return: URL to `base.js`
"""
response = self._send_get_request(url=YTM_DOMAIN)
match = re.search(r'jsUrl"\s*:\s*"([^"]+)"', response)
match = re.search(r'jsUrl"\s*:\s*"([^"]+)"', response.text)
if match is None:
raise Exception("Could not identify the URL for base.js player.")
@ -746,7 +746,7 @@ class BrowsingMixin:
if url is None:
url = self.get_basejs_url()
response = self._send_get_request(url=url)
match = re.search(r"signatureTimestamp[:=](\d+)", response)
match = re.search(r"signatureTimestamp[:=](\d+)", response.text)
if match is None:
raise Exception("Unable to identify the signatureTimestamp.")

View file

@ -1,3 +1,4 @@
from random import randint
from ytmusicapi.continuations import *
from ._utils import *
from ytmusicapi.parsers.browsing import *
@ -217,6 +218,20 @@ class LibraryMixin:
return songs
def add_history_item(self, song):
"""
Add an item to the account's history using the playbackTracking URI
obtained from :py:func:`get_song`.
:param song: Dictionary as returned by :py:func:`get_song`
:return: Full response. response.status_code is 204 if successful
"""
url = song["playbackTracking"]["videostatsPlaybackUrl"]["baseUrl"]
CPNA = ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_")
cpn = "".join((CPNA[randint(0, 256) & 63] for _ in range(0, 16)))
params = {"ver": 2, "c": "WEB_REMIX", "cpn": cpn}
return self._send_get_request(url, params)
def remove_history_items(self, feedbackTokens: List[str]) -> Dict: # pragma: no cover
"""
Remove an item from the account's history. This method does currently not work with brand accounts

View file

@ -104,8 +104,8 @@ class YTMusic(BrowsingMixin, SearchMixin, WatchMixin, ExploreMixin, LibraryMixin
locale_dir = os.path.abspath(os.path.dirname(__file__)) + os.sep + 'locales'
supported_languages = [f for f in next(os.walk(locale_dir))[1]]
if language not in supported_languages:
raise Exception("Language not supported. Supported languages are " +
(', '.join(supported_languages)) + ".")
raise Exception("Language not supported. Supported languages are "
+ (', '.join(supported_languages)) + ".")
self.language = language
try:
locale.setlocale(locale.LC_ALL, self.language)
@ -150,7 +150,7 @@ class YTMusic(BrowsingMixin, SearchMixin, WatchMixin, ExploreMixin, LibraryMixin
headers=self.headers,
proxies=self.proxies,
cookies=self.cookies)
return response.text
return response
def _check_auth(self):
if not self.auth: