Portrait mode support for gPodder on Maemo 5
[gpodder.git] / src / gpodder / sync.py
blobe295c07f3780132ad946dc8b1451ac451a20d304
1 # -*- coding: utf-8 -*-
3 # gPodder - A media aggregator and podcast client
4 # Copyright (c) 2005-2009 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/>.
21 # sync.py -- Device synchronization
22 # Thomas Perl <thp@perli.net> 2007-12-06
23 # based on libipodsync.py (2006-04-05 Thomas Perl)
25 import gpodder
27 from gpodder import util
28 from gpodder import services
29 from gpodder import libconverter
31 from gpodder.liblogger import log
33 import time
34 import calendar
36 _ = gpodder.gettext
38 gpod_available = True
39 try:
40 import gpod
41 except:
42 gpod_available = False
43 log('(gpodder.sync) Could not find gpod')
45 pymtp_available = True
46 try:
47 import pymtp
48 except:
49 pymtp_available = False
50 log('(gpodder.sync) Could not find pymtp.')
52 try:
53 import mad
54 except:
55 log('(gpodder.sync) Could not find pymad')
57 try:
58 import eyeD3
59 except:
60 log( '(gpodder.sync) Could not find eyeD3')
62 try:
63 import Image
64 except:
65 log('(gpodder.sync) Could not find Python Imaging Library (PIL)')
67 # Register our dependencies for the synchronization module
68 services.dependency_manager.depend_on(_('iPod synchronization'), _('Support synchronization of podcasts to Apple iPod devices via libgpod.'), ['gpod', 'mad', 'eyeD3'], [])
69 services.dependency_manager.depend_on(_('MTP device synchronization'), _('Support synchronization of podcasts to devices using the Media Transfer Protocol via pymtp.'), ['pymtp'], [])
70 services.dependency_manager.depend_on(_('iPod OGG converter'), _('Convert OGG podcasts to MP3 files on synchronization to iPods using oggdec and LAME.'), [], ['oggdec', 'lame'])
71 services.dependency_manager.depend_on(_('iPod video podcasts'), _('Detect video lengths via MPlayer, to synchronize video podcasts to iPods.'), [], ['mplayer'])
72 services.dependency_manager.depend_on(_('Rockbox cover art support'), _('Copy podcast cover art to filesystem-based MP3 players running Rockbox.org firmware. Needs Python Imaging.'), ['Image'], [])
74 import os
75 import os.path
76 import glob
77 import shutil
78 import sys
79 import time
80 import string
81 import email.Utils
82 import re
85 def open_device(config):
86 device_type = config.device_type
87 if device_type == 'ipod':
88 return iPodDevice(config)
89 elif device_type == 'filesystem':
90 return MP3PlayerDevice(config)
91 elif device_type == 'mtp':
92 return MTPDevice(config)
93 else:
94 return None
96 def get_track_length(filename):
97 if util.find_command('mplayer') is not None:
98 try:
99 mplayer_output = os.popen('mplayer -msglevel all=-1 -identify -vo null -ao null -frames 0 "%s" 2>/dev/null' % filename).read()
100 return int(float(mplayer_output[mplayer_output.index('ID_LENGTH'):].splitlines()[0][10:])*1000)
101 except:
102 pass
103 else:
104 log('Please install MPlayer for track length detection.')
106 try:
107 mad_info = mad.MadFile(filename)
108 return int(mad_info.total_time())
109 except:
110 pass
112 try:
113 eyed3_info = eyeD3.Mp3AudioFile(filename)
114 return int(eyed3_info.getPlayTime()*1000)
115 except:
116 pass
118 return int(60*60*1000*3) # Default is three hours (to be on the safe side)
121 class SyncTrack(object):
123 This represents a track that is on a device. You need
124 to specify at least the following keyword arguments,
125 because these will be used to display the track in the
126 GUI. All other keyword arguments are optional and can
127 be used to reference internal objects, etc... See the
128 iPod synchronization code for examples.
130 Keyword arguments needed:
131 playcount (How often has the track been played?)
132 podcast (Which podcast is this track from? Or: Folder name)
133 released (The release date of the episode)
135 If any of these fields is unknown, it should not be
136 passed to the function (the values will default to None
137 for all required fields).
139 def __init__(self, title, length, modified, **kwargs):
140 self.title = title
141 self.length = length
142 self.filesize = util.format_filesize(length)
143 self.modified = modified
145 # Set some (possible) keyword arguments to default values
146 self.playcount = None
147 self.podcast = None
148 self.released = None
150 # Convert keyword arguments to object attributes
151 self.__dict__.update(kwargs)
154 class Device(services.ObservableService):
155 def __init__(self, config):
156 self._config = config
157 self.cancelled = False
158 self.allowed_types = ['audio', 'video']
159 self.errors = []
160 self.tracks_list = []
161 signals = ['progress', 'sub-progress', 'status', 'done', 'post-done']
162 services.ObservableService.__init__(self, signals)
164 def open(self):
165 pass
167 def cancel(self):
168 self.cancelled = True
169 self.notify('status', _('Cancelled by user'))
171 def close(self):
172 self.notify('status', _('Writing data to disk'))
173 if self._config.sync_disks_after_transfer:
174 successful_sync = (os.system('sync') == 0)
175 else:
176 log('Not syncing disks. Unmount your device before unplugging.', sender=self)
177 successful_sync = True
178 self.notify('done')
179 self.notify('post-done', self, successful_sync)
180 return True
182 def add_tracks(self, tracklist=[], force_played=False):
183 for track in list(tracklist):
184 # Filter tracks that are not meant to be synchronized
185 does_not_exist = not track.was_downloaded(and_exists=True)
186 exclude_played = track.is_played and not force_played and \
187 self._config.only_sync_not_played
188 wrong_type = track.file_type() not in self.allowed_types
190 if does_not_exist or exclude_played or wrong_type:
191 log('Excluding %s from sync', track.title, sender=self)
192 tracklist.remove(track)
194 compare_episodes = lambda a, b: cmp(a.pubDate, b.pubDate)
195 for id, track in enumerate(sorted(tracklist, cmp=compare_episodes)):
196 if self.cancelled:
197 return False
199 self.notify('progress', id+1, len(tracklist))
201 added = self.add_track(track)
203 if self._config.on_sync_mark_played:
204 log('Marking as played on transfer: %s', track.url, sender=self)
205 track.mark(is_played=True)
207 if added and self._config.on_sync_delete:
208 log('Removing episode after transfer: %s', track.url, sender=self)
209 track.delete_from_disk()
210 return True
212 def convert_track(self, episode):
213 filename = episode.local_filename(create=False)
214 # The file has to exist, if we ought to transfer it, and therefore,
215 # local_filename(create=False) must never return None as filename
216 assert filename is not None
217 (fn, extension) = os.path.splitext(filename)
218 if libconverter.converters.has_converter(extension):
219 if self._config.disable_pre_sync_conversion:
220 log('Pre-sync conversion is not enabled, set disable_pre_sync_conversion to "False" to enable')
221 return filename
223 log('Converting: %s', filename, sender=self)
224 callback_status = lambda percentage: self.notify('sub-progress', int(percentage))
225 local_filename = libconverter.converters.convert(filename, callback=callback_status)
227 if local_filename is None:
228 log('Cannot convert %s', original_filename, sender=self)
229 return filename
231 return str(local_filename)
233 return filename
235 def remove_tracks(self, tracklist=[]):
236 for id, track in enumerate(tracklist):
237 if self.cancelled:
238 return False
239 self.notify('progress', id, len(tracklist))
240 self.remove_track(track)
241 return True
243 def get_all_tracks(self):
244 pass
246 def add_track(self, track):
247 pass
249 def remove_track(self, track):
250 pass
252 def get_free_space(self):
253 pass
255 def episode_on_device(self, episode):
256 return self._track_on_device(episode.title)
258 def _track_on_device(self, track_name):
259 for t in self.tracks_list:
260 title = t.title
261 if track_name == title:
262 return t
263 return None
265 class iPodDevice(Device):
266 def __init__(self, config):
267 Device.__init__(self, config)
269 self.mountpoint = str(self._config.ipod_mount)
271 self.itdb = None
272 self.podcast_playlist = None
275 def get_free_space(self):
276 # Reserve 10 MiB for iTunesDB writing (to be on the safe side)
277 RESERVED_FOR_ITDB = 1024*1024*10
278 return util.get_free_disk_space(self.mountpoint) - RESERVED_FOR_ITDB
280 def open(self):
281 Device.open(self)
282 if not gpod_available or not os.path.isdir(self.mountpoint):
283 return False
285 self.notify('status', _('Opening iPod database'))
286 self.itdb = gpod.itdb_parse(self.mountpoint, None)
287 if self.itdb is None:
288 return False
290 self.itdb.mountpoint = self.mountpoint
291 self.podcasts_playlist = gpod.itdb_playlist_podcasts(self.itdb)
293 if self.podcasts_playlist:
294 self.notify('status', _('iPod opened'))
296 # build the initial tracks_list
297 self.tracks_list = self.get_all_tracks()
299 return True
300 else:
301 return False
303 def close(self):
304 if self.itdb is not None:
305 self.notify('status', _('Saving iPod database'))
306 gpod.itdb_write(self.itdb, None)
307 self.itdb = None
309 Device.close(self)
310 return True
312 def update_played_or_delete(self, channel, episodes, delete_from_db):
314 Check whether episodes on ipod are played and update as played
315 and delete if required.
317 for episode in episodes:
318 track = self.episode_on_device(episode)
319 if track:
320 gtrack = track.libgpodtrack
321 if gtrack.playcount > 0:
322 if delete_from_db and not gtrack.rating:
323 log('Deleting episode from db %s', gtrack.title, sender=self)
324 channel.delete_episode_by_url(gtrack.podcasturl)
325 else:
326 log('Marking episode as played %s', gtrack.title, sender=self)
327 episode.mark(is_played=True)
329 def purge(self):
330 for track in gpod.sw_get_playlist_tracks(self.podcasts_playlist):
331 if gpod.itdb_filename_on_ipod(track) is None:
332 log('Episode has no file: %s', track.title, sender=self)
333 # self.remove_track_gpod(track)
334 elif track.playcount > 0 and not track.rating:
335 log('Purging episode: %s', track.title, sender=self)
336 self.remove_track_gpod(track)
338 def get_all_tracks(self):
339 tracks = []
340 for track in gpod.sw_get_playlist_tracks(self.podcasts_playlist):
341 filename = gpod.itdb_filename_on_ipod(track)
342 length = util.calculate_size(filename)
344 timestamp = util.file_modification_timestamp(filename)
345 modified = util.format_date(timestamp)
346 released = gpod.itdb_time_mac_to_host(track.time_released)
347 released = util.format_date(released)
349 t = SyncTrack(track.title, length, modified, modified_sort=timestamp, libgpodtrack=track, playcount=track.playcount, released=released, podcast=track.artist)
350 tracks.append(t)
351 return tracks
353 def remove_track(self, track):
354 self.notify('status', _('Removing %s') % track.title)
355 self.remove_track_gpod(track.libgpodtrack)
357 def remove_track_gpod(self, track):
358 filename = gpod.itdb_filename_on_ipod(track)
360 try:
361 gpod.itdb_playlist_remove_track(self.podcasts_playlist, track)
362 except:
363 log('Track %s not in playlist', track.title, sender=self)
365 gpod.itdb_track_unlink(track)
366 util.delete_file(filename)
368 def add_track(self, episode):
369 self.notify('status', _('Adding %s') % episode.title)
370 for track in gpod.sw_get_playlist_tracks(self.podcasts_playlist):
371 if episode.url == track.podcasturl:
372 if track.playcount > 0:
373 episode.mark(is_played=True)
374 # Mark as played on iPod if played locally (and set podcast flags)
375 self.set_podcast_flags(track, episode)
376 return True
378 original_filename = episode.local_filename(create=False)
379 # The file has to exist, if we ought to transfer it, and therefore,
380 # local_filename(create=False) must never return None as filename
381 assert original_filename is not None
382 local_filename = original_filename
384 if util.calculate_size(original_filename) > self.get_free_space():
385 log('Not enough space on %s, sync aborted...', self.mountpoint, sender = self)
386 self.errors.append( _('Error copying %s: Not enough free disk space on %s') % (episode.title, self.mountpoint))
387 self.cancelled = True
388 return False
390 local_filename = self.convert_track(episode)
392 (fn, extension) = os.path.splitext(local_filename)
393 if extension.lower().endswith('ogg'):
394 log('Cannot copy .ogg files to iPod.', sender=self)
395 return False
397 track = gpod.itdb_track_new()
399 # Add release time to track if pubDate has a valid value
400 if episode.pubDate > 0:
401 try:
402 # libgpod>= 0.5.x uses a new timestamp format
403 track.time_released = gpod.itdb_time_host_to_mac(int(episode.pubDate))
404 except:
405 # old (pre-0.5.x) libgpod versions expect mactime, so
406 # we're going to manually build a good mactime timestamp here :)
408 # + 2082844800 for unixtime => mactime (1970 => 1904)
409 track.time_released = int(episode.pubDate + 2082844800)
411 track.title = str(episode.title)
412 track.album = str(episode.channel.title)
413 track.artist = str(episode.channel.title)
414 track.description = str(util.remove_html_tags(episode.description))
416 track.podcasturl = str(episode.url)
417 track.podcastrss = str(episode.channel.url)
419 track.tracklen = get_track_length(local_filename)
420 track.size = os.path.getsize(local_filename)
422 if episode.file_type() == 'audio':
423 track.filetype = 'mp3'
424 track.mediatype = 0x00000004
425 elif episode.file_type() == 'video':
426 track.filetype = 'm4v'
427 track.mediatype = 0x00000006
429 self.set_podcast_flags(track, episode)
430 self.set_cover_art(track, local_filename)
432 gpod.itdb_track_add(self.itdb, track, -1)
433 gpod.itdb_playlist_add_track(self.podcasts_playlist, track, -1)
434 gpod.itdb_cp_track_to_ipod(track, str(local_filename), None)
436 # If the file has been converted, delete the temporary file here
437 if local_filename != original_filename:
438 util.delete_file(local_filename)
440 return True
442 def set_podcast_flags(self, track, episode):
443 try:
444 # Set blue bullet for unplayed tracks on 5G iPods
445 if episode.is_played:
446 track.mark_unplayed = 0x01
447 if track.playcount == 0:
448 track.playcount = 1
449 else:
450 if track.playcount > 0 or track.bookmark_time > 0:
451 #track is partially played so no blue bullet
452 track.mark_unplayed = 0x01
453 else:
454 #totally unplayed
455 track.mark_unplayed = 0x02
457 # Set several flags for to podcast values
458 track.remember_playback_position = 0x01
459 track.flag1 = 0x02
460 track.flag2 = 0x01
461 track.flag3 = 0x01
462 track.flag4 = 0x01
463 except:
464 log('Seems like your python-gpod is out-of-date.', sender=self)
466 def set_cover_art(self, track, local_filename):
467 try:
468 tag = eyeD3.Tag()
469 if tag.link(local_filename):
470 if 'APIC' in tag.frames and len(tag.frames['APIC']) > 0:
471 apic = tag.frames['APIC'][0]
473 extension = 'jpg'
474 if apic.mimeType == 'image/png':
475 extension = 'png'
476 cover_filename = '%s.cover.%s' (local_filename, extension)
478 cover_file = open(cover_filename, 'w')
479 cover_file.write(apic.imageData)
480 cover_file.close()
482 gpod.itdb_track_set_thumbnails(track, cover_filename)
483 return True
484 except:
485 log('Error getting cover using eyeD3', sender=self)
487 try:
488 cover_filename = os.path.join(os.path.dirname(local_filename), 'cover')
489 if os.path.isfile(cover_filename):
490 gpod.itdb_track_set_thumbnails(track, cover_filename)
491 return True
492 except:
493 log('Error getting cover using channel cover', sender=self)
495 return False
498 class MP3PlayerDevice(Device):
499 # if different players use other filenames besides
500 # .scrobbler.log, add them to this list
501 scrobbler_log_filenames = ['.scrobbler.log']
503 def __init__(self, config):
504 Device.__init__(self, config)
505 self.destination = self._config.mp3_player_folder
506 self.buffer_size = 1024*1024 # 1 MiB
507 self.scrobbler_log = []
509 def get_free_space(self):
510 return util.get_free_disk_space(self.destination)
512 def open(self):
513 Device.open(self)
514 self.notify('status', _('Opening MP3 player'))
515 if util.directory_is_writable(self.destination):
516 self.notify('status', _('MP3 player opened'))
517 # build the initial tracks_list
518 self.tracks_list = self.get_all_tracks()
519 if self._config.mp3_player_use_scrobbler_log:
520 mp3_player_mount_point = util.find_mount_point(self.destination)
521 # If a moint point cannot be found look inside self.destination for scrobbler_log_filenames
522 # this prevents us from os.walk()'ing the entire / filesystem
523 if mp3_player_mount_point == '/':
524 mp3_player_mount_point = self.destination
525 log_location = self.find_scrobbler_log(mp3_player_mount_point)
526 if log_location is not None and self.load_audioscrobbler_log(log_location):
527 log('Using Audioscrobbler log data to mark tracks as played', sender=self)
528 return True
529 else:
530 return False
532 def add_track(self, episode):
533 self.notify('status', _('Adding %s') % episode.title.decode('utf-8', 'ignore'))
535 if self._config.fssync_channel_subfolders:
536 # Add channel title as subfolder
537 folder = episode.channel.title
538 # Clean up the folder name for use on limited devices
539 folder = util.sanitize_filename(folder, self._config.mp3_player_max_filename_length)
540 folder = os.path.join(self.destination, folder)
541 else:
542 folder = self.destination
544 from_file = util.sanitize_encoding(self.convert_track(episode))
545 filename_base = util.sanitize_filename(episode.sync_filename(self._config.custom_sync_name_enabled, self._config.custom_sync_name), self._config.mp3_player_max_filename_length)
547 to_file = filename_base + os.path.splitext(from_file)[1].lower()
549 # dirty workaround: on bad (empty) episode titles,
550 # we simply use the from_file basename
551 # (please, podcast authors, FIX YOUR RSS FEEDS!)
552 if os.path.splitext(to_file)[0] == '':
553 to_file = os.path.basename(from_file)
555 to_file = os.path.join(folder, to_file)
557 if not os.path.exists(folder):
558 try:
559 os.makedirs(folder)
560 except:
561 log('Cannot create folder on MP3 player: %s', folder, sender=self)
562 return False
564 if (self._config.mp3_player_use_scrobbler_log and not episode.is_played
565 and [episode.channel.title, episode.title] in self.scrobbler_log):
566 log('Marking "%s" from "%s" as played', episode.title, episode.channel.title, sender=self)
567 episode.mark(is_played=True)
569 if self._config.rockbox_copy_coverart and not os.path.exists(os.path.join(folder, 'cover.bmp')):
570 log('Creating Rockbox album art for "%s"', episode.channel.title, sender=self)
571 self.copy_player_cover_art(folder, from_file, \
572 'cover.bmp', 'BMP', self._config.rockbox_coverart_size)
574 if self._config.custom_player_copy_coverart \
575 and not os.path.exists(os.path.join(folder, \
576 self._config.custom_player_coverart_name)):
577 log('Creating custom player album art for "%s"',
578 episode.channel.title, sender=self)
579 self.copy_player_cover_art(folder, from_file, \
580 self._config.custom_player_coverart_name, \
581 self._config.custom_player_coverart_format, \
582 self._config.custom_player_coverart_size)
584 if not os.path.exists(to_file):
585 log('Copying %s => %s', os.path.basename(from_file), to_file.decode(util.encoding), sender=self)
586 return self.copy_file_progress(from_file, to_file)
588 return True
590 def copy_file_progress(self, from_file, to_file):
591 try:
592 out_file = open(to_file, 'wb')
593 except IOError, ioerror:
594 self.errors.append(_('Error opening %s: %s') % (ioerror.filename, ioerror.strerror))
595 self.cancel()
596 return False
598 try:
599 in_file = open(from_file, 'rb')
600 except IOError, ioerror:
601 self.errors.append(_('Error opening %s: %s') % (ioerror.filename, ioerror.strerror))
602 self.cancel()
603 return False
605 in_file.seek(0, 2)
606 bytes = in_file.tell()
607 in_file.seek(0)
609 bytes_read = 0
610 s = in_file.read(self.buffer_size)
611 while s:
612 bytes_read += len(s)
613 try:
614 out_file.write(s)
615 except IOError, ioerror:
616 self.errors.append(ioerror.strerror)
617 try:
618 out_file.close()
619 except:
620 pass
621 try:
622 log('Trying to remove partially copied file: %s' % to_file, sender=self)
623 os.unlink( to_file)
624 log('Yeah! Unlinked %s at least..' % to_file, sender=self)
625 except:
626 log('Error while trying to unlink %s. OH MY!' % to_file, sender=self)
627 self.cancel()
628 return False
629 self.notify('sub-progress', int(min(100, 100*float(bytes_read)/float(bytes))))
630 s = in_file.read(self.buffer_size)
631 out_file.close()
632 in_file.close()
634 return True
636 def get_all_tracks(self):
637 tracks = []
639 if self._config.fssync_channel_subfolders:
640 files = glob.glob(os.path.join(self.destination, '*', '*'))
641 else:
642 files = glob.glob(os.path.join(self.destination, '*'))
644 for filename in files:
645 (title, extension) = os.path.splitext(os.path.basename(filename))
646 length = util.calculate_size(filename)
648 timestamp = util.file_modification_timestamp(filename)
649 modified = util.format_date(timestamp)
650 if self._config.fssync_channel_subfolders:
651 podcast_name = os.path.basename(os.path.dirname(filename))
652 else:
653 podcast_name = None
655 t = SyncTrack(title, length, modified, modified_sort=timestamp, filename=filename, podcast=podcast_name)
656 tracks.append(t)
657 return tracks
659 def episode_on_device(self, episode):
660 e = util.sanitize_filename(episode.sync_filename(self._config.custom_sync_name_enabled, self._config.custom_sync_name), self._config.mp3_player_max_filename_length)
661 return self._track_on_device(e)
663 def remove_track(self, track):
664 self.notify('status', _('Removing %s') % track.title)
665 util.delete_file(track.filename)
666 directory = os.path.dirname(track.filename)
667 if self.directory_is_empty(directory) and self._config.fssync_channel_subfolders:
668 try:
669 os.rmdir(directory)
670 except:
671 log('Cannot remove %s', directory, sender=self)
673 def directory_is_empty(self, directory):
674 files = glob.glob(os.path.join(directory, '*'))
675 dotfiles = glob.glob(os.path.join(directory, '.*'))
676 return len(files+dotfiles) == 0
678 def find_scrobbler_log(self, mount_point):
679 """ find an audioscrobbler log file from log_filenames in the mount_point dir """
680 for dirpath, dirnames, filenames in os.walk(mount_point):
681 for log_file in self.scrobbler_log_filenames:
682 filename = os.path.join(dirpath, log_file)
683 if os.path.isfile(filename):
684 return filename
686 # No scrobbler log on that device
687 return None
689 def copy_player_cover_art(self, destination, local_filename, \
690 cover_dst_name, cover_dst_format, \
691 cover_dst_size):
693 Try to copy the channel cover to the podcast folder on the MP3
694 player. This makes the player, e.g. Rockbox (rockbox.org), display the
695 cover art in its interface.
697 You need the Python Imaging Library (PIL) installed to be able to
698 convert the cover file to a Bitmap file, which Rockbox needs.
700 try:
701 cover_loc = os.path.join(os.path.dirname(local_filename), 'cover')
702 cover_dst = os.path.join(destination, cover_dst_name)
703 if os.path.isfile(cover_loc):
704 log('Creating cover art file on player', sender=self)
705 log('Cover art size is %s', cover_dst_size, sender=self)
706 size = (cover_dst_size, cover_dst_size)
707 try:
708 cover = Image.open(cover_loc)
709 cover.thumbnail(size)
710 cover.save(cover_dst, cover_dst_format)
711 except IOError:
712 log('Cannot create %s (PIL?)', cover_dst, traceback=True, sender=self)
713 return True
714 else:
715 log('No cover available to set as player cover', sender=self)
716 return True
717 except:
718 log('Error getting cover using channel cover', sender=self)
719 return False
722 def load_audioscrobbler_log(self, log_file):
723 """ Retrive track title and artist info for all the entries
724 in an audioscrobbler portable player format logfile
725 http://www.audioscrobbler.net/wiki/Portable_Player_Logging """
726 try:
727 log('Opening "%s" as AudioScrobbler log.', log_file, sender=self)
728 f = open(log_file, 'r')
729 entries = f.readlines()
730 f.close()
731 except IOError, ioerror:
732 log('Error: "%s" cannot be read.', log_file, sender=self)
733 return False
735 try:
736 # regex that can be used to get all the data from a scrobbler.log entry
737 entry_re = re.compile('^(.*)\t(.*)\t(.*)\t(.*)\t(.*)\t(.*)\t(.*)$')
738 for entry in entries:
739 match_obj = re.match(entry_re, entry)
740 # L means at least 50% of the track was listened to (S means < 50%)
741 if match_obj and match_obj.group(6).strip().lower() == 'l':
742 # append [artist_name, track_name]
743 self.scrobbler_log.append([match_obj.group(1), match_obj.group(3)])
744 except:
745 log('Error while parsing "%s".', log_file, sender=self)
747 return True
749 class MTPDevice(Device):
750 def __init__(self, config):
751 Device.__init__(self, config)
752 self.__model_name = None
753 self.__MTPDevice = pymtp.MTP()
755 def __callback(self, sent, total):
756 if self.cancelled:
757 return -1
758 percentage = round(float(sent)/float(total)*100)
759 text = ('%i%%' % percentage)
760 self.notify('progress', sent, total, text)
762 def __date_to_mtp(self, date):
764 this function format the given date and time to a string representation
765 according to MTP specifications: YYYYMMDDThhmmss.s
767 return
768 the string representation od the given date
770 if not date:
771 return ""
772 try:
773 d = time.gmtime(date)
774 return time.strftime("%Y%m%d-%H%M%S.0Z", d)
775 except Exception, exc:
776 log('ERROR: An error has happend while trying to convert date to an mtp string (%s)', exc, sender=self)
777 return None
779 def __mtp_to_date(self, mtp):
781 this parse the mtp's string representation for date
782 according to specifications (YYYYMMDDThhmmss.s) to
783 a python time object
786 if not mtp:
787 return None
789 try:
790 mtp = mtp.replace(" ", "0") # replace blank with 0 to fix some invalid string
791 d = time.strptime(mtp[:8] + mtp[9:13],"%Y%m%d%H%M%S")
792 _date = calendar.timegm(d)
793 if len(mtp)==20:
794 # TIME ZONE SHIFTING: the string contains a hour/min shift relative to a time zone
795 try:
796 shift_direction=mtp[15]
797 hour_shift = int(mtp[16:18])
798 minute_shift = int(mtp[18:20])
799 shift_in_sec = hour_shift * 3600 + minute_shift * 60
800 if shift_direction == "+":
801 _date += shift_in_sec
802 elif shift_direction == "-":
803 _date -= shift_in_sec
804 else:
805 raise ValueError("Expected + or -")
806 except Exception, exc:
807 log('WARNING: ignoring invalid time zone information for %s (%s)', mtp, exc, sender=self)
808 return max( 0, _date )
809 except Exception, exc:
810 log('WARNING: the mtp date "%s" can not be parsed against mtp specification (%s)', mtp, exc, sender=self)
811 return None
813 def get_name(self):
815 this function try to find a nice name for the device.
816 First, it tries to find a friendly (user assigned) name
817 (this name can be set by other application and is stored on the device).
818 if no friendly name was assign, it tries to get the model name (given by the vendor).
819 If no name is found at all, a generic one is returned.
821 Once found, the name is cached internaly to prevent reading again the device
823 return
824 the name of the device
827 if self.__model_name:
828 return self.__model_name
830 self.__model_name = self.__MTPDevice.get_devicename() # actually libmtp.Get_Friendlyname
831 if not self.__model_name or self.__model_name == "?????":
832 self.__model_name = self.__MTPDevice.get_modelname()
833 if not self.__model_name:
834 self.__model_name = "MTP device"
836 return self.__model_name
838 def open(self):
839 Device.open(self)
840 log("opening the MTP device", sender=self)
841 self.notify('status', _('Opening the MTP device'), )
843 try:
844 self.__MTPDevice.connect()
845 # build the initial tracks_list
846 self.tracks_list = self.get_all_tracks()
847 except Exception, exc:
848 log('unable to find an MTP device (%s)', exc, sender=self, traceback=True)
849 return False
851 self.notify('status', _('%s opened') % self.get_name())
852 return True
854 def close(self):
855 log("closing %s", self.get_name(), sender=self)
856 self.notify('status', _('Closing %s') % self.get_name())
858 try:
859 self.__MTPDevice.disconnect()
860 except Exception, exc:
861 log('unable to close %s (%s)', self.get_name(), exc, sender=self)
862 return False
864 self.notify('status', _('%s closed') % self.get_name())
865 Device.close(self)
866 return True
868 def add_track(self, episode):
869 self.notify('status', _('Adding %s...') % episode.title)
870 filename = str(self.convert_track(episode))
871 log("sending " + filename + " (" + episode.title + ").", sender=self)
873 try:
874 # verify free space
875 needed = util.calculate_size(filename)
876 free = self.get_free_space()
877 if needed > free:
878 log('Not enough space on device %s: %s available, but need at least %s', self.get_name(), util.format_filesize(free), util.format_filesize(needed), sender=self)
879 self.cancelled = True
880 return False
882 # fill metadata
883 metadata = pymtp.LIBMTP_Track()
884 metadata.title = str(episode.title)
885 metadata.artist = str(episode.channel.title)
886 metadata.album = str(episode.channel.title)
887 metadata.genre = "podcast"
888 metadata.date = self.__date_to_mtp(episode.pubDate)
889 metadata.duration = get_track_length(str(filename))
891 # send the file
892 self.__MTPDevice.send_track_from_file(filename,
893 episode.basename + episode.extension(),
894 metadata, 0, callback=self.__callback)
895 except:
896 log('unable to add episode %s', episode.title, sender=self, traceback=True)
897 return False
899 return True
901 def remove_track(self, sync_track):
902 self.notify('status', _('Removing %s') % sync_track.mtptrack.title)
903 log("removing %s", sync_track.mtptrack.title, sender=self)
905 try:
906 self.__MTPDevice.delete_object(sync_track.mtptrack.item_id)
907 except Exception, exc:
908 log('unable remove file %s (%s)', sync_track.mtptrack.filename, exc, sender=self)
910 log('%s removed', sync_track.mtptrack.title , sender=self)
912 def get_all_tracks(self):
913 try:
914 listing = self.__MTPDevice.get_tracklisting(callback=self.__callback)
915 except Exception, exc:
916 log('unable to get file listing %s (%s)', exc, sender=self)
918 tracks = []
919 for track in listing:
920 title = track.title
921 if not title or title=="": title=track.filename
922 if len(title) > 50: title = title[0:49] + '...'
923 artist = track.artist
924 if artist and len(artist) > 50: artist = artist[0:49] + '...'
925 length = track.filesize
926 age_in_days = 0
927 date = self.__mtp_to_date(track.date)
928 if not date:
929 modified = track.date # not a valid mtp date. Display what mtp gave anyway
930 modified_sort = -1 # no idea how to sort invalid date
931 else:
932 modified = util.format_date(date)
933 modified_sort = date
935 t = SyncTrack(title, length, modified, modified_sort=modified_sort, mtptrack=track, podcast=artist)
936 tracks.append(t)
937 return tracks
939 def get_free_space(self):
940 return self.__MTPDevice.get_freespace()