From 7a053ec44bf7bd4c80e8d555b504d27367edd6d0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Marek=20St=C4=99pniowski?= Date: Sun, 30 Nov 2008 22:53:50 +0100 Subject: [PATCH] Added script to semiautomatically rename mp3 and ogg files according to their id3 tags. --- irename.py | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100755 irename.py diff --git a/irename.py b/irename.py new file mode 100755 index 000000000..3ac419224 --- /dev/null +++ b/irename.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +from django.core.management import setup_environ +from wolnelektury import settings +import sys +from os.path import abspath, join, dirname, splitext +import os + +# Add apps and lib directories to PYTHONPATH +sys.path.insert(0, abspath(join(dirname(__file__), 'apps'))) +sys.path.insert(0, abspath(join(dirname(__file__), 'lib'))) + +setup_environ(settings) + +from catalogue.models import Book +from mutagen import easyid3 +from slughifi import slughifi + +chosen_book_slugs = set() + +for file_name in os.listdir('mp3'): + base_name, ext = splitext(file_name) + if ext != '.mp3': + continue + + audio = easyid3.EasyID3(join('mp3', file_name)) + title = audio['title'][0] + artist = title.split(',', 1)[0].strip() + artist_slug = slughifi(artist) + title_part = slughifi(title.rsplit(',', 1)[1].strip()) + + print "--------------------" + print "File: %s" % file_name + print "Title: %s" % title + print + print "Matching books:" + + matching_books = [book for book in Book.tagged.with_all(artist_slug) if book.slug not in chosen_book_slugs] + matching_books = [book for book in matching_books if title_part in book.slug] + + if len(matching_books) > 1: + for i, book in enumerate(matching_books): + print "%d: %s (%s)" % (i, book.title, ', '.join(tag.slug for tag in book.tags)) + print + i = int(input("Choose which book is read in this file:")) + elif len(matching_books) == 1: + i = 0 + else: + print "Skipping %s: No matching book found" % file_name + continue + + print "You chose %d (%s)" % (i, matching_books[i].slug) + + chosen_book_slugs.add(matching_books[i].slug) + os.rename(join('mp3', file_name), join('new_mp3', matching_books[i].slug + '.mp3')) + os.rename(join('oggvorbis', base_name + '.ogg'), join('new_ogg', matching_books[i].slug + '.ogg')) + + \ No newline at end of file -- 2.20.1