Skip to content

Commit

Permalink
youtubeify
Browse files Browse the repository at this point in the history
  • Loading branch information
natumbri committed Sep 16, 2022
1 parent fa2e219 commit 7c6ee57
Show file tree
Hide file tree
Showing 16 changed files with 94 additions and 26 deletions.
2 changes: 1 addition & 1 deletion mopidy_spotify/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import threading

import pykka
import spotify
from mopidy import backend, httpclient

import spotify
from mopidy_spotify import Extension, library, playback, playlists, web

logger = logging.getLogger(__name__)
Expand Down
2 changes: 1 addition & 1 deletion mopidy_spotify/browse.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import logging

import spotify
from mopidy import models

import spotify
from mopidy_spotify import countries, playlists, translator
from mopidy_spotify.utils import flatten

Expand Down
1 change: 1 addition & 0 deletions mopidy_spotify/distinct.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging

import spotify

from mopidy_spotify import search

logger = logging.getLogger(__name__)
Expand Down
4 changes: 4 additions & 0 deletions mopidy_spotify/lookup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging

import spotify

from mopidy_spotify import browse, playlists, translator, utils
from mopidy_spotify.web import LinkType, WebLink

Expand Down Expand Up @@ -43,8 +44,11 @@ def lookup(config, session, web_client, uri):


def _lookup_track(config, sp_link):

sp_track = sp_link.as_track()

sp_track.load(config["timeout"])

track = translator.to_track(sp_track, bitrate=config["bitrate"])
if track is not None:
yield track
Expand Down
5 changes: 2 additions & 3 deletions mopidy_spotify/playback.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import functools
import collections
import functools
import logging
import threading

from mopidy import audio, backend

import spotify
from mopidy import audio, backend

logger = logging.getLogger(__name__)

Expand Down
2 changes: 1 addition & 1 deletion mopidy_spotify/playlists.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import logging

import spotify
from mopidy import backend

import spotify
from mopidy_spotify import translator, utils

_sp_links = {}
Expand Down
2 changes: 1 addition & 1 deletion mopidy_spotify/search.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import logging
import urllib.parse

import spotify
from mopidy import models

import spotify
from mopidy_spotify import lookup, translator

_SEARCH_TYPES = ["album", "artist", "track"]
Expand Down
80 changes: 72 additions & 8 deletions mopidy_spotify/translator.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import collections
import logging

from mopidy import models

import spotify
from mopidy import models
from mopidy_tubeify.data import flatten
from mopidy_tubeify.yt_matcher import (
search_and_get_best_album,
search_and_get_best_match,
)
from ytmusicapi import YTMusic

logger = logging.getLogger(__name__)
ytmusic = YTMusic()


class memoized: # noqa N801
Expand Down Expand Up @@ -114,11 +120,38 @@ def web_to_album_ref(web_album):
return models.Ref.album(uri=uri, name=web_album.get("name", uri))


def web_to_album_refs(web_albums):
def web_to_yt_album_refs(web_albums):

artists_albumtitles = []
ytm_albums = []
for web_album in web_albums:
# The extra level here is to also support "saved album objects".
web_album = web_album.get("album", web_album)
ref = web_to_album_ref(web_album)
artists = [artist["name"] for artist in web_album["artists"]]
title = web_album["name"]
artists_albumtitles.append((artists, title))
ytm_albums.extend(
[
search_and_get_best_album(artist_albumtitle, ytmusic)
for artist_albumtitle in artists_albumtitles
]
)
return [
models.Ref.track(
uri=f"yt:playlist:{ytm_album['browseId']}", name=ytm_album["title"]
)
for ytm_album in flatten(ytm_albums)
if "browseId" in ytm_album
]


def web_to_album_refs(web_albums):
web_albums_got = []
for web_album in list(web_albums):
# The extra level here is to also support "saved track objects".
web_albums_got.append(web_album.get("album", web_album))

refs = web_to_yt_album_refs(web_albums_got)

for ref in refs:
if ref is not None:
yield ref

Expand Down Expand Up @@ -199,11 +232,42 @@ def web_to_track_ref(web_track, *, check_playable=True):
return models.Ref.track(uri=uri, name=web_track.get("name", uri))


def web_to_yt_track_refs(web_tracks, *, check_playable=True):

web_tracks = [
track for track in web_tracks if valid_web_data(track, "track")
]

tracks = [
{
"song_name": track["name"],
"song_artists": [artist["name"] for artist in track["artists"]],
"song_duration": track["duration_ms"] // 1000,
"isrc": None,
}
for track in web_tracks
]

ytm_tracks = search_and_get_best_match(tracks, ytmusic)

return [
models.Ref.track(
uri=f"yt:video:{ytm_track['videoId']}", name=ytm_track["title"]
)
for ytm_track in ytm_tracks
if "videoId" in ytm_track
]


def web_to_track_refs(web_tracks, *, check_playable=True):
web_tracks_got = []
for web_track in web_tracks:
# The extra level here is to also support "saved track objects".
web_track = web_track.get("track", web_track)
ref = web_to_track_ref(web_track, check_playable=check_playable)
web_tracks_got.append(web_track.get("track", web_track))

refs = web_to_yt_track_refs(web_tracks_got, check_playable=check_playable)

for ref in refs:
if ref is not None:
yield ref

Expand Down
2 changes: 1 addition & 1 deletion mopidy_spotify/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def _request_with_retries(self, method, url, *args, **kwargs):
# instead some endpoints return 200 with no content, or true/false.

# Decide how long to sleep in the next iteration.
backoff_time = backoff_time or (2**i * self._backoff_factor)
backoff_time = backoff_time or (2 ** i * self._backoff_factor)
logger.debug(
f"Retrying {prepared_request.url} in {backoff_time:.3f} "
"seconds."
Expand Down
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from unittest import mock

import pytest
import spotify
from mopidy import backend as backend_api
from mopidy import models

import spotify
from mopidy_spotify import backend, library, utils, playlists, web
from mopidy_spotify import backend, library, playlists, utils, web


@pytest.fixture
Expand Down
2 changes: 1 addition & 1 deletion tests/test_backend.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import threading
from unittest import mock

import spotify
from mopidy import backend as backend_api

import spotify
from mopidy_spotify import backend, library, playback, playlists


Expand Down
3 changes: 1 addition & 2 deletions tests/test_browse.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from unittest import mock

from mopidy import models

import spotify
from mopidy import models


def test_has_a_root_directory(provider):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_playback.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
from unittest import mock

import pytest
import spotify
from mopidy import audio
from mopidy import backend as backend_api
from mopidy import models

import spotify
from mopidy_spotify import backend, playback


Expand Down
2 changes: 1 addition & 1 deletion tests/test_playlists.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from unittest import mock

import pytest
import spotify
from mopidy import backend as backend_api
from mopidy.models import Ref

import spotify
from mopidy_spotify import playlists


Expand Down
2 changes: 1 addition & 1 deletion tests/test_search.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import spotify
from mopidy import models

import spotify
from mopidy_spotify import search


Expand Down
5 changes: 3 additions & 2 deletions tests/test_translator.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import pytest
from mopidy import models
from unittest import mock
from unittest.mock import patch, sentinel

import pytest
import spotify
from mopidy import models

from mopidy_spotify import translator


Expand Down

0 comments on commit 7c6ee57

Please sign in to comment.