Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/sources/songs/Directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from src.sources.SongSearchItem import SongSearchItem
from src.sources.songs.SongsSourceBase import SongsSourceBase
from ..utils import list_or_empty


class Directory(SongsSourceBase):
Expand All @@ -11,7 +12,7 @@ class Directory(SongsSourceBase):
INPUT_PATH = []

def __init__(self, user_args):
self.INPUT_PATH = user_args["input_path"] or os.getenv("INPUT_DIRECTORY_PATH") or self.INPUT_PATH
self.INPUT_PATH = user_args["input_path"] or list_or_empty(os.getenv("INPUT_DIRECTORY_PATH"))

if self.INPUT_PATH and not all([os.path.isdir(input_dir)] for input_dir in self.INPUT_PATH):
self.raise_error(f'{self.INPUT_PATH} is not a valid directory. Exiting...')
Expand Down
6 changes: 4 additions & 2 deletions src/sources/songs/File.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import os

from src.sources.ColorPrint import ColorPrint
from src.sources.SongSearchItem import SongSearchItem
from src.sources.songs.SongsSourceBase import SongsSourceBase
from src.sources.SongSearchItem import SongSearchItem
from ..utils import list_or_empty


class File(SongsSourceBase):

INPUT_FILE = []
def __init__(self, user_args):
self.INPUT_FILE = user_args["inputTextfile"] or [os.getenv("INPUT_FILE_PATH")] or self.INPUT_FILE
self.INPUT_FILE = user_args["inputTextfile"] or list_or_empty(os.getenv("INPUT_FILE_PATH"))

def get_song_list(self) -> list[str]:
search_list = []
Expand Down
49 changes: 26 additions & 23 deletions src/sources/songs/Spotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import spotipy
from spotipy import SpotifyClientCredentials

from ..utils import list_or_empty

from .SongsSourceBase import SongsSourceBase
from ..SongSearchItem import SongSearchItem

Expand All @@ -11,14 +13,14 @@ class Spotify(SongsSourceBase):

CLIENT_ID = ""
CLIENT_SECRET = ""
PLAYLIST_ID = []
PLAYLIST_IDS = []

def __init__(self, user_args):
self.CLIENT_ID = user_args["spotify_id"] or os.getenv("SPOTIPY_CLIENT_ID")
self.CLIENT_SECRET = user_args["spotify_secret"] or os.getenv("SPOTIPY_CLIENT_SECRET")
self.PLAYLIST_ID = user_args["spotify_input"] or os.getenv("SPOTIPY_PLAYLIST_ID")
self.PLAYLIST_IDS = user_args["spotify_input"] or list_or_empty(os.getenv("SPOTIPY_PLAYLIST_ID"))

if self.PLAYLIST_ID and not (self.CLIENT_ID and self.CLIENT_SECRET): self.raise_error(
if self.PLAYLIST_IDS and not (self.CLIENT_ID and self.CLIENT_SECRET): self.raise_error(
"Client ID and secret are required if a Spotify playlist is specified")

super().__init__()
Expand All @@ -33,33 +35,34 @@ def get_song_list(self) -> list[str]:
item = SongSearchItem(name_set, artist_set)
search_list.append(item)

print(f"Successfully got parsed playlist from Spotify: {self.PLAYLIST_ID}")
print(f"Successfully got parsed playlist from Spotify: {self.PLAYLIST_IDS}")
return search_list


def _get_all_tracks(self):
auth_manager = SpotifyClientCredentials(self.CLIENT_ID, self.CLIENT_SECRET)
spotify_client = spotipy.Spotify(auth_manager=auth_manager)
playlist_identifier = self.PLAYLIST_ID

tracks = []
offset = 0
limit = 100

while True:
print(f"Query Tracks {offset} to {offset + limit}")
playlist = spotify_client.playlist_items(
playlist_id=playlist_identifier,
fields="items(track(name,artists(name)))",
offset=offset,
limit=limit
)
items = playlist["items"]

if not items:
break

tracks.extend(items)
offset += limit

for playlist_identifier in self.PLAYLIST_IDS:
offset = 0
limit = 100

while True:
print(f"Query Tracks {offset} to {offset + limit}")
playlist = spotify_client.playlist_items(
playlist_id=playlist_identifier,
fields="items(track(name,artists(name)))",
offset=offset,
limit=limit
)
items = playlist["items"]

if not items:
break

tracks.extend(items)
offset += limit

return tracks
3 changes: 3 additions & 0 deletions src/sources/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def list_or_empty(value):
"""Return [] if value is None, otherwise [value]."""
return [] if value is None else [value]
20 changes: 10 additions & 10 deletions usdx_scraper.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,20 +96,20 @@ def main():
UsdbAnimuxDe.__class__:UsdbAnimuxDe(user_args),
}

song_sources = {
Spotify.__class__:Spotify(user_args),
Directory.__class__:Directory(user_args),
File.__class__:File(user_args),
}
song_sources = [
Spotify(user_args),
Directory(user_args),
File(user_args),
]

media_sources = {
Youtube.__class__:Youtube(user_args, lyrics_source=lyrics_sources[UsdbAnimuxDe.__class__]), # TODO Change so we can have multiple lyrics sources
}
media_sources = [
Youtube(user_args, lyrics_source=lyrics_sources[UsdbAnimuxDe.__class__]), # TODO Change so we can have multiple lyrics sources
]

# Go through all the sources and get a list of all songs
search_list = []
for source in song_sources:
search_list += song_sources[source].get_song_list()
search_list += source.get_song_list()

# Remove duplicate elements in the list.
# Happens if the songs are in multiple sources.
Expand All @@ -136,7 +136,7 @@ def main():

# Right now we only have one media source, so we can just use that one.
#todo think about adding more sources. YouTube can't be the only source.
media_source = media_sources[Youtube.__class__]
media_source = media_sources[0]

# Download songs

Expand Down