diff --git a/itunes_last_export/db_management.py b/itunes_last_export/db_management.py index 08bca66..e7ada22 100755 --- a/itunes_last_export/db_management.py +++ b/itunes_last_export/db_management.py @@ -38,7 +38,7 @@ def backup_db(db_path): # shutil.copy(os.path.expanduser("%s/clementine.db" %db_path), # os.path.expanduser("%s/clementine_backup.db" %db_path)) -def update_db(itunes, extract, force_update=True, updated_part="None", progress_value=None, progress_bar=None): +def update_db(itunes, extract, force_update=True, updated_part="None", status=None): """Update the ratings or the playcounts of a database according to an extract file :param itunes: Applescript itunes application @@ -97,7 +97,8 @@ def update_db(itunes, extract, force_update=True, updated_part="None", progress_ print("Updating playcount for {0} from artist {1} to {2} (previous {3})".format(title, artist, lastfm_playcount, current_playcount)) matched.append("{0} {1}".format(artiste, title)) elif lastfm_playcount < current_playcount: - print("Playcount for {0} from artist {1} higher than on last.fm {2} (on itunes {3})".format(title, artist, lastfm_playcount, current_playcount)) + print("Playcount higher than on last.fm {0} (on itunes {1}) for {2}\t{3}\t{4}\t{5}\t{6}\tL\t{7}".format(lastfm_playcount, current_playcount, + artist, track.album().lower().encode('utf-8'), title, track.trackNumber(), int(track.duration()), int(track.playedDate().timeIntervalSince1970()))) else: already_ok.append("{0} {1}".format(artiste, title)) else: @@ -111,9 +112,12 @@ def update_db(itunes, extract, force_update=True, updated_part="None", progress_ pass # print("Track '{0}' from artist '{1}' is too short".format(title, artist)) track_count +=1 - if progress_value: - progress_value.set(50+(50*track_count)/(nbtracks)) - progress_bar.update() + if status: + status.progress_value.set(50+(50*track_count)/(nbtracks)) + status.progress_bar.update() + text = "{0} - {1}".format(track.artist().encode('utf-8'), track.name().encode('utf-8')) + status.status_text.set(text) + status.status_bar.update() extract_file.close() diff --git a/itunes_last_export/gui.py b/itunes_last_export/gui.py index eed5ecf..55c711c 100644 --- a/itunes_last_export/gui.py +++ b/itunes_last_export/gui.py @@ -45,7 +45,7 @@ def __init__(self, fenetre, **kwargs): self.use_cache = False self.force_update = False self.load_config() - self.progress_value = IntVar() + self.status = Status() # Création de nos widgets self.message = Label(self, text="Please enter your last.fm username") @@ -76,8 +76,13 @@ def __init__(self, fenetre, **kwargs): self.bouton_cliquer = Button(self, text="Launch", command=self.cliquer) self.bouton_cliquer.grid(row=5, column=3) - self.progressbar = ttk.Progressbar(self, orient=HORIZONTAL, length=300, mode='determinate', variable=self.progress_value) - self.progressbar.grid(row=6, column=1, columnspan=3) + progress_bar = ttk.Progressbar(self, orient=HORIZONTAL, length=300, mode='determinate', variable=self.status.progress_value) + self.status.init_progress_bar(progress_bar) + self.status.progress_bar.grid(row=6, column=1, columnspan=3) + + status_bar = Label(fenetre, textvariable=self.status.status_text, bd=1, relief=SUNKEN, anchor=W) + self.status.init_status_bar(status_bar) + self.status.status_bar.pack(side=BOTTOM, fill=X) self.pack(fill=BOTH) @@ -85,15 +90,16 @@ def cliquer(self): """ Function called when pressing the "Run" button on the UI """ - self.progress_value.set(0) + self.status.start() self.username = self.username_entry.get() self.use_cache = self.use_cache_var.get() self.force_update = self.force_update_var.get() print(self.username, self.force_update, self.use_cache) self.store_config() - self.thread = UpdatePlaycount(force_update=self.force_update, use_cache=self.use_cache, progress_bar=self.progressbar, progress_value=self.progress_value) + self.thread = UpdatePlaycount(force_update=self.force_update, use_cache=self.use_cache, status=self.status) self.thread.set_infos(self.username, self.server, self.extract_file) self.thread.run() + self.status.finish() def load_config(self): """ @@ -127,6 +133,36 @@ def store_config(self): with open(self.config_path, 'wb') as configfile: self.parser.write(configfile) + +class Status(object): + """ + Class containing the status objects (progress bar and status bar) + """ + + def __init__(self): + self.progress_bar = None + self.progress_value = IntVar() + self.statusbar = None + self.status_text = StringVar() + + + def init_status_bar(self, status_bar): + self.status_bar = status_bar + self.status_text.set("Idle") + + def init_progress_bar(self, progress_bar): + self.progress_bar = progress_bar + + def start(self): + self.progress_value.set(0) + self.status_text.set("Processing") + + def finish(self): + self.status_text.set("Finished") + + + + def main(): """ Gui function, to be called by the launcher @@ -134,6 +170,7 @@ def main(): window = Tk() window.title("iTunes Last Export") interface = Interface(window) + interface.pack_propagate(0) interface.mainloop() diff --git a/itunes_last_export/server_management.py b/itunes_last_export/server_management.py index ec193cb..fa222e2 100755 --- a/itunes_last_export/server_management.py +++ b/itunes_last_export/server_management.py @@ -194,7 +194,7 @@ def parse_line(ligne): print("The following line cannot be parsed: {0}".format(ligne[:-1])) return int(playing_date), title, artist -def lastexporter(server, username, startpage, outfile, tracktype='recenttracks', use_cache=False, progress_value=None, progress_bar=None): +def lastexporter(server, username, startpage, outfile, tracktype='recenttracks', use_cache=False, status=None): """Function called to import the information from the server and store it in a dedicated file :param server: Server on which the information will be extracted @@ -231,9 +231,11 @@ def lastexporter(server, username, startpage, outfile, tracktype='recenttracks', try: for page, totalpages, tracks in get_tracks(server, username, startpage, tracktype=tracktype, firsttrack=firsttrack): print("Got page %s of %s..." % (page, totalpages)) - if progress_value: - progress_value.set(50*page/totalpages) - progress_bar.update() #the import takes 50% of the progress bar + if status: + status.progress_value.set(50*page/totalpages) + status.progress_bar.update() #the import takes 50% of the progress bar + status.status_text.set("Processing page %s of %s..." % (page, totalpages)) + status.status_bar.update() for track in tracks: if tracktype == 'recenttracks': trackdict.setdefault(track[0], track) diff --git a/itunes_last_export/update_playcount.py b/itunes_last_export/update_playcount.py index 2b50a62..0186f69 100755 --- a/itunes_last_export/update_playcount.py +++ b/itunes_last_export/update_playcount.py @@ -30,7 +30,7 @@ class UpdatePlaycount(): Class called to update the playcount, a class is used as it used to be a thread """ - def __init__(self, force_update=False, use_cache=False, progress_bar=None, progress_value=None): + def __init__(self, force_update=False, use_cache=False, status=None): self.username = "" self.input_file = "" self.server = "" @@ -38,8 +38,7 @@ def __init__(self, force_update=False, use_cache=False, progress_bar=None, progr self.startpage = 1 self.force_update = force_update self.use_cache = use_cache - self.progress_bar = progress_bar - self.progress_value = progress_value + self.status = status def set_infos(self, username, server, extract_file): """ @@ -54,29 +53,28 @@ def run(self): """ Main part of the class, called run as it was a thread """ - if self.progress_value: - self.progress_value.set(0) - self.progress_bar.update() + if self.status: + self.status.progress_value.set(0) + self.status.progress_bar.update() print("No input file given, extracting directly from {0} servers".format(self.server)) lastexporter(self.server, self.username, self.startpage, self.extract_file, - tracktype='recenttracks', use_cache=self.use_cache, progress_value=self.progress_value, progress_bar=self.progress_bar) + tracktype='recenttracks', use_cache=self.use_cache, status=self.status) - if self.progress_value: - self.progress_value.set(50) - self.progress_bar.update() + if self.status: + self.status.progress_value.set(50) + self.status.progress_bar.update() itunes = SBApplication.applicationWithBundleIdentifier_("com.apple.iTunes") print("Reading extract file and updating database") - matched, not_matched, already_ok = update_db(itunes, self.extract_file, self.force_update, - updated_part="playcount", progress_value=self.progress_value, progress_bar=self.progress_bar) + matched, not_matched, already_ok = update_db(itunes, self.extract_file, self.force_update, updated_part="playcount", status=self.status) print("%d have been updated, %d had the correct playcount, no match was found for %d" %(len(matched), len(already_ok), len(not_matched))) - if self.progress_value: - self.progress_value.set(100) - self.progress_bar.update() + if self.status: + self.status.progress_value.set(100) + self.status.progress_bar.update() if __name__ == "__main__":