1
|
|
|
|
2
|
|
|
import sys |
3
|
|
|
import codecs |
4
|
|
|
import os.path |
5
|
|
|
|
6
|
|
|
from werkzeug.utils import cached_property |
7
|
|
|
from browsepy.compat import range, PY_LEGACY |
8
|
|
|
from browsepy.file import Node, File, Directory, \ |
9
|
|
|
underscore_replace, check_under_base |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
if PY_LEGACY: |
13
|
|
|
import ConfigParser as configparser |
14
|
|
|
else: |
15
|
|
|
import configparser |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
extensions = { |
19
|
|
|
'mp3': 'audio/mpeg', |
20
|
|
|
'ogg': 'audio/ogg', |
21
|
|
|
'wav': 'audio/wav', |
22
|
|
|
'm3u': 'audio/x-mpegurl', |
23
|
|
|
'm3u8': 'audio/x-mpegurl', |
24
|
|
|
'pls': 'audio/x-scpls', |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
class PlayableFile(File): |
29
|
|
|
media_map = { |
30
|
|
|
'audio/mpeg': 'mp3', |
31
|
|
|
'audio/ogg': 'ogg', |
32
|
|
|
'audio/wav': 'wav', |
33
|
|
|
} |
34
|
|
|
mimetypes = tuple(media_map) |
35
|
|
|
|
36
|
|
|
def __init__(self, **kwargs): |
37
|
|
|
self.duration = kwargs.pop('duration', None) |
38
|
|
|
self.title = kwargs.pop('title', None) |
39
|
|
|
super(PlayableFile, self).__init__(**kwargs) |
40
|
|
|
|
41
|
|
|
@property |
42
|
|
|
def title(self): |
43
|
|
|
return self._title or self.name |
44
|
|
|
|
45
|
|
|
@title.setter |
46
|
|
|
def title(self, title): |
47
|
|
|
self._title = title |
48
|
|
|
|
49
|
|
|
@property |
50
|
|
|
def media_format(self): |
51
|
|
|
return self.media_map[self.type] |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
class PlayListFile(Directory): |
55
|
|
|
playable_class = PlayableFile |
56
|
|
|
mimetypes = ('audio/x-mpegurl', 'audio/x-scpls') |
57
|
|
|
|
58
|
|
|
@classmethod |
59
|
|
|
def from_urlpath(cls, path, app=None): |
60
|
|
|
original = Node.from_urlpath(path, app) |
61
|
|
|
if original.mimetype == PlayableDirectory.mimetype: |
62
|
|
|
return PlayableDirectory(original.path, original.app) |
63
|
|
|
elif original.mimetype == M3UFile.mimetype: |
64
|
|
|
return M3UFile(original.path, original.app) |
65
|
|
|
if original.mimetype == PLSFile.mimetype: |
66
|
|
|
return PLSFile(original.path, original.app) |
67
|
|
|
return original |
68
|
|
|
|
69
|
|
|
def normalize_playable_path(self, path): |
70
|
|
|
if not os.path.isabs(path): |
71
|
|
|
path = os.path.normpath(os.path.join(self.parent.path, path)) |
72
|
|
|
if check_under_base(path, self.app.config['directory_base']): |
73
|
|
|
return path |
74
|
|
|
return None |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
class PLSFile(PlayListFile): |
78
|
|
|
ini_parser_cls = ( |
79
|
|
|
configparser.SafeConfigParser |
80
|
|
|
if hasattr(configparser, 'SafeConfigParser') else |
81
|
|
|
configparser.ConfigParser |
82
|
|
|
) |
83
|
|
|
maxsize = getattr(sys, 'maxsize', None) or getattr(sys, 'maxint', None) |
84
|
|
|
mimetype = 'audio/x-scpls' |
85
|
|
|
|
86
|
|
|
@cached_property |
87
|
|
|
def _parser(self): |
88
|
|
|
parser = self.ini_parser() |
89
|
|
|
parser.read(self.path) |
90
|
|
|
return parser |
91
|
|
|
|
92
|
|
|
def _listdir(self): |
93
|
|
|
maxsize = self._parser.getint('playlist', 'NumberOfEntries', None) |
94
|
|
|
for i in range(self.maxsize if maxsize is None else maxsize): |
95
|
|
|
pf = self.playable_class( |
96
|
|
|
path=self.normalize_playable_path( |
97
|
|
|
self._parser.get('playlist', 'File%d' % i, None) |
98
|
|
|
), |
99
|
|
|
duration=self._parser.getint('playlist', 'Length%d' % i, None), |
100
|
|
|
title=self._parser.get('playlist', 'Title%d' % i, None), |
101
|
|
|
) |
102
|
|
|
if pf.path: |
103
|
|
|
yield pf |
104
|
|
|
elif maxsize is None: |
105
|
|
|
break |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
class M3UFile(PlayListFile): |
109
|
|
|
mimetype = 'audio/x-mpegurl' |
110
|
|
|
|
111
|
|
|
def _iter_lines(self): |
112
|
|
|
prefix = '#EXTM3U\n' |
113
|
|
|
encoding = 'utf-8' if self.path.endswith('.m3u8') else 'ascii' |
114
|
|
|
with codecs.open( |
115
|
|
|
self.path, 'r', |
116
|
|
|
encoding=encoding, |
117
|
|
|
errors=underscore_replace |
118
|
|
|
) as f: |
119
|
|
|
if f.read(len(prefix)) != prefix: |
120
|
|
|
f.seek(0) |
121
|
|
|
for line in f: |
122
|
|
|
line = line.rstrip('\n') |
123
|
|
|
if line: |
124
|
|
|
yield line |
125
|
|
|
|
126
|
|
|
def _listdir(self): |
127
|
|
|
data = {} |
128
|
|
|
for line in self._iter_lines(): |
129
|
|
|
if line.startswith('#EXTINF:'): |
130
|
|
|
duration, title = line.split(',', 1) |
131
|
|
|
data['duration'] = None if duration == '-1' else int(duration) |
132
|
|
|
data['title'] = title |
133
|
|
|
continue |
134
|
|
|
print(line) |
135
|
|
|
data['path'] = self.normalize_playable_path(line) |
136
|
|
|
if data['path']: |
137
|
|
|
yield self.playable_class(**data) |
138
|
|
|
data.clear() |
139
|
|
|
|
140
|
|
|
|
141
|
|
|
class PlayableDirectory(Directory): |
142
|
|
|
@classmethod |
143
|
|
|
def detect(cls, node): |
144
|
|
|
if node.is_directory: |
145
|
|
|
for file in node._listdir(): |
146
|
|
|
if file.name.rsplit('.', 1)[-1] in extensions: |
147
|
|
|
return True |
148
|
|
|
return False |
149
|
|
|
|
150
|
|
|
def _listdir(self): |
151
|
|
|
for file in super(PlayableDirectory, self)._listdir(): |
152
|
|
|
if file.name.rsplit('.', 1)[-1] in extensions: |
153
|
|
|
yield file |
154
|
|
|
|