diff --git a/ideemyouworthy/accountmanager.py b/ideemyouworthy/accountmanager.py index 99ca1c2..c590691 100644 --- a/ideemyouworthy/accountmanager.py +++ b/ideemyouworthy/accountmanager.py @@ -16,7 +16,7 @@ def __init__(self, logger): if not self.account_info_file.exists(): self.account_info_file.touch() - self.account_info_file.write_text(json.dumps(self.account_info_dict, indent = 4, ensure_ascii = False)) + self.account_info_file.write_text(json.dumps(self.account_info_dict, indent = 4, ensure_ascii = False), encoding = "utf-8") self.logger.info("Created account info file") diff --git a/ideemyouworthy/downloadfinished_messageinterface.py b/ideemyouworthy/downloadfinished_messageinterface.py index 322cd04..157d0ad 100644 --- a/ideemyouworthy/downloadfinished_messageinterface.py +++ b/ideemyouworthy/downloadfinished_messageinterface.py @@ -1,3 +1,6 @@ +import json +from pathlib import Path + from deemix.app.messageinterface import MessageInterface from tinytag import TinyTag @@ -18,7 +21,13 @@ def __init__(self, logger, downloaded_tracks, track_manager, new_playlists, play self.deezer_tracks_to_download = None - def send(self, message, value = None): # TODO: Is there a way to make this more modular so that if it crashes it doesn't lose the progress? I think deezer should make it okay, but still... + self.master_track_file = Path(Path.cwd().parents[0] / "cache" / "track_master_list.json") + if not self.master_track_file.exists(): + self.master_track_file.touch() + self.master_track_file.write_text(json.dumps({}), encoding = "utf-8") + self.logger.info("Created master track file") + + def send(self, message, value = None): if message == "updateQueue" and "downloaded" in value: if value["downloaded"]: # {'uuid': self.queueItem.uuid, 'downloaded': True, 'downloadPath': writepath} @@ -28,9 +37,16 @@ def send(self, message, value = None): # TODO: Is there a way to make this more tags = TinyTag.get(value["downloadPath"]) print("[" + str(self.deezer_tracks_to_download - len(self.queue_manager.queue)) + "/" + str(self.deezer_tracks_to_download) + "] Downloaded " + tags.title) - if len(self.queue_manager.queue) == 0: - if self.youtube_manager is None: - self.track_manager.finished_queue(self.downloaded_tracks, self.new_playlists, self.playlist_changes, self.use_itunes) - else: - self.youtube_manager.update_objects(self.downloaded_tracks, self.new_playlists, self.playlist_changes, self.use_itunes, self.track_manager) - self.youtube_manager.start_download_process() + master_track_dict = json.loads(self.master_track_file.read_text(encoding = "utf-8")) + master_track_dict[track] = self.downloaded_tracks[track] + self.master_track_file.write_text(json.dumps(master_track_dict, indent = 4, ensure_ascii = False), encoding = "utf-8") + break + + if len(self.queue_manager.queue) == 0 and not self.track_manager.has_finished_queue: + self.track_manager.has_finished_queue = True + + if self.youtube_manager is None: + self.track_manager.finished_queue(self.downloaded_tracks, self.new_playlists, self.playlist_changes, self.use_itunes) + else: + self.youtube_manager.update_objects(self.downloaded_tracks, self.new_playlists, self.playlist_changes, self.use_itunes, self.track_manager) + self.youtube_manager.start_download_process() diff --git a/ideemyouworthy/main.py b/ideemyouworthy/main.py index 03c41fb..093446c 100644 --- a/ideemyouworthy/main.py +++ b/ideemyouworthy/main.py @@ -106,6 +106,7 @@ first_result = youtube_manager.search(search_string) youtube_list.append(first_result) downloaded_tracks[track] = first_result + # todo: store spotify track info, then read it to tag youtube songs logger.info("Downloading " + str(len(queue_list)) + " deezer tracks") logger.info("Downloading " + str(len(youtube_list)) + " YouTube tracks") diff --git a/ideemyouworthy/playlistmanager.py b/ideemyouworthy/playlistmanager.py index 18810df..2917227 100644 --- a/ideemyouworthy/playlistmanager.py +++ b/ideemyouworthy/playlistmanager.py @@ -19,7 +19,7 @@ def __init__(self, logger, account_manager): Path.mkdir(Path.cwd().parents[0] / "cache", exist_ok = True) if not self.master_playlist_file.exists(): self.master_playlist_file.touch() - self.master_playlist_file.write_text(json.dumps({})) + self.master_playlist_file.write_text(json.dumps({}), encoding = "utf-8") self.logger.info("Created master playlist file") self.custom_playlist_file = Path(Path.cwd().parents[0] / "custom_playlists.json") @@ -28,7 +28,7 @@ def __init__(self, logger, account_manager): sample_custom_playlists = collections.OrderedDict() sample_custom_playlists["https://open.spotify.com/playlist/3p22aU2NEvY8KErZAoWSJD"] = "[example--will not be used]" sample_custom_playlists["https://open.spotify.com/playlist/2JD9j9iGtPGaupLf0NwZXe"] = "[example--will not be used]" - self.custom_playlist_file.write_text(json.dumps(sample_custom_playlists, indent = 4, ensure_ascii = False)) + self.custom_playlist_file.write_text(json.dumps(sample_custom_playlists, indent = 4, ensure_ascii = False), encoding = "utf-8") self.logger.info("Created custom playlist file") def uri_to_url(self, uri): @@ -69,16 +69,16 @@ def create_playlist_files(self, new_playlists): file_path = Path.cwd().parents[0] / "playlists" / (playlist + ".json") if not file_path.exists(): file_path.touch() - file_path.write_text(json.dumps({})) + file_path.write_text(json.dumps({}), encoding = "utf-8") new_file_count += 1 self.logger.info("Created " + str(new_file_count) + " new playlist files") def store_user_playlists(self, new_playlists): - self.master_playlist_file.write_text(json.dumps(new_playlists, indent = 4, ensure_ascii = False)) + self.master_playlist_file.write_text(json.dumps(new_playlists, indent = 4, ensure_ascii = False), encoding = "utf-8") def store_custom_playlists(self, new_playlists): - self.custom_playlist_file.write_text(json.dumps(new_playlists, indent = 4, ensure_ascii = False)) + self.custom_playlist_file.write_text(json.dumps(new_playlists, indent = 4, ensure_ascii = False), encoding = "utf-8") def read_custom_playlists(self): custom_playlists_dict = json.loads(self.custom_playlist_file.read_text()) @@ -158,7 +158,7 @@ def create_m3u(self): playlist_dict = {**playlist_dict, **custom_dict} master_track_file = Path(Path.cwd().parents[0] / "cache" / "track_master_list.json") - master_track_dict = json.loads(master_track_file.read_text()) + master_track_dict = json.loads(master_track_file.read_text(encoding = "utf-8")) for playlist in playlist_dict: playlist_file_path = Path.cwd().parents[0] / "playlists" / (playlist + ".json") @@ -168,7 +168,7 @@ def create_m3u(self): if not playlist_m3u.exists(): playlist_m3u.touch() - playlist_m3u.write_text("") + playlist_m3u.write_text("", encoding = "utf-8") with playlist_m3u.open("a") as append_file: for track in playlist_tracks: diff --git a/ideemyouworthy/trackmanager.py b/ideemyouworthy/trackmanager.py index 88a15a6..92e17dc 100644 --- a/ideemyouworthy/trackmanager.py +++ b/ideemyouworthy/trackmanager.py @@ -16,7 +16,7 @@ def __init__(self, logger, account_manager): self.master_track_file = Path(Path.cwd().parents[0] / "cache" / "track_master_list.json") if not self.master_track_file.exists(): self.master_track_file.touch() - self.master_track_file.write_text(json.dumps({})) + self.master_track_file.write_text(json.dumps({}), encoding = "utf-8") self.logger.info("Created master track file") self.problematic_tracks_file = Path(Path.cwd().parents[0] / "cache" / "problematic_tracks.txt") @@ -64,12 +64,12 @@ def find_new_tracks(self, new_playlists): playlist_changes[playlist] = playlist_differences self.logger.info(playlist + ": Found " + str(len(playlist_differences)) + " new tracks") - playlist_file_path.write_text(json.dumps(new_playlist_songs, indent = 4, ensure_ascii = False)) + playlist_file_path.write_text(json.dumps(new_playlist_songs, indent = 4, ensure_ascii = False), encoding = "utf-8") return playlist_changes def clear_duplicate_downloads(self, playlist_changes): - master_track_dict = json.loads(self.master_track_file.read_text()) + master_track_dict = json.loads(self.master_track_file.read_text(encoding = "utf-8")) master_track_set = util.dictToSet(master_track_dict) tracks_to_download = set() @@ -88,13 +88,13 @@ def finished_queue(self, downloaded_tracks, new_playlists, playlist_changes, use self.has_finished_queue = True self.logger.info("deezer and YouTube queues finished downloading") - old_master_track_dict = json.loads(self.master_track_file.read_text()) - for track in downloaded_tracks: - if track not in old_master_track_dict and isinstance(downloaded_tracks[track], dict): - old_master_track_dict[track] = downloaded_tracks[track] - - self.master_track_file.write_text(json.dumps(old_master_track_dict, indent = 4, ensure_ascii = False)) - self.logger.info("Saved master track list to file") + old_master_track_dict = json.loads(self.master_track_file.read_text(encoding = "utf-8")) + # for track in downloaded_tracks: + # if track not in old_master_track_dict and isinstance(downloaded_tracks[track], dict): + # old_master_track_dict[track] = downloaded_tracks[track] + # + # self.master_track_file.write_text(json.dumps(old_master_track_dict, indent = 4, ensure_ascii = False), encoding = "utf-8") + # self.logger.info("Saved master track list to file") if use_itunes: self.logger.info("Using iTunes") @@ -187,11 +187,11 @@ def verify_itunes(self): for playlist in itunes_playlists_dict: playlist_file_path = Path.cwd().parents[0] / "playlists" / (playlist + ".json") - official_playlist_dict = json.loads(playlist_file_path.read_text()) # todo: this doesn't allow other playlists in itunes + official_playlist_dict = json.loads(playlist_file_path.read_text()) # todo: this doesn't allow other playlists in itunes official_locations = set() for track in official_playlist_dict: - master_file_dict = json.loads(self.master_track_file.read_text()) + master_file_dict = json.loads(self.master_track_file.read_text(encoding = "utf-8")) if track in master_file_dict: track_location = master_file_dict[track]["download_location"] official_locations.add(track_location) diff --git a/ideemyouworthy/youtubemanager.py b/ideemyouworthy/youtubemanager.py index fc7f0a3..64961ff 100644 --- a/ideemyouworthy/youtubemanager.py +++ b/ideemyouworthy/youtubemanager.py @@ -44,9 +44,15 @@ def __init__(self, logger, spotify_manager, music_directory): self.master_track_file = Path(Path.cwd().parents[0] / "cache" / "track_master_list.json") if not self.master_track_file.exists(): self.master_track_file.touch() - self.master_track_file.write_text(json.dumps({})) + self.master_track_file.write_text(json.dumps({}), encoding = "utf-8") self.logger.info("Created master track file") + # self.youtube_tag_cache_file = Path(Path.cwd().parents[0] / "cache" / "youtube_tags.json") + # if not self.youtube_tag_cache_file.exists(): + # self.youtube_tag_cache_file.touch() + # self.youtube_tag_cache_file.write_text(json.dumps({}), encoding = "utf-8") + # self.logger.info("Created YouTube tag cache file") + self.youtube_dl_manager = youtube_dl.YoutubeDL(ydl_opts) self.logger.info("Finished setting up youtube_manager") @@ -55,6 +61,16 @@ def get_search_string(self, spotify_id): track_name = track_item["name"] track_artist = track_item["artists"][0]["name"] search_string = track_name + " " + track_artist + " lyrics" + + # full_uri = "spotify:track:" + spotify_id + # tags_dict = {} + # tags_dict["title"] = track_name + # tags_dict["album_artist"] = track_artist + # + # full_tags_dict = json.loads(self.youtube_tag_cache_file.read_text()) + # full_tags_dict[full_uri] = tags_dict + # self.youtube_tag_cache_file.write_text(full_tags_dict, encoding = "utf-8") + return search_string def search(self, search_string): @@ -85,10 +101,25 @@ def progress_hook(self, response): print("[" + str(self.current_download_count) + "/" + str(self.youtube_tracks_to_download) + "] Downloaded " + file_name) + # spotify_uri = None + # + # # downloaded_tracks uses full spotify uri + # full_tags_dict = json.loads(self.youtube_tag_cache_file.read_text()) + # tags_dict = full_tags_dict[spotify_uri] + # tags_dict["title"] + # tags_dict["album_artist"] + for track in self.downloaded_tracks: if self.downloaded_tracks[track] == self.current_download_url: self.downloaded_tracks[track] = {"youtube_url": self.current_download_url, "download_location": file_name} + master_track_dict = json.loads(self.master_track_file.read_text(encoding = "utf-8")) + master_track_dict[track] = self.downloaded_tracks[track] + self.master_track_file.write_text(json.dumps(master_track_dict, indent = 4, ensure_ascii = False), encoding = "utf-8") + break + + # spotify_uri = track + # from mutagen.easyid3 import EasyID3 # from mutagen.id3 import ID3 # from mutagen import File, MutagenError