Update the UI more efficiently, make it much faster
[gpodder.git] / src / gpodder / libtagupdate.py
blob1416fef706bfe4f069ff386f0c7b914e872d7470
1 # -*- coding: utf-8 -*-
3 # gPodder - A media aggregator and podcast client
4 # Copyright (c) 2005-2008 Thomas Perl and the gPodder Team
6 # gPodder is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # gPodder is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
22 # libtagupdate.py -- tag updating/writing library
23 # thomas perl <thp@perli.net> 20070315
28 # for ogg/vorbis (vorbiscomment utility)
29 import popen2
31 # for logging
32 from liblogger import log
34 # for mp3 files
35 has_eyed3 = True
36 try:
37 import eyeD3
38 except:
39 log('(tagupdate) eyed3 not found -- tag update disabled')
40 has_eyed3 = False
42 # do we provide tagging functions to the user?
43 def tagging_supported():
44 global has_eyed3
45 return has_eyed3
48 tag_update_methods = {}
50 def update_metadata_on_file( filename, **metadata):
51 global tag_update_methods
53 ext = filename[-3:]
54 if ext in tag_update_methods:
55 log('Updating tag for %s', filename)
56 return tag_update_methods[ext]( filename, **metadata)
58 log('Do not know how to update file extension %s :/', ext)
59 return False
62 def update_tag_ogg( filename, **metadata):
63 data = '\n'.join( [ '%s=%s' % ( i.upper(), metadata[i] ) for i in metadata ] + [''])
65 p = popen2.Popen3('vorbiscomment -w "%s"' % filename)
67 writer = p.tochild
68 writer.write(data)
69 writer.close()
71 result = p.wait() == 0
73 if not result:
74 log('Error while running vorbiscomment. Is it installed?! (vorbis-tools)')
76 return result
78 tag_update_methods['ogg'] = update_tag_ogg
81 def update_tag_mp3( filename, **metadata):
82 if not has_eyed3:
83 log('eyeD3 not found -> please install. no tags have been updated.')
84 return False
86 tag = eyeD3.tag.Tag( fileName = filename)
87 tag.remove( eyeD3.tag.ID3_ANY_VERSION)
88 tag.setVersion( eyeD3.tag.ID3_ANY_VERSION)
90 for key in metadata:
91 if key.lower() == 'artist':
92 tag.setArtist( metadata[key])
93 # Set "album" info from artist name if it is not available
94 if 'album' not in metadata and tag.getAlbum().strip() == '':
95 log('(tagupdate) No album information - using artist')
96 tag.setAlbum(metadata[key])
97 elif key.lower() == 'title':
98 tag.setTitle( metadata[key])
99 elif key.lower() == 'album':
100 tag.setAlbum( metadata[key])
101 elif key.lower() == 'genre':
102 tag.setGenre(metadata[key])
104 return tag.update( eyeD3.tag.ID3_V2) == 1 and tag.update( eyeD3.tag.ID3_V1) == 1
106 tag_update_methods['mp3'] = update_tag_mp3