Switch codebase to use sanitized_Request instead of
[youtube-dl] / youtube_dl / extractor / flickr.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..utils import (
7     ExtractorError,
8     find_xpath_attr,
9     sanitized_Request,
10 )
11
12
13 class FlickrIE(InfoExtractor):
14     _VALID_URL = r'https?://(?:www\.|secure\.)?flickr\.com/photos/(?P<uploader_id>[\w\-_@]+)/(?P<id>\d+).*'
15     _TEST = {
16         'url': 'http://www.flickr.com/photos/forestwander-nature-pictures/5645318632/in/photostream/',
17         'md5': '6fdc01adbc89d72fc9c4f15b4a4ba87b',
18         'info_dict': {
19             'id': '5645318632',
20             'ext': 'mp4',
21             "description": "Waterfalls in the Springtime at Dark Hollow Waterfalls. These are located just off of Skyline Drive in Virginia. They are only about 6/10 of a mile hike but it is a pretty steep hill and a good climb back up.",
22             "uploader_id": "forestwander-nature-pictures",
23             "title": "Dark Hollow Waterfalls"
24         }
25     }
26
27     def _real_extract(self, url):
28         mobj = re.match(self._VALID_URL, url)
29
30         video_id = mobj.group('id')
31         video_uploader_id = mobj.group('uploader_id')
32         webpage_url = 'http://www.flickr.com/photos/' + video_uploader_id + '/' + video_id
33         req = sanitized_Request(webpage_url)
34         req.add_header(
35             'User-Agent',
36             # it needs a more recent version
37             'Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20150101 Firefox/38.0 (Chrome)')
38         webpage = self._download_webpage(req, video_id)
39
40         secret = self._search_regex(r'secret"\s*:\s*"(\w+)"', webpage, 'secret')
41
42         first_url = 'https://secure.flickr.com/apps/video/video_mtl_xml.gne?v=x&photo_id=' + video_id + '&secret=' + secret + '&bitrate=700&target=_self'
43         first_xml = self._download_xml(first_url, video_id, 'Downloading first data webpage')
44
45         node_id = find_xpath_attr(
46             first_xml, './/{http://video.yahoo.com/YEP/1.0/}Item', 'id',
47             'id').text
48
49         second_url = 'https://secure.flickr.com/video_playlist.gne?node_id=' + node_id + '&tech=flash&mode=playlist&bitrate=700&secret=' + secret + '&rd=video.yahoo.com&noad=1'
50         second_xml = self._download_xml(second_url, video_id, 'Downloading second data webpage')
51
52         self.report_extraction(video_id)
53
54         stream = second_xml.find('.//STREAM')
55         if stream is None:
56             raise ExtractorError('Unable to extract video url')
57         video_url = stream.attrib['APP'] + stream.attrib['FULLPATH']
58
59         return {
60             'id': video_id,
61             'url': video_url,
62             'ext': 'mp4',
63             'title': self._og_search_title(webpage),
64             'description': self._og_search_description(webpage),
65             'thumbnail': self._og_search_thumbnail(webpage),
66             'uploader_id': video_uploader_id,
67         }