[ndtv] Add extractor (Fixes #1924)
[youtube-dl] / youtube_dl / extractor / ndtv.py
1 import json
2 import re
3 import time
4
5 from .common import InfoExtractor
6 from ..utils import month_by_name
7
8
9 class NDTVIE(InfoExtractor):
10     _VALID_URL = r'^https?://(?:www\.)?ndtv\.com/video/player/[^/]*/[^/]*/(?P<id>[a-z0-9]+)'
11
12     _TEST = {
13         u"url": u"http://www.ndtv.com/video/player/news/ndtv-exclusive-don-t-need-character-certificate-from-rahul-gandhi-says-arvind-kejriwal/300710",
14         u"file": u"300710.mp4",
15         u"md5": u"39f992dbe5fb531c395d8bbedb1e5e88",
16         u"info_dict": {
17             u"title": u"NDTV exclusive: Don't need character certificate from Rahul Gandhi, says Arvind Kejriwal",
18             u"description": u"In an exclusive interview to NDTV, Aam Aadmi Party's Arvind Kejriwal says it makes no difference to him that Rahul Gandhi said the Congress needs to learn from his party.",
19             u"upload_date": u"20131208",
20             u"duration": 1327,
21             u"thumbnail": u"http://i.ndtvimg.com/video/images/vod/medium/2013-12/big_300710_1386518307.jpg",
22         },
23     }
24
25     def _real_extract(self, url):
26         mobj = re.match(self._VALID_URL, url)
27         video_id = mobj.group('id')
28
29         webpage = self._download_webpage(url, video_id)
30
31         filename = self._search_regex(
32             r"__filename='([^']+)'", webpage, u'video filename')
33         video_url = (u'http://bitcast-b.bitgravity.com/ndtvod/23372/ndtv/%s' %
34                      filename)
35
36         duration_str = filename = self._search_regex(
37             r"__duration='([^']+)'", webpage, u'duration', fatal=False)
38         duration = None if duration_str is None else int(duration_str)
39
40         date_m = re.search(r'''(?x)
41             <p\s+class="vod_dateline">\s*
42                 Published\s+On:\s*
43                 (?P<monthname>[A-Za-z]+)\s+(?P<day>[0-9]+),\s*(?P<year>[0-9]+)
44             ''', webpage)
45         upload_date = None
46         assert date_m
47         if date_m is not None:
48             month = month_by_name(date_m.group('monthname'))
49             if month is not None:
50                 upload_date = '%s%02d%02d' % (
51                     date_m.group('year'), month, int(date_m.group('day')))
52
53         description = self._og_search_description(webpage)
54         READ_MORE = u' (Read more)'
55         if description.endswith(READ_MORE):
56             description = description[:-len(READ_MORE)]
57
58         return {
59             'id': video_id,
60             'url': video_url,
61             'title': self._og_search_title(webpage),
62             'description': description,
63             'thumbnail': self._og_search_thumbnail(webpage),
64             'duration': duration,
65             'upload_date': upload_date,
66         }