Skip to content

Commit

Permalink
positively identifying file types for tagging
Browse files Browse the repository at this point in the history
  • Loading branch information
brokkr committed Dec 6, 2017
1 parent 62ac632 commit 61c5db6
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 26 deletions.
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@

setup(
name='poca',
version='1.0',
version='1.1alpha',
license='GPL3',
description='A fast and highly customizable command line podcast client',
long_description=('A fast and highly customizable command line podcast '
'client'),
author='Mads Michelsen',
author_email='mail@brokkr.net',
url='https://github.com/brokkr/poca',
download_url='https://github.com/brokkr/poca/archive/v1.0',
#download_url='https://github.com/brokkr/poca/archive/v1.0',
scripts=['src/scripts/poca', 'src/scripts/poca-subscribe'],
packages=['poca'],
package_dir={'poca': 'src/poca'},
Expand Down
1 change: 1 addition & 0 deletions src/poca/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@
from . import xmlconf
from . import lxmlfuncs
from . import tag
from . import valid_tags
2 changes: 1 addition & 1 deletion src/poca/about.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

"""Basic info"""

__version__ = '1.0'
__version__ = '1.1alpha'
VERSION = __version__
MAINTAINER = "Mads Michelsen <mail@brokkr.net>"
DESCRIPTION = ("A fast and highly customizable command line podcast client")
Expand Down
34 changes: 11 additions & 23 deletions src/poca/tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,10 @@

import mutagen

from poca.valid_tags import validate_keys
from poca.outcome import Outcome


easymp4_keys = ['title', 'album', 'artist', 'albumartist', 'date', 'comment',
'description', 'grouping', 'genre', 'copyright', 'albumsort',
'albumartistsort', 'artistsort', 'titlesort', 'composersort']


def tag_audio_file(settings, sub, jar, entry):
'''Metdata tagging using mutagen'''
id3v1_dic = {'yes': 0, 'no': 2}
Expand All @@ -27,6 +23,7 @@ def tag_audio_file(settings, sub, jar, entry):
tracks = sub.find('./track_numbering')
tracks = tracks.text if tracks else 'no'
frames = sub.xpath('./metadata/*')
invalid_keys = []
if not frames and tracks == 'no':
return Outcome(True, 'Tagging skipped')
try:
Expand All @@ -42,6 +39,7 @@ def tag_audio_file(settings, sub, jar, entry):
entry['poca_abspath'])
if audio.tags is None:
audio.add_tags()
# easify
if isinstance(audio, mutagen.mp3.MP3):
if id3v2 == 3:
audio.tags.update_to_v23()
Expand All @@ -51,14 +49,12 @@ def tag_audio_file(settings, sub, jar, entry):
audio = mutagen.File(entry['poca_abspath'], easy=True)
if isinstance(audio, mutagen.mp4.MP4):
audio = mutagen.File(entry['poca_abspath'], easy=True)
if isinstance(audio, mutagen.mp3.EasyMP3):
overrides, invalid_keys = validate_keys(frames,
audio.tags.valid_keys.keys())
if isinstance(audio, mutagen.easymp4.EasyMP4):
overrides, invalid_keys = validate_keys(frames, easymp4_keys)
# recognise specific mutagen types. if not recognised don't attempt tag.
else:
overrides = [(override.tag, override.text) for override in frames]
# validate
audio_type = type(audio)
outcome, overrides, invalid_keys = validate_keys(frames, audio_type)
# deal with bad outcomes and bad invalid_keys
print(outcome.success, overrides, invalid_keys)
# deal with bad outcomes and bad invalid_keys
for override in overrides:
audio[override[0]] = override[1]
if tracks == 'yes' or (tracks == 'if missing' and 'tracknumber' not in
Expand All @@ -67,19 +63,11 @@ def tag_audio_file(settings, sub, jar, entry):
track_no += 1
jar.track_no = track_no
jar.save()
if isinstance(audio, (mutagen.mp3.EasyMP3, mutagen.oggvorbis.OggVorbis,
mutagen.oggopus.OggOpus, mutagen.flac.FLAC)):
audio['tracknumber'] = str(track_no)
if isinstance(audio, mutagen.mp3.EasyMP3):
audio.save(v1=id3v1, v2_version=id3v2)
else:
audio.save()
return Outcome(True, 'Metadata successfully updated')


def validate_keys(frames, valid_keys):
'''Returns a tuple with valid overrides and a list of invalid keys'''
overrides = [(override.tag, override.text) for override in frames
if override.tag in valid_keys]
invalid_keys = [override.tag for override in frames if override.tag
not in valid_keys)
return (overrides, invalid_keys)

37 changes: 37 additions & 0 deletions src/poca/valid_tags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# -*- coding: utf-8 -*-

# Copyright 2010-2017 Mads Michelsen (mail@brokkr.net)
# This file is part of Poca.
# Poca 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 3 of the License,
# or (at your option) any later version.

"""Lists of valid tagging keys for reference"""

import mutagen


mp3_list = list(mutagen.easyid3.EasyID3.valid_keys.keys())
mp4_list = ['title', 'album', 'artist', 'albumartist', 'date', 'comment',
'description', 'grouping', 'genre', 'copyright', 'albumsort',
'albumartistsort', 'artistsort', 'titlesort', 'composersort']
def mp3_keys(key): yield key if key in mp3_list else None
def mp4_keys(key): yield key if key in mp4_list else None
def vorbis_keys(key): yield key
type_dic = {mutagen.mp3.EasyMP3: mp3_keys,
mutagen.easymp4.EasyMP4: mp4_keys,
mutagen.oggvorbis.OggVorbis: vorbis_keys,
mutagen.oggopus.OggOpus: vorbis_keys,
mutagen.flac.FLAC: vorbis_keys}

def validate_keys(audio_type, frames):
'''Returns a tuple with valid overrides and a list of invalid keys'''
valid_keys = type_dic.get(audio_type, None)
if valid_keys is None:
return (Outcome(False, 'Unsupported file type for tagging'), [], [])
overrides = [(override.tag, override.text) for override in frames
if override.tag in valid_keys(override.tag)]
invalid_keys = [override.tag for override in frames if override.tag
not in valid_keys)
return (Outcome(True, 'Valid audio type'), overrides, invalid_keys)

0 comments on commit 61c5db6

Please sign in to comment.