Merge remote-tracking branch 'dstftw/generic-webpage-unescape'
[youtube-dl] / youtube_dl / extractor / cspan.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..utils import (
7     unescapeHTML,
8     find_xpath_attr,
9 )
10
11
12 class CSpanIE(InfoExtractor):
13     _VALID_URL = r'http://(?:www\.)?c-span\.org/video/\?(?P<id>[0-9a-f]+)'
14     IE_DESC = 'C-SPAN'
15     _TESTS = [{
16         'url': 'http://www.c-span.org/video/?313572-1/HolderonV',
17         'md5': '8e44ce11f0f725527daccc453f553eb0',
18         'info_dict': {
19             'id': '315139',
20             'ext': 'mp4',
21             'title': 'Attorney General Eric Holder on Voting Rights Act Decision',
22             'description': 'Attorney General Eric Holder spoke to reporters following the Supreme Court decision in Shelby County v. Holder in which the court ruled that the preclearance provisions of the Voting Rights Act could not be enforced until Congress established new guidelines for review.',
23         },
24         'skip': 'Regularly fails on travis, for unknown reasons',
25     }, {
26         'url': 'http://www.c-span.org/video/?c4486943/cspan-international-health-care-models',
27         # For whatever reason, the served video alternates between
28         # two different ones
29         #'md5': 'dbb0f047376d457f2ab8b3929cbb2d0c',
30         'info_dict': {
31             'id': '340723',
32             'ext': 'mp4',
33             'title': 'International Health Care Models',
34             'description': 'md5:7a985a2d595dba00af3d9c9f0783c967',
35         }
36     }]
37
38     def _real_extract(self, url):
39         mobj = re.match(self._VALID_URL, url)
40         page_id = mobj.group('id')
41         webpage = self._download_webpage(url, page_id)
42         video_id = self._search_regex(r'progid=\'?([0-9]+)\'?>', webpage, 'video id')
43
44         description = self._html_search_regex(
45             [
46                 # The full description
47                 r'<div class=\'expandable\'>(.*?)<a href=\'#\'',
48                 # If the description is small enough the other div is not
49                 # present, otherwise this is a stripped version
50                 r'<p class=\'initial\'>(.*?)</p>'
51             ],
52             webpage, 'description', flags=re.DOTALL)
53
54         info_url = 'http://c-spanvideo.org/videoLibrary/assets/player/ajax-player.php?os=android&html5=program&id=' + video_id
55         data = self._download_json(info_url, video_id)
56
57         url = unescapeHTML(data['video']['files'][0]['path']['#text'])
58
59         doc = self._download_xml('http://www.c-span.org/common/services/flashXml.php?programid=' + video_id,
60             video_id)
61
62         def find_string(s):
63             return find_xpath_attr(doc, './/string', 'name', s).text
64
65         return {
66             'id': video_id,
67             'title': find_string('title'),
68             'url': url,
69             'description': description,
70             'thumbnail': find_string('poster'),
71         }