-
Notifications
You must be signed in to change notification settings - Fork 136
/
backends.py
423 lines (330 loc) · 10.5 KB
/
backends.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
import re
import sys
import json
import requests
if sys.version_info.major == 3:
import urllib.parse as urlparse
else:
import urlparse
from django.http import QueryDict
from django.template.loader import render_to_string
from django.utils.functional import cached_property
from django.utils.safestring import mark_safe
from .utils import import_by_path
from .settings import EMBED_VIDEO_BACKENDS, EMBED_VIDEO_TIMEOUT, \
EMBED_VIDEO_YOUTUBE_DEFAULT_QUERY
class EmbedVideoException(Exception):
""" Parental class for all embed_video exceptions """
pass
class VideoDoesntExistException(EmbedVideoException):
""" Exception thrown if video doesn't exist """
pass
class UnknownBackendException(EmbedVideoException):
""" Exception thrown if video backend is not recognized. """
pass
class UnknownIdException(VideoDoesntExistException):
"""
Exception thrown if backend is detected, but video ID cannot be parsed.
"""
pass
def detect_backend(url):
"""
Detect the right backend for given URL.
Goes over backends in ``settings.EMBED_VIDEO_BACKENDS``,
calls :py:func:`~VideoBackend.is_valid` and returns backend instance.
:param url: URL which is passed to `is_valid` methods of VideoBackends.
:type url: str
:return: Returns recognized VideoBackend
:rtype: VideoBackend
"""
for backend_name in EMBED_VIDEO_BACKENDS:
backend = import_by_path(backend_name)
if backend.is_valid(url):
return backend(url)
raise UnknownBackendException
class VideoBackend(object):
"""
Base class used as parental class for backends.
Backend variables:
.. autosummary::
url
code
thumbnail
query
info
is_secure
protocol
template_name
.. code-block:: python
class MyBackend(VideoBackend):
...
"""
re_code = None
"""
Compiled regex (:py:func:`re.compile`) to search code in URL.
Example: ``re.compile(r'myvideo\.com/\?code=(?P<code>\w+)')``
"""
re_detect = None
"""
Compilede regec (:py:func:`re.compile`) to detect, if input URL is valid
for current backend.
Example: ``re.compile(r'^http://myvideo\.com/.*')``
"""
pattern_url = None
"""
Pattern in which the code is inserted.
Example: ``http://myvideo.com?code=%s``
:type: str
"""
pattern_thumbnail_url = None
"""
Pattern in which the code is inserted to get thumbnail url.
Example: ``http://static.myvideo.com/thumbs/%s``
:type: str
"""
allow_https = True
"""
Sets if HTTPS version allowed for specific backend.
:type: bool
"""
template_name = 'embed_video/embed_code.html'
"""
Name of embed code template used by :py:meth:`get_embed_code`.
Passed template variables: ``{{ backend }}`` (instance of VideoBackend),
``{{ width }}``, ``{{ height }}``
:type: str
"""
default_query = ''
"""
Default query string or `QueryDict` appended to url
:type: str
"""
is_secure = False
"""
Decides if secured protocol (HTTPS) is used.
:type: bool
"""
def __init__(self, url):
"""
First it tries to load data from cache and if it don't succeed, run
:py:meth:`init` and then save it to cache.
:type url: str
"""
self.backend = self.__class__.__name__
self._url = url
self.query = QueryDict(self.default_query, mutable=True)
@property
def code(self):
"""
Code of video.
"""
return self.get_code()
@property
def url(self):
"""
URL of video.
"""
return self.get_url()
@property
def protocol(self):
"""
Protocol used to generate URL.
"""
return 'https' if self.allow_https and self.is_secure else 'http'
@property
def thumbnail(self):
"""
URL of video thumbnail.
"""
return self.get_thumbnail_url()
@property
def info(self):
"""
Additional information about video. Not implemented in all backends.
"""
return self.get_info()
@property
def query(self):
"""
String transformed to QueryDict appended to url.
"""
return self._query
@query.setter
def query(self, value):
"""
:type value: QueryDict | str
"""
self._query = value \
if isinstance(value, QueryDict) \
else QueryDict(value, mutable=True)
@classmethod
def is_valid(cls, url):
"""
Class method to control if passed url is valid for current backend. By
default it is done by :py:data:`re_detect` regex.
:type url: str
"""
return True if cls.re_detect.match(url) else False
def get_code(self):
"""
Returns video code matched from given url by :py:data:`re_code`.
:rtype: str
"""
match = self.re_code.search(self._url)
if match:
return match.group('code')
def get_url(self):
"""
Returns URL folded from :py:data:`pattern_url` and parsed code.
"""
url = self.pattern_url.format(code=self.code, protocol=self.protocol)
url += '?' + self.query.urlencode() if self.query else ''
return mark_safe(url)
def get_thumbnail_url(self):
"""
Returns thumbnail URL folded from :py:data:`pattern_thumbnail_url` and
parsed code.
:rtype: str
"""
return self.pattern_thumbnail_url.format(code=self.code,
protocol=self.protocol)
def get_embed_code(self, width, height):
"""
Returns embed code rendered from template :py:data:`template_name`.
:type width: int | str
:type height: int | str
:rtype: str
"""
return render_to_string(self.template_name, {
'backend': self,
'width': width,
'height': height,
})
def get_info(self):
"""
:rtype: dict
"""
raise NotImplementedError
def set_options(self, options):
"""
:type options: dict
"""
for key in options:
setattr(self, key, options[key])
class YoutubeBackend(VideoBackend):
"""
Backend for YouTube URLs.
"""
re_detect = re.compile(
r'^(http(s)?://)?(www\.|m\.)?youtu(\.?)be(\.com)?/.*', re.I
)
re_code = re.compile(
r'''youtu(\.?)be(\.com)?/ # match youtube's domains
(\#/)? # for mobile urls
(embed/)? # match the embed url syntax
(v/)?
(watch\?v=)? # match the youtube page url
(ytscreeningroom\?v=)?
(feeds/api/videos/)?
(user\S*[^\w\-\s])?
(?P<code>[\w\-]{11})[a-z0-9;:@?&%=+/\$_.-]* # match and extract
''',
re.I | re.X
)
pattern_url = '{protocol}://www.youtube.com/embed/{code}'
pattern_thumbnail_url = '{protocol}://img.youtube.com/vi/{code}/{resolution}'
default_query = EMBED_VIDEO_YOUTUBE_DEFAULT_QUERY
resolutions = [
'maxresdefault.jpg',
'sddefault.jpg',
'hqdefault.jpg',
'mqdefault.jpg',
]
def get_code(self):
code = super(YoutubeBackend, self).get_code()
if not code:
parsed_url = urlparse.urlparse(self._url)
parsed_qs = urlparse.parse_qs(parsed_url.query)
if 'v' in parsed_qs:
code = parsed_qs['v'][0]
elif 'video_id' in parsed_qs:
code = parsed_qs['video_id'][0]
else:
raise UnknownIdException('Cannot get ID from `{0}`'.format(self._url))
return code
def get_thumbnail_url(self):
"""
Returns thumbnail URL folded from :py:data:`pattern_thumbnail_url` and
parsed code.
:rtype: str
"""
for resolution in self.resolutions:
temp_thumbnail_url = self.pattern_thumbnail_url.format(
code=self.code, protocol=self.protocol, resolution=resolution)
if int(requests.head(temp_thumbnail_url).status_code) < 400:
return temp_thumbnail_url
return None
class VimeoBackend(VideoBackend):
"""
Backend for Vimeo URLs.
"""
re_detect = re.compile(
r'^((http(s)?:)?//)?(www\.)?(player\.)?vimeo\.com/.*', re.I
)
re_code = re.compile(r'''vimeo\.com/(video/)?(channels/(.*/)?)?(?P<code>[0-9]+)''', re.I)
pattern_url = '{protocol}://player.vimeo.com/video/{code}'
pattern_info = '{protocol}://vimeo.com/api/v2/video/{code}.json'
def get_info(self):
try:
response = requests.get(
self.pattern_info.format(code=self.code, protocol=self.protocol),
timeout=EMBED_VIDEO_TIMEOUT
)
return json.loads(response.text)[0]
except ValueError:
raise VideoDoesntExistException()
def get_thumbnail_url(self):
return self.info.get('thumbnail_large')
class SoundCloudBackend(VideoBackend):
"""
Backend for SoundCloud URLs.
"""
base_url = 'http://soundcloud.com/oembed'
re_detect = re.compile(r'^(http(s)?://(www\.)?)?soundcloud\.com/.*', re.I)
re_code = re.compile(r'src=".*%2F(?P<code>\d+)&show_artwork.*"', re.I)
re_url = re.compile(r'src="(?P<url>.*?)"', re.I)
@cached_property
def width(self):
"""
:rtype: str
"""
return self.info.get('width')
@cached_property
def height(self):
"""
:rtype: str
"""
return self.info.get('height')
def get_info(self):
params = {
'format': 'json',
'url': self._url,
}
r = requests.get(self.base_url, params=params,
timeout=EMBED_VIDEO_TIMEOUT)
if r.status_code != 200:
raise VideoDoesntExistException(
'SoundCloud returned status code `{0}`.'.format(r.status_code)
)
return json.loads(r.text)
def get_thumbnail_url(self):
return self.info.get('thumbnail_url')
def get_url(self):
match = self.re_url.search(self.info.get('html'))
return match.group('url')
def get_code(self):
match = self.re_code.search(self.info.get('html'))
return match.group('code')
def get_embed_code(self, width, height):
return super(SoundCloudBackend, self). \
get_embed_code(width=width, height=height)