Skip to content

Commit

Permalink
Fixed a few bugs
Browse files Browse the repository at this point in the history
Took 1 hour 45 minutes
  • Loading branch information
TheNathanSpace committed Nov 5, 2021
1 parent d3c1107 commit 9e35d00
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 16 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ So much work just to hear some trashy rapper say "I be high off marijuana"...
- Can load settings from file `user_settings.json`
- Spotify local files found in playlists are downloaded from YouTube based on their title

##### Changed

- Album artwork is renamed to `folder.jpg` when copied to Android so as not to clutter up your photos

### 1.12

I really shouldn't be incrementing the version number for each change, but it's too late to stop now!
Expand Down
6 changes: 5 additions & 1 deletion ideemyouworthy/android_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ def transfer_all(logger: Logger):
for file in all_music:
file_name = file.name
file_parent = file.parents[0].name
android_path = f"/storage/emulated/0/Music/music/{file_parent}/{file_name}"
if file.suffix == ".jpg":
android_path = f"/storage/emulated/0/Music/music/{file_parent}/folder{file.suffix}"
else:
android_path = f"/storage/emulated/0/Music/music/{file_parent}/{file_name}"
exists = "No such file or directory" not in device.shell(f"ls \"{android_path}\"")
if not exists:
device.push(file, android_path)
Expand All @@ -45,6 +48,7 @@ def transfer_all(logger: Logger):
for file in all_playlists:
file_name = file.name
android_path = f"/storage/emulated/0/Music/playlists/{file_name}"

device.push(file, android_path)
transferred_count += 1

Expand Down
30 changes: 24 additions & 6 deletions ideemyouworthy/download_finished_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,51 @@

from downloaded_track import DownloadedTrack
from track_manager import TrackManager
from youtube_manager import YoutubeManager


class DownloadFinishedListener(LogListener):
def __init__(self, track_manager: TrackManager, logger: Logger):
def __init__(self, track_manager: TrackManager, youtube_manager: YoutubeManager, logger: Logger):
self.track_manager = track_manager
self.youtube_manager = youtube_manager

self.logger: Logger = logger

self.first_complete = False
self.downloaded_number = 0

def send(self, message, value = None):
if message == "updateQueue" and "error" in value and "Track not available" in value["error"]:
downloaded_track: DownloadedTrack
for downloaded_track in self.track_manager.deezer_tracks:
if downloaded_track.deezer_uuid == value["uuid"]:
self.downloaded_number += 1

self.logger.info(f"[{str(self.downloaded_number)}/{str(len(self.track_manager.deezer_tracks))}] Couldn't get track {downloaded_track.key} from deezer; will use YouTube")

downloaded_track.deezer_uuid = None
downloaded_track.youtube_tags = self.track_manager.get_track_data(downloaded_track.key)

split_uri = downloaded_track.key.split(":")
search_string = self.youtube_manager.get_search_string(split_uri[2])
first_result = self.youtube_manager.search(search_string)
downloaded_track.youtube_url = first_result
downloaded_track.update_traits()
self.track_manager.youtube_tracks.append(downloaded_track)
self.track_manager.deezer_tracks.remove(downloaded_track)

if message == "updateQueue" and "downloaded" in value:
if value["downloaded"]:
# {'uuid': self.queueItem.uuid, 'downloaded': True, 'downloadPath': writepath}
downloaded_track: DownloadedTrack
for downloaded_track in self.track_manager.deezer_tracks:
if downloaded_track.deezer_uuid == value["uuid"]:
if not self.first_complete:
self.logger.info("--- deezer downloads: ---")
self.first_complete = True

self.downloaded_number += 1

downloaded_track.deezer_uuid = value["uuid"]
downloaded_track.download_location = Path(value["downloadPath"])
tags = TinyTag.get(value["downloadPath"])
self.logger.info("[" + str(self.downloaded_number) + "/" + str(len(self.track_manager.deezer_tracks)) + "] Downloaded " + str(tags.title))
self.logger.info(f"[{str(self.downloaded_number)}/{str(len(self.track_manager.deezer_tracks))}] Downloaded {str(tags.title)}")

downloaded_track.store_to_master_file()
break
4 changes: 2 additions & 2 deletions ideemyouworthy/log_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def __init__(self):
self.log_file.touch()

self.deemix_logger = logging.getLogger('deemix')
self.deemix_logger.setLevel(logging.INFO)
self.deemix_logger.setLevel(logging.DEBUG)

self.yt_logger = logging.getLogger('youtube-dl')
self.yt_logger.setLevel(logging.INFO)
Expand All @@ -34,7 +34,7 @@ def __init__(self):

