dotfiles

config files and scripts at HOME.
git clone git://git.ricketyspace.net/dotfiles.git
Log | Files | Refs

commit 455e290a9a84211a5eb4b26303c3837474232a30
parent 170976097f863723b9f7336050a0d510976d7cd4
Author: rsiddharth <rsiddharth@ninthfloor.org>
Date:   Fri, 13 May 2016 05:15:11 +0000

Add .bin/tagmp3album

- Used to add meta information to mp3 files.
  - Use eyed3 library to get this shit done.

Diffstat:
.bin/tagmp3album | 147+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 147 insertions(+), 0 deletions(-)

diff --git a/.bin/tagmp3album b/.bin/tagmp3album @@ -0,0 +1,147 @@ +#!/usr/bin/env python +# +# Copyright (C) 2016 rsiddharth <s@ricketyspace.net> +# +# +# 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 3 of the +# License, or (at your option) any later version. +# +# This program 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 +# <http://www.gnu.org/licenses/>. + +import json +import os + +import eyed3 + +from argparse import ArgumentParser +from collections import OrderedDict +from os import path +from sys import exit + + +def read_file(filename): + """Read file and return it as a string. + + :param str filename: pathname of the file. + + """ + content = '' + + try: + with open(filename, 'rb') as f: + for line in f: + content = content + line + except IOError: + print "Error: unable to open %s" % filename + + return content + + +def json_read(filename): + """Deserialize JSON formatted `str` from `filename` into Python object. + """ + if not path.isfile(filename): + print '%s not found. Quitting' % filename + exit(1) + + return json.loads(read_file(filename), + object_pairs_hook=OrderedDict) + + +def contains_all_words(filename, words): + """Returns True if filename contains all `words`. + """ + matches = 0 + + for word in words: + if word in filename: + matches +=1 + + return matches == len(words) + + +def print_tracks_not_found(tracks): + """Print tracks whose file where not found. + """ + print 'Track(s) not found:' + + for track in tracks: + print track + + +def track_path(dir_path, title): + """Return track `title`'s path; False otherwise. + """ + words = title.split() + + for dirpath, dirnames, filenames in os.walk(dir_path): + for filename in filenames: + if contains_all_words(filename, words): + return path.join(dirpath, filename) + + return False + + +def tag_track(path, artist, album, title, track_num): + """Tag track. + + """ + track = eyed3.load(path) + track.initTag() + + track.tag.artist = artist + track.tag.album = album + track.tag.title = title + track.tag.track_num = track_num + + track.tag.save() + + +def tag_album(album_info_file): + """Tag the whole album + """ + abs_path = path.abspath(album_info_file) + dir_path = path.dirname(abs_path) + album_info = json_read(album_info_file) + + not_found = [] + + artist = album_info['artist'] + album = album_info['album'] + track_num = 0 + + for track_title in album_info['tracks']: + track_num += 1 + t_path = track_path(dir_path, track_title) + + if not t_path: + not_found.append(track_title) + continue + + tag_track(t_path, artist, album, track_title, track_num) + + if not_found: + print_tracks_not_found(not_found) + + +def main(): + parser = ArgumentParser() + + help_text = "Path to file containing album info in JSON format" + parser.add_argument("album_info_file", + help=help_text) + args = parser.parse_args() + + tag_album(args.album_info_file) + +if __name__ == "__main__": + main()