Skip to content

Commit

Permalink
Add a status bar and move all in a status object
Browse files Browse the repository at this point in the history
  • Loading branch information
werdeil committed Sep 10, 2018
1 parent bc3d499 commit 92c028a
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 29 deletions.
14 changes: 9 additions & 5 deletions itunes_last_export/db_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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()

Expand Down
47 changes: 42 additions & 5 deletions itunes_last_export/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -76,24 +76,30 @@ 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)

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):
"""
Expand Down Expand Up @@ -127,13 +133,44 @@ 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
"""
window = Tk()
window.title("iTunes Last Export")
interface = Interface(window)
interface.pack_propagate(0)
interface.mainloop()


Expand Down
10 changes: 6 additions & 4 deletions itunes_last_export/server_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
28 changes: 13 additions & 15 deletions itunes_last_export/update_playcount.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,15 @@ 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 = ""
self.extract_file = ""
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):
"""
Expand All @@ -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__":
Expand Down

0 comments on commit 92c028a

Please sign in to comment.