Merge remote-tracking branch 'r4mos/played'
[youtube-dl] / youtube_dl / extractor / played.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5 import time
6 import os.path
7
8 from .common import InfoExtractor
9 from ..utils import (
10     compat_urllib_parse,
11     compat_urllib_request,
12 )
13
14
15 class PlayedIE(InfoExtractor):
16     IE_NAME = 'played.to'
17     _VALID_URL = r'https?://played\.to/(?P<id>[a-zA-Z0-9_-]+)'
18
19     _TEST = {
20         'url': 'http://played.to/j2f2sfiiukgt',
21         'md5': 'c2bd75a368e82980e7257bf500c00637',
22         'info_dict': {
23             'id': 'j2f2sfiiukgt',
24             'ext': 'flv',
25             'title': 'youtube-dl_test_video.mp4',
26         },
27     }
28
29     def _real_extract(self, url):
30         mobj = re.match(self._VALID_URL, url)
31         video_id = mobj.group('id')
32
33         orig_webpage = self._download_webpage(url, video_id)
34         fields = re.findall(r'type="hidden" name="(.+?)"\s* value="?(.+?)">', orig_webpage)
35         data = dict(fields)
36
37         self.to_screen('%s: Waiting for timeout' % video_id)
38         time.sleep(2)
39
40         post = compat_urllib_parse.urlencode(data)
41         headers = {
42             b'Content-Type': b'application/x-www-form-urlencoded',
43         }
44         req = compat_urllib_request.Request(url, post, headers)
45         webpage = self._download_webpage(
46             req, video_id, note='Downloading video page ...')
47
48         title = os.path.splitext(data['fname'])[0]
49
50         video_url = self._search_regex(
51             r'file: "?(.+?)",', webpage, 'video URL')
52
53         return {
54             'id': video_id,
55             'title': title,
56             'url': video_url,
57         }