Skip to content

Commit

Permalink
Updates master track file as it downloads instead of the end
Browse files Browse the repository at this point in the history
  • Loading branch information
TheNathanSpace committed May 16, 2021
1 parent 34b92bf commit 849d95e
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 28 deletions.
2 changes: 1 addition & 1 deletion ideemyouworthy/accountmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
30 changes: 23 additions & 7 deletions ideemyouworthy/downloadfinished_messageinterface.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import json
from pathlib import Path

from deemix.app.messageinterface import MessageInterface
from tinytag import TinyTag

Expand All @@ -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}
Expand All @@ -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()
1 change: 1 addition & 0 deletions ideemyouworthy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
14 changes: 7 additions & 7 deletions ideemyouworthy/playlistmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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):
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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")
Expand All @@ -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:
Expand Down
24 changes: 12 additions & 12 deletions ideemyouworthy/trackmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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()
Expand All @@ -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")
Expand Down Expand Up @@ -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)
Expand Down
33 changes: 32 additions & 1 deletion ideemyouworthy/youtubemanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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):
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 849d95e

Please sign in to comment.