Initial commit
This commit is contained in:
commit
aad6fdf150
8 changed files with 188 additions and 0 deletions
9
.envrc.example
Normal file
9
.envrc.example
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
export SPOTIPY_CLIENT_ID= # REPLACE THIS
|
||||||
|
export SPOTIPY_CLIENT_SECRET= # REPLACE THIS
|
||||||
|
export SPOTIPY_REDIRECT_URI= # REPLACE THIS
|
||||||
|
|
||||||
|
export YT_MUSIC_AUTH= # REPLACE THIS
|
||||||
|
export YT_MUSIC_COOKIE= # REPLACE THIS
|
||||||
|
|
||||||
|
export GOOGLE_CLIENT_ID= # REPLACE THIS
|
||||||
|
export GOOGLE_CLIENT_SECRET= # REPLACE THIS
|
||||||
7
.gitignore
vendored
Normal file
7
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
.envrc
|
||||||
|
.venv
|
||||||
|
__pycache__
|
||||||
|
oauth.json
|
||||||
|
out.json
|
||||||
|
resp.json
|
||||||
|
.cache
|
||||||
12
.vscode/launch.json
vendored
Normal file
12
.vscode/launch.json
vendored
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
{
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "Python Debugger: Current File",
|
||||||
|
"type": "debugpy",
|
||||||
|
"request": "launch",
|
||||||
|
"module": "src.main",
|
||||||
|
"console": "integratedTerminal",
|
||||||
|
"justMyCode": false
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
2
requirements.txt
Normal file
2
requirements.txt
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
spotipy==2.25.1
|
||||||
|
ytmusicapi @ git+https://github.com/Goldenfreddy0703/ytmusicapi.git@43971eb7d5609eef0ca0cc0350f44d645eb9242f
|
||||||
9
src/__init__.py
Normal file
9
src/__init__.py
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from typing import Union, List
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Song:
|
||||||
|
name: str
|
||||||
|
artist: Union[str, List[str]]
|
||||||
|
album: str
|
||||||
6
src/main.py
Normal file
6
src/main.py
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
from src.ytmusic import get_all_songs_from_library
|
||||||
|
import json
|
||||||
|
|
||||||
|
all_songs = get_all_songs_from_library()
|
||||||
|
with open("out.json", "w") as fh:
|
||||||
|
fh.write(json.dumps([{"title": x.name, "album": x.album, "artist": x.artist} for x in all_songs], indent=2))
|
||||||
19
src/spotify.py
Normal file
19
src/spotify.py
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
from spotipy import Spotify
|
||||||
|
from spotipy.oauth2 import SpotifyClientCredentials
|
||||||
|
from typing import List, Union
|
||||||
|
|
||||||
|
|
||||||
|
_INSTANCE: Union[Spotify, None]
|
||||||
|
|
||||||
|
|
||||||
|
def get_instance():
|
||||||
|
global _INSTANCE
|
||||||
|
|
||||||
|
if not _INSTANCE:
|
||||||
|
auth_manager = SpotifyClientCredentials()
|
||||||
|
_INSTANCE = Spotify(auth_manager=auth_manager)
|
||||||
|
|
||||||
|
return _INSTANCE
|
||||||
|
|
||||||
|
|
||||||
|
get_instance().user
|
||||||
124
src/ytmusic.py
Normal file
124
src/ytmusic.py
Normal file
|
|
@ -0,0 +1,124 @@
|
||||||
|
from os import environ
|
||||||
|
from typing import List, Union
|
||||||
|
|
||||||
|
from ytmusicapi import OAuthCredentials, YTMusic
|
||||||
|
|
||||||
|
from src import Song
|
||||||
|
|
||||||
|
_INSTANCE: Union[YTMusic, None] = None
|
||||||
|
UNK = "Unknown"
|
||||||
|
|
||||||
|
|
||||||
|
def get_instance() -> YTMusic:
|
||||||
|
"""Gets the YTMusic Singleton Instance
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
Exception: When GOOGLE_CLIENT_ID or GOOGLE_CLIENT_SECRET are not vaild environment variables
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
YTMusic: The existing, or newly created, singleton instance of YTMusic
|
||||||
|
"""
|
||||||
|
global _INSTANCE
|
||||||
|
|
||||||
|
client_id = environ.get("GOOGLE_CLIENT_ID", "")
|
||||||
|
if not client_id:
|
||||||
|
raise Exception("GOOGLE_CLIENT_ID environment variable cannot be empty!")
|
||||||
|
|
||||||
|
client_secret = environ.get("GOOGLE_CLIENT_SECRET", "")
|
||||||
|
if not client_secret:
|
||||||
|
raise Exception("GOOGLE_CLIENT_SECRET environment variable cannot be empty!")
|
||||||
|
|
||||||
|
if not _INSTANCE:
|
||||||
|
_INSTANCE = YTMusic(
|
||||||
|
"oauth.json", # TODO: Get abspath?
|
||||||
|
oauth_credentials=OAuthCredentials(client_id=client_id, client_secret=client_secret),
|
||||||
|
)
|
||||||
|
|
||||||
|
return _INSTANCE
|
||||||
|
|
||||||
|
|
||||||
|
def to_song(raw: dict) -> Song:
|
||||||
|
"""Converts a single raw track from the YTMusic API into our dataclass form
|
||||||
|
Performs parsing and fallback logic to use "Unknown" for any unparsed /
|
||||||
|
empty fields
|
||||||
|
|
||||||
|
Args:
|
||||||
|
raw (dict): The dictionary object for the individual track
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Song: A Song dataclass instance with the fields parsed properly
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __get_field_safe(field: str) -> str:
|
||||||
|
"""Gets a field from the raw dictionary object, returning "Unknown" if
|
||||||
|
the field isn't found in the object or if the field exists but is None
|
||||||
|
|
||||||
|
Args:
|
||||||
|
field (str): The field to lookup and validate
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
str: The field or "Unknown" as a fallback
|
||||||
|
"""
|
||||||
|
if field not in raw:
|
||||||
|
return UNK
|
||||||
|
return raw[field] or UNK
|
||||||
|
|
||||||
|
def __get_artists_safe(field: str) -> Union[List[str], str]:
|
||||||
|
"""Parses the artists in a relatively safe way, slightly different process
|
||||||
|
than that of the other two fields because a track may have multiple
|
||||||
|
artists and we want to parse that. If there are multiple, a list of
|
||||||
|
strings are returned, otherwise just a string is returned.
|
||||||
|
|
||||||
|
Like the standard safe field lookup, will return "Unknown" if the field
|
||||||
|
doesn't exist in the object or if the field exists but is None
|
||||||
|
|
||||||
|
Args:
|
||||||
|
field (str): The field to lookup and validate
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Union[List[str], str]:
|
||||||
|
- List[str] if multiple artists are present
|
||||||
|
- str if only a single artist is present, or if the field cannot be found
|
||||||
|
or the field is None (in which case "Unknown" is returned)
|
||||||
|
"""
|
||||||
|
if field not in raw:
|
||||||
|
return UNK
|
||||||
|
if not raw[field]:
|
||||||
|
return UNK
|
||||||
|
val = [x["name"] for x in raw[field]]
|
||||||
|
return val[0] if len(val) == 1 else val
|
||||||
|
|
||||||
|
title = __get_field_safe("title")
|
||||||
|
artists = __get_artists_safe("artists")
|
||||||
|
album = __get_field_safe("album")
|
||||||
|
return Song(title, artists, album)
|
||||||
|
|
||||||
|
|
||||||
|
def get_all_songs_from_library() -> List[Song]:
|
||||||
|
"""Gets all songs from the Liked Songs 'playlist' in YTMusic for the
|
||||||
|
currently authed user.
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
Exception: When YT_MUSIC_AUTH or YT_MUSIC_COOKIE are not valid environment variables
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
List[Song]: A list of parsed Songs from the user's library
|
||||||
|
"""
|
||||||
|
|
||||||
|
ytmusic = get_instance()
|
||||||
|
|
||||||
|
auth = environ.get("YT_MUSIC_AUTH", "")
|
||||||
|
if not auth:
|
||||||
|
raise Exception("YT_MUSIC_AUTH environment variable cannot be empty!")
|
||||||
|
|
||||||
|
cookie = environ.get("YT_MUSIC_COOKIE", "")
|
||||||
|
if not cookie:
|
||||||
|
raise Exception("YT_MUSIC_COOKIE environment variable cannot be empty!")
|
||||||
|
|
||||||
|
ytmusic.headers["Cookie"] = cookie
|
||||||
|
ytmusic.headers["authorization"] = auth
|
||||||
|
|
||||||
|
songs = ytmusic.get_liked_songs(limit=None) # type: ignore -- limit=None works and gets _all_ songs, and there's no pagination method
|
||||||
|
songs = songs["tracks"]
|
||||||
|
|
||||||
|
return [to_song(x) for x in songs]
|
||||||
Loading…
Reference in a new issue