system_handler = logging.FileHandler(filename = str(self.log_file), mode = "a")
system_handler.setFormatter(formatter)
system_handler.setLevel(logging.WARNING)
system_handler.setLevel(logging.CRITICAL)

self.system_logger.addHandler(system_handler)
self.deemix_logger.addHandler(main_handler)
Expand Down
6 changes: 5 additions & 1 deletion ideemyouworthy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
import logging
import os
import shutil
from pathlib import Path

from deemix.downloader import Downloader
Expand Down Expand Up @@ -117,7 +118,7 @@
track_manager.unique_spotify_tracks = unique_spotify_tracks
track_manager.custom_tracks = all_custom_tracks

listener = DownloadFinishedListener(track_manager = track_manager, logger = logger)
listener = DownloadFinishedListener(track_manager = track_manager, youtube_manager = youtube_manager, logger = logger)
logger.info("Converting Spotify tracks to deezer and YouTube, this might take a while...")
track_manager.process_spotify_tracks(deezer_object = deezer_object, listener = listener, youtube_manager = youtube_manager)
track_manager.process_custom_tracks(youtube_manager = youtube_manager)
Expand All @@ -126,14 +127,17 @@
logger.info("Downloading 0 tracks!")

if len(track_manager.deezer_tracks) != 0:
logger.info("--- deezer downloads: ---")
listener.deezer_tracks_to_download = len(track_manager.deezer_tracks)
downloaded_track: DownloadedTrack
for downloaded_track in track_manager.deezer_tracks:
downloader = Downloader(dz = deezer_object, downloadObject = downloaded_track.deezer_single, settings = account_manager.deezer_settings, listener = listener)
listener.downloader = downloader
downloader.start()
# todo: fallback bitrate

if len(track_manager.youtube_tracks) != 0:
logger.info("--- YouTube downloads: ---")
youtube_manager.in_process_list = track_manager.youtube_tracks.copy()
youtube_manager.all_tracks_to_download = track_manager.youtube_tracks.copy()
listener.youtube_manager = youtube_manager
Expand Down
8 changes: 5 additions & 3 deletions ideemyouworthy/playlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,13 @@ def find_new_tracks(self):
old_tracks = self.load_tracks_from_file()

newly_added = new_tracks.copy()
to_remove = set()
to_remove = []
for track in newly_added:
if track in old_tracks:
to_remove.add(track)
to_remove.append(track)

for track in to_remove:
newly_added.pop(track)
newly_added.remove(track)

self.newly_added = newly_added

Expand Down Expand Up @@ -159,6 +159,8 @@ def add_track_to_itunes(self, track_key: str):

def update_playlist_file(self):
old_master_track_dict = json.loads(self.master_track_file.read_text(encoding = "utf-8"))
self.playlist_file.write_text(json.dumps([]))

for track_uri in self.spotify_tracks:
if track_uri in old_master_track_dict:
self.add_track(track_uri)
8 changes: 7 additions & 1 deletion ideemyouworthy/playlist_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,12 @@ def retrieve_custom_playlists(self):
original_custom_playlists[playlist_url]["name"] = playlist_name

playlist: Playlist = Playlist(spotify_uri = playlist_uri, name = playlist_name, logger = self.logger, account_manager = self.account_manager)
playlist.custom_track_strings = original_custom_playlists[playlist_url]["custom_tracks"]
if "custom_tracks" in original_custom_playlists[playlist_url]:
playlist.custom_track_strings = original_custom_playlists[playlist_url]["custom_tracks"]
else:
playlist.custom_track_strings = []
original_custom_playlists[playlist_url]["custom_tracks"] = []

self.playlists.append(playlist)
valid_count += 1

Expand Down Expand Up @@ -154,6 +159,7 @@ def get_custom_tracks(self, custom_tracks: list):
def add_new_tracks(self, use_itunes: bool):
playlist: Playlist
for playlist in self.playlists:
playlist.playlist_file.write_text(json.dumps([]))
downloaded_track: DownloadedTrack
for spotify_uri in (*playlist.spotify_tracks, *playlist.custom_track_strings):
playlist.add_track(spotify_uri)
Expand Down
2 changes: 0 additions & 2 deletions ideemyouworthy/youtube_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ def search(self, search_string):

def start_download_process(self):
self.logger.debug("Starting YouTube downloads")
self.logger.info("--- YouTube downloads: ---")

self.currently_downloading = True
self.continue_download_process()

Expand Down

0 comments on commit 9e35d00

Please sign in to comment.