2 # -*- coding: utf-8 -*-
3 from django.core.management import setup_environ
4 from wolnelektury import settings
6 from os.path import abspath, join, dirname, splitext
9 # Add apps and lib directories to PYTHONPATH
10 sys.path.insert(0, abspath(join(dirname(__file__), 'apps')))
11 sys.path.insert(0, abspath(join(dirname(__file__), 'lib')))
13 setup_environ(settings)
15 from catalogue.models import Book
16 from mutagen import easyid3
17 from slughifi import slughifi
19 chosen_book_slugs = set()
21 for file_name in os.listdir('mp3'):
22 base_name, ext = splitext(file_name)
26 audio = easyid3.EasyID3(join('mp3', file_name))
27 title = audio['title'][0]
28 artist = title.split(',', 1)[0].strip()
29 artist_slug = slughifi(artist)
30 title_part = slughifi(title.rsplit(',', 1)[1].strip())
32 print "--------------------"
33 print "File: %s" % file_name
34 print "Title: %s" % title
36 print "Matching books:"
38 matching_books = [book for book in Book.tagged.with_all(artist_slug) if book.slug not in chosen_book_slugs]
39 matching_books = [book for book in matching_books if title_part in book.slug]
41 if len(matching_books) > 1:
42 for i, book in enumerate(matching_books):
43 print "%d: %s (%s)" % (i, book.title, ', '.join(tag.slug for tag in book.tags))
45 i = int(input("Choose which book is read in this file:"))
46 elif len(matching_books) == 1:
49 print "Skipping %s: No matching book found" % file_name
52 print "You chose %d (%s)" % (i, matching_books[i].slug)
54 chosen_book_slugs.add(matching_books[i].slug)
55 os.rename(join('mp3', file_name), join('new_mp3', matching_books[i].slug + '.mp3'))
56 os.rename(join('oggvorbis', base_name + '.ogg'), join('new_ogg', matching_books[i].slug + '.ogg'))