Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for ytmusicapi 1.0.0 #73

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions mopidy_ytmusic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def get_default_config(self):
def get_config_schema(self):
schema = super().get_config_schema()
schema["auth_json"] = config.Path(optional=True)
schema["oauth_json"] = config.Path(optional=True)
schema["auto_playlist_refresh"] = config.Integer(
minimum=0, optional=True
)
Expand Down
14 changes: 11 additions & 3 deletions mopidy_ytmusic/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
TITLE_TEXT,
nav,
)
from ytmusicapi.ytmusic import YTMusic
from ytmusicapi import YTMusic

from mopidy_ytmusic import logger

Expand All @@ -36,6 +36,7 @@ def __init__(self, config, audio):
self.audio = audio
self.uri_schemes = ["ytmusic"]
self.auth = False
self.oauth = False

self._auto_playlist_refresh_rate = (
config["ytmusic"]["auto_playlist_refresh"] * 60
Expand All @@ -61,8 +62,14 @@ def __init__(self, config, audio):
self._ytmusicapi_auth_json = config["ytmusic"]["auth_json"]
self.auth = True

if self.auth:
self.api = YTMusic(self._ytmusicapi_auth_json)
if config["ytmusic"]["oauth_json"]:
self._ytmusicapi_oauth_json = config["ytmusic"]["oauth_json"]
self.oauth = True

if self.auth and not self.oauth:
self.api = YTMusic(auth=self._ytmusicapi_auth_json)
elif self.oauth:
self.api = YTMusic(auth=self._ytmusicapi_oauth_json)
else:
self.api = YTMusic()

Expand Down Expand Up @@ -103,6 +110,7 @@ def _refresh_youtube_player(self):
def _get_youtube_player(self):
# Refresh our js player URL so YDL can decode the signature correctly.
try:
self.api.headers.pop('filepath', None)
response = requests.get(
"https://music.youtube.com",
headers=self.api.headers,
Expand Down
38 changes: 19 additions & 19 deletions mopidy_ytmusic/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,20 @@ class SetupCommand(commands.Command):
help = "Generate auth.json"

def run(self, args, config):
from ytmusicapi.ytmusic import YTMusic
from ytmusicapi.setup import setup_oauth

filepath = input(
"Enter the path where you want to save auth.json [default=current dir]: "
)
if not filepath:
filepath = os.getcwd()
path = Path(filepath + "/auth.json")
path = Path(filepath + "/oauth.json")
print('Using "' + str(path) + '"')
if path.exists():
print("File already exists!")
return 1
print(
"Open Youtube Music, open developer tools (F12), go to Network tab,"
)
print(
'right click on a POST request and choose "Copy request headers".'
)
print("Then paste (CTRL+SHIFT+V) them here and press CTRL+D.")
try:
print(YTMusic.setup(filepath=str(path)))
setup_oauth(filepath=str(path))
except Exception:
logger.exception("YTMusic setup failed")
return 1
Expand All @@ -54,22 +47,29 @@ class ReSetupCommand(commands.Command):
help = "Regenerate auth.json"

def run(self, args, config):
from ytmusicapi.ytmusic import YTMusic
from ytmusicapi import YTMusic

path = config["ytmusic"]["auth_json"]
usingOauth = False
if not path:
logger.error("auth_json path not defined in config")
return 1
if config["ytmusic"]["oauth_json"] is not None:
path = config["ytmusic"]["oauth_json"]
usingOauth = True
print('Updating credentials in "' + str(path) + '"')
print(
"Open Youtube Music, open developer tools (F12), go to Network tab,"
)
print(
'right click on a POST request and choose "Copy request headers".'
)
print("Then paste (CTRL+SHIFT+V) them here and press CTRL+D.")
if not usingOauth:
print(
"Open Youtube Music, open developer tools (F12), go to Network tab,"
)
print(
'right click on a POST request and choose "Copy request headers".'
)
print("Then paste (CTRL+SHIFT+V) them here and press CTRL+D.")
else:
print("Updating via oauth, follow the instructions from ytmusicapi")
try:
print(YTMusic.setup(filepath=str(path)))
print(YTMusic(auth=path))
except Exception:
logger.exception("YTMusic setup failed")
return 1
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

install_requires = [
"Mopidy>=3,<4",
"pytube>=12.1.0,<13.0.0",
"ytmusicapi>=0.22.0,<0.30.0",
"pytube>=12.1.0",
"ytmusicapi>=0.22.0,<2.0.0",
]

entry_points = {"mopidy.ext": ["ytmusic = mopidy_ytmusic:Extension"]}
Expand Down