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