e6c60e7e46e3c2b4752f1c72ad7374df10ee616a
[youtube-dl] / youtube_dl / extractor / allmyvideos.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import os.path
5 import re
6
7 from .common import InfoExtractor
8 from ..utils import (
9     compat_urllib_parse,
10     compat_urllib_request,
11 )
12
13
14 class AllmyvideosIE(InfoExtractor):
15     IE_NAME = 'allmyvideos.net'
16     _VALID_URL = r'https?://allmyvideos\.net/(?P<id>[a-zA-Z0-9_-]+)'
17
18     _TEST = {
19         'url': 'http://allmyvideos.net/jih3nce3x6wn',
20         'md5': '710883dee1bfc370ecf9fa6a89307c88',
21         'info_dict': {
22             'id': 'jih3nce3x6wn',
23             'ext': 'mp4',
24             'title': 'youtube-dl test video',
25         },
26     }
27
28     def _real_extract(self, url):
29         mobj = re.match(self._VALID_URL, url)
30         video_id = mobj.group('id')
31
32         orig_webpage = self._download_webpage(url, video_id)
33         fields = re.findall(r'type="hidden" name="(.+?)"\s* value="?(.+?)">', orig_webpage)
34         data = dict(fields)
35
36         post = compat_urllib_parse.urlencode(data)
37         headers = {
38             b'Content-Type': b'application/x-www-form-urlencoded',
39         }
40         req = compat_urllib_request.Request(url, post, headers)
41         webpage = self._download_webpage(
42             req, video_id, note='Downloading video page ...')
43
44         title = os.path.splitext(data['fname'])[0]
45
46         #Could be several links with different quality
47         links = re.findall(r'"file" : "?(.+?)",', webpage)
48         # Assume the links are ordered in quality
49         formats = [{
50             'url': l,
51             'quality': i,
52         } for i, l in enumerate(links)]
53         self._sort_formats(formats)
54
55         return {
56             'id': video_id,
57             'title': title,
58             'formats': formats,
59         }