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)
27 from gpodder
import util
28 from gpodder
import services
29 from gpodder
import libconverter
31 from gpodder
.liblogger
import log
42 gpod_available
= False
43 log('(gpodder.sync) Could not find gpod')
45 pymtp_available
= True
49 pymtp_available
= False
50 log('(gpodder.sync) Could not find pymtp.')
55 log('(gpodder.sync) Could not find pymad')
60 log( '(gpodder.sync) Could not find eyeD3')
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'], [])
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
)
96 def get_track_length(filename
):
97 if util
.find_command('mplayer') is not None:
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)
104 log('Please install MPlayer for track length detection.')
107 mad_info
= mad
.MadFile(filename
)
108 return int(mad_info
.total_time())
113 eyed3_info
= eyeD3
.Mp3AudioFile(filename
)
114 return int(eyed3_info
.getPlayTime()*1000)
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
):
142 self
.filesize
= util
.format_filesize(length
)
143 self
.modified
= modified
145 # Set some (possible) keyword arguments to default values
146 self
.playcount
= 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']
160 self
.tracks_list
= []
161 signals
= ['progress', 'sub-progress', 'status', 'done', 'post-done']
162 services
.ObservableService
.__init
__(self
, signals
)
168 self
.cancelled
= True
169 self
.notify('status', _('Cancelled by user'))
172 self
.notify('status', _('Writing data to disk'))
173 if self
._config
.sync_disks_after_transfer
:
174 successful_sync
= (os
.system('sync') == 0)
176 log('Not syncing disks. Unmount your device before unplugging.', sender
=self
)
177 successful_sync
= True
179 self
.notify('post-done', self
, successful_sync
)
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
)):
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()
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')
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
)
231 return str(local_filename
)
235 def remove_tracks(self
, tracklist
=[]):
236 for id, track
in enumerate(tracklist
):
239 self
.notify('progress', id, len(tracklist
))
240 self
.remove_track(track
)
243 def get_all_tracks(self
):
246 def add_track(self
, track
):
249 def remove_track(self
, track
):
252 def get_free_space(self
):
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
:
261 if track_name
== title
:
265 class iPodDevice(Device
):
266 def __init__(self
, config
):
267 Device
.__init
__(self
, config
)
269 self
.mountpoint
= str(self
._config
.ipod_mount
)
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
282 if not gpod_available
or not os
.path
.isdir(self
.mountpoint
):
285 self
.notify('status', _('Opening iPod database'))
286 self
.itdb
= gpod
.itdb_parse(self
.mountpoint
, None)
287 if self
.itdb
is None:
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()
304 if self
.itdb
is not None:
305 self
.notify('status', _('Saving iPod database'))
306 gpod
.itdb_write(self
.itdb
, None)
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
)
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
)
326 log('Marking episode as played %s', gtrack
.title
, sender
=self
)
327 episode
.mark(is_played
=True)
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
):
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
)
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
)
361 gpod
.itdb_playlist_remove_track(self
.podcasts_playlist
, track
)
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
)
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
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
)
397 track
= gpod
.itdb_track_new()
399 # Add release time to track if pubDate has a valid value
400 if episode
.pubDate
> 0:
402 # libgpod>= 0.5.x uses a new timestamp format
403 track
.time_released
= gpod
.itdb_time_host_to_mac(int(episode
.pubDate
))
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
)
442 def set_podcast_flags(self
, track
, episode
):
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:
450 if track
.playcount
> 0 or track
.bookmark_time
> 0:
451 #track is partially played so no blue bullet
452 track
.mark_unplayed
= 0x01
455 track
.mark_unplayed
= 0x02
457 # Set several flags for to podcast values
458 track
.remember_playback_position
= 0x01
464 log('Seems like your python-gpod is out-of-date.', sender
=self
)
466 def set_cover_art(self
, track
, local_filename
):
469 if tag
.link(local_filename
):
470 if 'APIC' in tag
.frames
and len(tag
.frames
['APIC']) > 0:
471 apic
= tag
.frames
['APIC'][0]
474 if apic
.mimeType
== 'image/png':
476 cover_filename
= '%s.cover.%s' (local_filename
, extension
)
478 cover_file
= open(cover_filename
, 'w')
479 cover_file
.write(apic
.imageData
)
482 gpod
.itdb_track_set_thumbnails(track
, cover_filename
)
485 log('Error getting cover using eyeD3', sender
=self
)
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
)
493 log('Error getting cover using channel cover', sender
=self
)
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
)
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
)
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
)
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
):
561 log('Cannot create folder on MP3 player: %s', folder
, sender
=self
)
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
)
590 def copy_file_progress(self
, from_file
, to_file
):
592 out_file
= open(to_file
, 'wb')
593 except IOError, ioerror
:
594 self
.errors
.append(_('Error opening %s: %s') % (ioerror
.filename
, ioerror
.strerror
))
599 in_file
= open(from_file
, 'rb')
600 except IOError, ioerror
:
601 self
.errors
.append(_('Error opening %s: %s') % (ioerror
.filename
, ioerror
.strerror
))
606 bytes
= in_file
.tell()
610 s
= in_file
.read(self
.buffer_size
)
615 except IOError, ioerror
:
616 self
.errors
.append(ioerror
.strerror
)
622 log('Trying to remove partially copied file: %s' % to_file
, sender
=self
)
624 log('Yeah! Unlinked %s at least..' % to_file
, sender
=self
)
626 log('Error while trying to unlink %s. OH MY!' % to_file
, sender
=self
)
629 self
.notify('sub-progress', int(min(100, 100*float(bytes_read
)/float(bytes
))))
630 s
= in_file
.read(self
.buffer_size
)
636 def get_all_tracks(self
):
639 if self
._config
.fssync_channel_subfolders
:
640 files
= glob
.glob(os
.path
.join(self
.destination
, '*', '*'))
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
))
655 t
= SyncTrack(title
, length
, modified
, modified_sort
=timestamp
, filename
=filename
, podcast
=podcast_name
)
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
:
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
):
686 # No scrobbler log on that device
689 def copy_player_cover_art(self
, destination
, local_filename
, \
690 cover_dst_name
, cover_dst_format
, \
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.
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
)
708 cover
= Image
.open(cover_loc
)
709 cover
.thumbnail(size
)
710 cover
.save(cover_dst
, cover_dst_format
)
712 log('Cannot create %s (PIL?)', cover_dst
, traceback
=True, sender
=self
)
715 log('No cover available to set as player cover', sender
=self
)
718 log('Error getting cover using channel cover', sender
=self
)
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 """
727 log('Opening "%s" as AudioScrobbler log.', log_file
, sender
=self
)
728 f
= open(log_file
, 'r')
729 entries
= f
.readlines()
731 except IOError, ioerror
:
732 log('Error: "%s" cannot be read.', log_file
, sender
=self
)
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)])
745 log('Error while parsing "%s".', log_file
, sender
=self
)
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
):
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
768 the string representation od the given date
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
)
779 def __mtp_to_date(self
, mtp
):
781 this parse the mtp's string representation for date
782 according to specifications (YYYYMMDDThhmmss.s) to
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
)
794 # TIME ZONE SHIFTING: the string contains a hour/min shift relative to a time zone
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
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
)
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
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
840 log("opening the MTP device", sender
=self
)
841 self
.notify('status', _('Opening the MTP device'), )
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)
851 self
.notify('status', _('%s opened') % self
.get_name())
855 log("closing %s", self
.get_name(), sender
=self
)
856 self
.notify('status', _('Closing %s') % self
.get_name())
859 self
.__MTPDevice
.disconnect()
860 except Exception, exc
:
861 log('unable to close %s (%s)', self
.get_name(), exc
, sender
=self
)
864 self
.notify('status', _('%s closed') % self
.get_name())
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
)
875 needed
= util
.calculate_size(filename
)
876 free
= self
.get_free_space()
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
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
))
892 self
.__MTPDevice
.send_track_from_file(filename
,
893 episode
.basename
+ episode
.extension(),
894 metadata
, 0, callback
=self
.__callback
)
896 log('unable to add episode %s', episode
.title
, sender
=self
, traceback
=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
)
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
):
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
)
919 for track
in listing
:
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
927 date
= self
.__mtp
_to
_date
(track
.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
932 modified
= util
.format_date(date
)
935 t
= SyncTrack(title
, length
, modified
, modified_sort
=modified_sort
, mtptrack
=track
, podcast
=artist
)
939 def get_free_space(self
):
940 return self
.__MTPDevice
.get_freespace()