Merge remote-tracking branch 'upstream/master'
[youtube-dl] / youtube_dl / extractor / googleplus.py
1 # coding: utf-8
2
3 import datetime
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import (
8     ExtractorError,
9 )
10
11
12 class GooglePlusIE(InfoExtractor):
13     IE_DESC = u'Google Plus'
14     _VALID_URL = r'(?:https://)?plus\.google\.com/(?:[^/]+/)*?posts/(\w+)'
15     IE_NAME = u'plus.google'
16     _TEST = {
17         u"url": u"https://plus.google.com/u/0/108897254135232129896/posts/ZButuJc6CtH",
18         u"file": u"ZButuJc6CtH.flv",
19         u"info_dict": {
20             u"upload_date": u"20120613",
21             u"uploader": u"井上ヨシマサ",
22             u"title": u"嘆きの天使 降臨"
23         }
24     }
25
26     def _real_extract(self, url):
27         # Extract id from URL
28         mobj = re.match(self._VALID_URL, url)
29         if mobj is None:
30             raise ExtractorError(u'Invalid URL: %s' % url)
31
32         post_url = mobj.group(0)
33         video_id = mobj.group(1)
34
35         video_extension = 'flv'
36
37         # Step 1, Retrieve post webpage to extract further information
38         webpage = self._download_webpage(post_url, video_id, u'Downloading entry webpage')
39
40         self.report_extraction(video_id)
41
42         # Extract update date
43         upload_date = self._html_search_regex('title="Timestamp">(.*?)</a>',
44             webpage, u'upload date', fatal=False)
45         if upload_date:
46             # Convert timestring to a format suitable for filename
47             upload_date = datetime.datetime.strptime(upload_date, "%Y-%m-%d")
48             upload_date = upload_date.strftime('%Y%m%d')
49
50         # Extract uploader
51         uploader = self._html_search_regex(r'rel\="author".*?>(.*?)</a>',
52             webpage, u'uploader', fatal=False)
53
54         # Extract title
55         # Get the first line for title
56         video_title = self._html_search_regex(r'<meta name\=\"Description\" content\=\"(.*?)[\n<"]',
57             webpage, 'title', default=u'NA')
58
59         # Step 2, Simulate clicking the image box to launch video
60         DOMAIN = 'https://plus.google.com/'
61         video_page = self._search_regex(r'<a href="((?:%s)?photos/.*?)"' % re.escape(DOMAIN),
62             webpage, u'video page URL')
63         if not video_page.startswith(DOMAIN):
64             video_page = DOMAIN + video_page
65
66         webpage = self._download_webpage(video_page, video_id, u'Downloading video page')
67
68         # Extract video links on video page
69         """Extract video links of all sizes"""
70         pattern = r'\d+,\d+,(\d+),"(http\://redirector\.googlevideo\.com.*?)"'
71         mobj = re.findall(pattern, webpage)
72         if len(mobj) == 0:
73             raise ExtractorError(u'Unable to extract video links')
74
75         # Sort in resolution
76         links = sorted(mobj)
77
78         # Choose the lowest of the sort, i.e. highest resolution
79         video_url = links[-1]
80         # Only get the url. The resolution part in the tuple has no use anymore
81         video_url = video_url[-1]
82         # Treat escaped \u0026 style hex
83         try:
84             video_url = video_url.decode("unicode_escape")
85         except AttributeError: # Python 3
86             video_url = bytes(video_url, 'ascii').decode('unicode-escape')
87
88
89         return [{
90             'id':       video_id,
91             'url':      video_url,
92             'uploader': uploader,
93             'upload_date':  upload_date,
94             'title':    video_title,
95             'ext':      video_extension,
96         }]