Skip to content

Instantly share code, notes, and snippets.

@CreamyCookie
Last active September 23, 2020 17:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save CreamyCookie/60f007490486d09a174a87c0f7486954 to your computer and use it in GitHub Desktop.
Save CreamyCookie/60f007490486d09a174a87c0f7486954 to your computer and use it in GitHub Desktop.
QuodLibet Plugin that automatically stores ratings and playcounts in the respective files
# Copyright 2016 Joschua Gandert
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
from traceback import format_exc
import mutagen
from mutagen._vorbis import VCommentDict
from mutagen.mp3 import MP3
from gi.repository import Gtk
try:
from quodlibet import _, config
except ImportError:
def _(i):
return i
from quodlibet.util.dprint import print_d
from quodlibet.formats import AudioFileError
from quodlibet import app
from quodlibet import qltk
from quodlibet.qltk import Icons, ErrorMessage
from quodlibet.plugins import PluginConfig, BoolConfProp, IntConfProp
from quodlibet.plugins.events import EventPlugin
from quodlibet.plugins.songshelpers import is_writable
from quodlibet.util import enum
from quodlibet.util.songwrapper import SongWrapper
SONG_ERROR_FMT = "Error for song '{}' in {}"
ALBUM_ERROR_FMT = "Error for album '{}' in {}"
@enum
class UpdateStrategy(int):
AFTER_PLAY_NOT_SKIP = 0
AFTER_PLAY_OR_SKIP = 1
ONCE_ALBUM_RATED = 2
STRATEGY_TO_NAME = [_("After every play (default)"),
_("After every play or skip"),
_("Once, when album fully rated")]
STRATEGY_TO_DESC = [ #
_("Whenever a song was played but not skipped, the plugin will write the "
"tags to the file. The skip count isn't saved, so this avoids "
"unnecessary writes."),
_("Whenever a song was played or skipped, the plugin will write the tags "
"to the file. Can be useful if you want to make sure that ratings of "
"songs you dislike and thus skipped are written to the files."),
_("When a song was played or skipped, the album of that song will be "
"checked. If every song in the album has been rated and at least one "
"has no ratings or play counts stored in its file, the plugin will "
"write the tags to the songs' files. \n\nUse this to avoid constant "
"file updates, but be aware that once an album was updated, you'll have "
"to use the 'Update Tags in Files' plugin whenever you want modified "
"ratings and play counts to be written to the files.")]
PLAY_COUNT_ABOVE_ZERO_TOOLTIP = _(
"When the plugin will write the tags of an album, it will "
"first set the play count of the songs who are zero to one.\n"
"Sometimes you already know that you don't like a song, so "
"setting it to one when saving can be useful later on, when "
"searching for albums you have fully listened to (%s).")
class Config(object):
_config = PluginConfig("autoupdatetagsinfiles")
update_strategy = IntConfProp(_config, "update_strategy",
UpdateStrategy.AFTER_PLAY_NOT_SKIP)
ensure_play_counts_above_zero = BoolConfProp( # useful for searching
_config, "ensure_play_counts_above_zero", False)
CONFIG = Config()
class AutoUpdateTagsInFiles(EventPlugin):
PLUGIN_ID = "AutoUpdateTagsInFilesUserVersion"
PLUGIN_NAME = _("Auto Update Tags in Files")
PLUGIN_DESC = _("When songs were played, update the tags in their files. "
"This will ensure play counts and ratings are up to date.")
PLUGIN_ICON = Icons.DOCUMENT_SAVE
def PluginPreferences(self, _):
return AutoUpdateTagsPrefs()
def plugin_on_song_ended(self, song, skipped):
if song is None or not is_writable(song):
return
strategy = CONFIG.update_strategy
if strategy == UpdateStrategy.AFTER_PLAY_NOT_SKIP:
if not skipped:
self._try_to_update_song(song)
return
elif strategy == UpdateStrategy.AFTER_PLAY_OR_SKIP:
self._try_to_update_song(song)
return
self._update_album_if_fully_rated(song.album_key)
def _try_to_update_song(self, song_wrapper):
try:
song_wrapper._needs_write = True
self._write_tags_to_files([song_wrapper])
except Exception as e:
ErrorMessage(app.window,
SONG_ERROR_FMT.format(song_wrapper, self.PLUGIN_NAME),
"{}\n\n{}".format(e, format_exc())).run()
def _update_album_if_fully_rated(self, album_key):
album = app.library.albums.get(album_key, None)
if album is None:
return
songs = album.songs
# first check for ratings to avoid costly file checks
if not songs or not all(song.has_rating for song in songs):
return
one_or_more_need_update = False
for song in songs:
if one_or_more_need_update:
continue # avoid files access
elif not is_rating_and_play_count_in_file(song['~filename']):
one_or_more_need_update = True
if not one_or_more_need_update:
return
# at least one song has no ratings or play counts stored in files
try:
self._update_album(songs)
except Exception as e:
ErrorMessage(app.window,
ALBUM_ERROR_FMT.format(album_key, self.PLUGIN_NAME),
"{}\n\n{}".format(e, format_exc())).run()
def _update_album(self, songs):
song_wrappers = []
req_play_counts_above_zero = CONFIG.ensure_play_counts_above_zero
for song in songs:
if req_play_counts_above_zero and not song.get("~#playcount", 0):
song['~#playcount'] = 1
wrapper = SongWrapper(song)
wrapper._needs_write = True
song_wrappers.append(wrapper)
self._write_tags_to_files(song_wrappers)
def _write_tags_to_files(self, song_wrappers):
background_check_wrapper_changed(app.library, song_wrappers)
def is_rating_and_play_count_in_file(path):
save_email = config.get("editing", "save_email").strip()
f = mutagen.File(path)
if isinstance(f, MP3):
if 'POPM:' + save_email in f:
return True
elif f.tags is not None and isinstance(f.tags, VCommentDict):
if 'rating:' + save_email in f and 'playcount:' + save_email in f:
return True
return False
class AutoUpdateTagsPrefs(Gtk.Box):
def __init__(self):
super(AutoUpdateTagsPrefs, self).__init__(
orientation=Gtk.Orientation.VERTICAL, spacing=6)
strategy_boxes = []
for desc in STRATEGY_TO_DESC:
desc_label = Gtk.Label(label=desc, wrap=True, xalign=0,
valign=Gtk.Align.START)
box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
box.pack_start(desc_label, False, False, 0)
strategy_boxes.append(box)
def ensure_play_count_toggled(button, *args):
CONFIG.ensure_play_counts_above_zero = button.get_active()
ensure_play_count_checkbutton = Gtk.CheckButton(
label=_("Ensure play counts are above zero when saving"),
valign=Gtk.Align.START)
ensure_play_count_checkbutton.set_tooltip_text(
PLAY_COUNT_ABOVE_ZERO_TOOLTIP % "#(playcount:min = 1)")
ensure_play_count_checkbutton.set_active(
CONFIG.ensure_play_counts_above_zero)
ensure_play_count_checkbutton.connect("toggled",
ensure_play_count_toggled)
album_box = strategy_boxes[UpdateStrategy.ONCE_ALBUM_RATED]
album_box.pack_start(ensure_play_count_checkbutton, False, False, 0)
def show_only_current_box():
current = CONFIG.update_strategy
for n, box in enumerate(strategy_boxes):
box.set_visible(n == current)
grid = Gtk.Grid(column_spacing=6, row_spacing=6)
def grid_add(x, y, child):
grid.attach(child, x, y, 1, 1)
def change_strategy(button):
new_strategy = button.get_active()
CONFIG.update_strategy = new_strategy
show_only_current_box()
update_combobox = Gtk.ComboBoxText()
for name in STRATEGY_TO_NAME:
update_combobox.append_text(name)
update_combobox.set_active(CONFIG.update_strategy)
update_combobox.connect('changed', change_strategy)
update_lbl = ConfigLabel(_("_Update strategy:"), update_combobox)
grid_add(0, 0, update_lbl)
grid_add(1, 0, update_combobox)
for box in strategy_boxes:
# show_all will be called on the plugin preference interface, so
# without the following that would result in overlapping text
box.show_all()
box.props.no_show_all = True
grid_add(1, 1, box)
show_only_current_box()
frame = qltk.Frame(label=_("Preferences"), child=grid)
frame.set_border_width(6)
self.pack_start(frame, False, False, 0)
def background_check_wrapper_changed(library, songs):
need_write = [s for s in songs if s._needs_write]
if need_write:
for song in need_write:
try:
song._song.write()
except AudioFileError as e:
print_d("Couldn't save song %s (%s)" % (song("~filename"), e))
_inform_library_of_changed(library, songs)
def _inform_library_of_changed(library, songs):
changed = []
for song in songs:
if song._was_updated():
changed.append(song._song)
elif not song.valid() and song.exists():
library.reload(song._song)
library.changed(changed)
class ConfigLabel(Gtk.Label):
"""Customised Label for configuration, tied to a widget"""
def __init__(self, text, widget):
super(Gtk.Label, self).__init__(label=text, use_underline=True)
self.set_mnemonic_widget(widget)
self.set_alignment(0.0, 0.5)
@azarmadr
Copy link

what should i change

@CreamyCookie
Copy link
Author

what should i change

I'm sorry. I'm not sure what you're asking. Could you rephrase it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment