1
# -*- coding: utf-8 -*-
3
# gPodder - A media aggregator and podcast client
4
# Copyright (c) 2005-2011 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/>.
25
from gpodder import util
28
logger = logging.getLogger(__name__)
30
class gPodderDevicePlaylist(object):
31
def __init__(self, config, playlist_name):
33
self.linebreak = '\r\n'
34
self.playlist_file=util.sanitize_filename(playlist_name + '.m3u')
35
self.playlist_folder = os.path.join(self._config.device_sync.device_folder, self._config.device_sync.playlists.folder)
36
self.mountpoint = util.find_mount_point(util.sanitize_encoding(self.playlist_folder))
37
if self.mountpoint == '/':
38
self.mountpoint = self.playlist_folder
39
logger.warning('MP3 player resides on / - using %s as MP3 player root', self.mountpoint)
40
self.playlist_absolute_filename=os.path.join(self.playlist_folder, self.playlist_file)
42
def build_extinf(self, filename):
43
#TO DO: Windows playlists
44
# if self._config.mp3_player_playlist_win_path:
45
# filename = filename.replace('\\', os.sep)
47
# # rebuild the whole filename including the mountpoint
48
# if self._config.device_sync.playlist_absolute_path:
49
# absfile = os.path.join(self.mountpoint,filename)
50
# else: #TODO: Test rel filenames
51
# absfile = util.rel2abs(filename, os.path.dirname(self.playlist_file))
53
# fallback: use the basename of the file
54
(title, extension) = os.path.splitext(os.path.basename(filename))
56
return "#EXTINF:0,%s%s" % (title.strip(), self.linebreak)
60
read all files from the existing playlist
63
logger.info("Read data from the playlistfile %s" % self.playlist_absolute_filename)
64
if os.path.exists(self.playlist_absolute_filename):
65
for line in open(self.playlist_absolute_filename, 'r'):
66
if not line.startswith('#EXT'):
67
tracks.append(line.rstrip('\r\n'))
70
def get_filename_for_playlist(self, episode):
72
get the filename for the given episode for the playlist
74
filename_base = util.sanitize_filename(episode.sync_filename(
75
self._config.device_sync.custom_sync_name_enabled,
76
self._config.device_sync.custom_sync_name),
77
self._config.device_sync.max_filename_length)
78
filename = filename_base + os.path.splitext(episode.local_filename(create=False))[1].lower()
81
def get_absolute_filename_for_playlist(self, episode):
83
get the filename including full path for the given episode for the playlist
85
filename = self.get_filename_for_playlist(episode)
86
if self._config.device_sync.one_folder_per_podcast:
87
filename = os.path.join(util.sanitize_filename(episode.channel.title), filename)
88
if self._config.device_sync.playlist.absolute_path:
89
filename = os.path.join(util.relpath(self.mountpoint, self._config.device_sync.device_folder), filename)
92
def write_m3u(self, episodes):
94
write the list into the playlist on the device
96
logger.info('Writing playlist file: %s', self.playlist_file)
97
if not util.make_directory(self.playlist_folder):
98
raise IOError(_('Folder %s could not be created.') % self.playlist_folder, _('Error writing playlist'))
100
fp = open(os.path.join(self.playlist_folder, self.playlist_file), 'w')
101
fp.write('#EXTM3U%s' % self.linebreak)
102
for current_episode in episodes:
103
filename_base = util.sanitize_filename(current_episode.sync_filename(
104
self._config.device_sync.custom_sync_name_enabled,
105
self._config.device_sync.custom_sync_name),
106
self._config.device_sync.max_filename_length)
107
filename = filename_base + os.path.splitext(current_episode.local_filename(create=False))[1].lower()
108
filename = self.get_filename_for_playlist(current_episode)
109
fp.write(self.build_extinf(filename))
110
filename = self.get_absolute_filename_for_playlist(current_episode)
112
fp.write(self.linebreak)