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