[played] Simplify (#3798)
[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?://(?:www\.)?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         video_id = self._match_id(url)
31
32         orig_webpage = self._download_webpage(url, video_id)
33         fields = re.findall(
34             r'type="hidden" name="([^"]+)"\s+value="([^"]+)">', orig_webpage)
35         data = dict(fields)
36
37         self._sleep(2, video_id)
38
39         post = compat_urllib_parse.urlencode(data)
40         headers = {
41             b'Content-Type': b'application/x-www-form-urlencoded',
42         }
43         req = compat_urllib_request.Request(url, post, headers)
44         webpage = self._download_webpage(
45             req, video_id, note='Downloading video page ...')
46
47         title = os.path.splitext(data['fname'])[0]
48
49         video_url = self._search_regex(
50             r'file: "?(.+?)",', webpage, 'video URL')
51
52         return {
53             'id': video_id,
54             'title': title,
55             'url': video_url,
56         }