#!/usr/bin/python # -*- coding: utf-8 -*- # # Hooks script for gPodder to set a new id3 info from the database (after download) # # Version 1.0 - Rodolfo García Peñas (kix) # To use, copy it as a Python script into ~/.config/gpodder/hooks/retagging.py # # Dependencies: # * This script needs the eyeD3 command in the $PATH # In debian is included in the package eyed3 # # This script 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. # # gPodder is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . import datetime import gpodder import subprocess import os from gpodder.liblogger import log ## settings strip_album_from_title = True genre_tag = u'Podcast' try: from mutagen import File mutagen_installed = True except: log("retagging hook: Could not find mutagen") mutagen_installed = False def retaggin_hook(self, episode): # read filename (incl. file path) from gPodder database filename = episode.local_filename(create=False, check_only=True) if filename is None: log("retagging hook: No file!") return log("retagging hook: File: %s", filename) # open file with mutagen from mutagen.easyid3 import EasyID3 from mutagen.mp3 import MP3 import mutagen.id3 audio = EasyID3(filename) if audio is None: log("retagging hook: Unable to read the audio header") return log("retagging hook: header opened ok") log("retagging hook: header: %s", audio) # read title+album from gPodder database dbalbum = episode.channel.title dbtitle = episode.title if (strip_album_from_title and dbtitle and dbalbum and dbtitle.startswith(dbalbum)): dbtitle = dbtitle[len(dbalbum):].lstrip() log("retagging hook: dbtitle=%s, dbalbum=%s", dbtitle, dbalbum) # convert pubDate to string try: dbpubDate = datetime.datetime.fromtimestamp(episode.pubDate).strftime('%Y-%m-%d %H:%M') except: dbpubDate = None log("retagging hook: dbpubdate=%s", dbpubDate) # write title+album information into audio files try: audio.add_tags(EasyID3) except: log("retagging hook: Audio has tags") log("retagging hook: Retaggin time!") try: fltitle = audio['title'] log("retagging hook: title in file is %s, not changed", audio['title']); except: if dbtitle is not None: audio['title'] = dbtitle log("retagging hook: title in file is None changed to %s", dbtitle); else: log("retagging hook: title in file and in database is None! not set"); try: flartist = audio['artist'] log("retagging hook: artist in file is %s, not changed", audio['artist']); except: # Set the artist, using album if dbalbum is not None: audio['artist'] = dbalbum log("retagging hook: artist in file is None changed to %s", dbalbum); else: log("retagging hook: artist in file and in database is None! not set"); try: flalbum = audio['album'] log("retagging hook: album in file is %s, not changed", audio['album']); except: if dbalbum is not None: audio['album'] = dbalbum log("retagging hook: album in file is None changed to %s", dbalbum); else: log("retagging hook: album in file and in database is None! not set"); try: flgenre = audio['genre'] log("retagging hook: genre in file is %s, not changed", audio['genre']); except: if genre_tag is not None: audio['genre'] = genre_tag log("retagging hook: genre in file is None changed to %s", genre_tag); else: log("retagging hook: genre in file and in database is None! not set"); try: flgenre = audio['date'] log("retagging hook: published date in file is %s, not changed", audio['date']); except: if dbpubDate is not None: audio['date'] = dbpubDate log("retagging hook: published date in file is None changed to %s", dbpubDate); else: log("retagging hook: published date in file and in database is None! not set"); # Save the mp3 info, version 1 and 2 audio.save(v1=2) log("retagging hook: Finished, ID3 saved."); class gPodderHooks(object): def __init__(self): log('retagging hook: Initializing.') def on_episode_downloaded(self, episode): # exit if mutagen is not installed if not mutagen_installed: log("retagging hook: mutagen not installed") return log("retagging hook: on_episode_downloaded") retaggin_hook(self, episode)