You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

397 lines
16 KiB

Switch codebase to use sanitized_Request instead of compat_urllib_request.Request [downloader/dash] Use sanitized_Request [downloader/http] Use sanitized_Request [atresplayer] Use sanitized_Request [bambuser] Use sanitized_Request [bliptv] Use sanitized_Request [brightcove] Use sanitized_Request [cbs] Use sanitized_Request [ceskatelevize] Use sanitized_Request [collegerama] Use sanitized_Request [extractor/common] Use sanitized_Request [crunchyroll] Use sanitized_Request [dailymotion] Use sanitized_Request [dcn] Use sanitized_Request [dramafever] Use sanitized_Request [dumpert] Use sanitized_Request [eitb] Use sanitized_Request [escapist] Use sanitized_Request [everyonesmixtape] Use sanitized_Request [extremetube] Use sanitized_Request [facebook] Use sanitized_Request [fc2] Use sanitized_Request [flickr] Use sanitized_Request [4tube] Use sanitized_Request [gdcvault] Use sanitized_Request [extractor/generic] Use sanitized_Request [hearthisat] Use sanitized_Request [hotnewhiphop] Use sanitized_Request [hypem] Use sanitized_Request [iprima] Use sanitized_Request [ivi] Use sanitized_Request [keezmovies] Use sanitized_Request [letv] Use sanitized_Request [lynda] Use sanitized_Request [metacafe] Use sanitized_Request [minhateca] Use sanitized_Request [miomio] Use sanitized_Request [meovideo] Use sanitized_Request [mofosex] Use sanitized_Request [moniker] Use sanitized_Request [mooshare] Use sanitized_Request [movieclips] Use sanitized_Request [mtv] Use sanitized_Request [myvideo] Use sanitized_Request [neteasemusic] Use sanitized_Request [nfb] Use sanitized_Request [niconico] Use sanitized_Request [noco] Use sanitized_Request [nosvideo] Use sanitized_Request [novamov] Use sanitized_Request [nowness] Use sanitized_Request [nuvid] Use sanitized_Request [played] Use sanitized_Request [pluralsight] Use sanitized_Request [pornhub] Use sanitized_Request [pornotube] Use sanitized_Request [primesharetv] Use sanitized_Request [promptfile] Use sanitized_Request [qqmusic] Use sanitized_Request [rtve] Use sanitized_Request [safari] Use sanitized_Request [sandia] Use sanitized_Request [shared] Use sanitized_Request [sharesix] Use sanitized_Request [sina] Use sanitized_Request [smotri] Use sanitized_Request [sohu] Use sanitized_Request [spankwire] Use sanitized_Request [sportdeutschland] Use sanitized_Request [streamcloud] Use sanitized_Request [streamcz] Use sanitized_Request [tapely] Use sanitized_Request [tube8] Use sanitized_Request [tubitv] Use sanitized_Request [twitch] Use sanitized_Request [twitter] Use sanitized_Request [udemy] Use sanitized_Request [vbox7] Use sanitized_Request [veoh] Use sanitized_Request [vessel] Use sanitized_Request [vevo] Use sanitized_Request [viddler] Use sanitized_Request [videomega] Use sanitized_Request [viewvster] Use sanitized_Request [viki] Use sanitized_Request [vk] Use sanitized_Request [vodlocker] Use sanitized_Request [voicerepublic] Use sanitized_Request [wistia] Use sanitized_Request [xfileshare] Use sanitized_Request [xtube] Use sanitized_Request [xvideos] Use sanitized_Request [yandexmusic] Use sanitized_Request [youku] Use sanitized_Request [youporn] Use sanitized_Request [youtube] Use sanitized_Request [patreon] Use sanitized_Request [extractor/common] Remove unused import [nfb] PEP 8
9 years ago
Switch codebase to use sanitized_Request instead of compat_urllib_request.Request [downloader/dash] Use sanitized_Request [downloader/http] Use sanitized_Request [atresplayer] Use sanitized_Request [bambuser] Use sanitized_Request [bliptv] Use sanitized_Request [brightcove] Use sanitized_Request [cbs] Use sanitized_Request [ceskatelevize] Use sanitized_Request [collegerama] Use sanitized_Request [extractor/common] Use sanitized_Request [crunchyroll] Use sanitized_Request [dailymotion] Use sanitized_Request [dcn] Use sanitized_Request [dramafever] Use sanitized_Request [dumpert] Use sanitized_Request [eitb] Use sanitized_Request [escapist] Use sanitized_Request [everyonesmixtape] Use sanitized_Request [extremetube] Use sanitized_Request [facebook] Use sanitized_Request [fc2] Use sanitized_Request [flickr] Use sanitized_Request [4tube] Use sanitized_Request [gdcvault] Use sanitized_Request [extractor/generic] Use sanitized_Request [hearthisat] Use sanitized_Request [hotnewhiphop] Use sanitized_Request [hypem] Use sanitized_Request [iprima] Use sanitized_Request [ivi] Use sanitized_Request [keezmovies] Use sanitized_Request [letv] Use sanitized_Request [lynda] Use sanitized_Request [metacafe] Use sanitized_Request [minhateca] Use sanitized_Request [miomio] Use sanitized_Request [meovideo] Use sanitized_Request [mofosex] Use sanitized_Request [moniker] Use sanitized_Request [mooshare] Use sanitized_Request [movieclips] Use sanitized_Request [mtv] Use sanitized_Request [myvideo] Use sanitized_Request [neteasemusic] Use sanitized_Request [nfb] Use sanitized_Request [niconico] Use sanitized_Request [noco] Use sanitized_Request [nosvideo] Use sanitized_Request [novamov] Use sanitized_Request [nowness] Use sanitized_Request [nuvid] Use sanitized_Request [played] Use sanitized_Request [pluralsight] Use sanitized_Request [pornhub] Use sanitized_Request [pornotube] Use sanitized_Request [primesharetv] Use sanitized_Request [promptfile] Use sanitized_Request [qqmusic] Use sanitized_Request [rtve] Use sanitized_Request [safari] Use sanitized_Request [sandia] Use sanitized_Request [shared] Use sanitized_Request [sharesix] Use sanitized_Request [sina] Use sanitized_Request [smotri] Use sanitized_Request [sohu] Use sanitized_Request [spankwire] Use sanitized_Request [sportdeutschland] Use sanitized_Request [streamcloud] Use sanitized_Request [streamcz] Use sanitized_Request [tapely] Use sanitized_Request [tube8] Use sanitized_Request [tubitv] Use sanitized_Request [twitch] Use sanitized_Request [twitter] Use sanitized_Request [udemy] Use sanitized_Request [vbox7] Use sanitized_Request [veoh] Use sanitized_Request [vessel] Use sanitized_Request [vevo] Use sanitized_Request [viddler] Use sanitized_Request [videomega] Use sanitized_Request [viewvster] Use sanitized_Request [viki] Use sanitized_Request [vk] Use sanitized_Request [vodlocker] Use sanitized_Request [voicerepublic] Use sanitized_Request [wistia] Use sanitized_Request [xfileshare] Use sanitized_Request [xtube] Use sanitized_Request [xvideos] Use sanitized_Request [yandexmusic] Use sanitized_Request [youku] Use sanitized_Request [youporn] Use sanitized_Request [youtube] Use sanitized_Request [patreon] Use sanitized_Request [extractor/common] Remove unused import [nfb] PEP 8
9 years ago
Switch codebase to use sanitized_Request instead of compat_urllib_request.Request [downloader/dash] Use sanitized_Request [downloader/http] Use sanitized_Request [atresplayer] Use sanitized_Request [bambuser] Use sanitized_Request [bliptv] Use sanitized_Request [brightcove] Use sanitized_Request [cbs] Use sanitized_Request [ceskatelevize] Use sanitized_Request [collegerama] Use sanitized_Request [extractor/common] Use sanitized_Request [crunchyroll] Use sanitized_Request [dailymotion] Use sanitized_Request [dcn] Use sanitized_Request [dramafever] Use sanitized_Request [dumpert] Use sanitized_Request [eitb] Use sanitized_Request [escapist] Use sanitized_Request [everyonesmixtape] Use sanitized_Request [extremetube] Use sanitized_Request [facebook] Use sanitized_Request [fc2] Use sanitized_Request [flickr] Use sanitized_Request [4tube] Use sanitized_Request [gdcvault] Use sanitized_Request [extractor/generic] Use sanitized_Request [hearthisat] Use sanitized_Request [hotnewhiphop] Use sanitized_Request [hypem] Use sanitized_Request [iprima] Use sanitized_Request [ivi] Use sanitized_Request [keezmovies] Use sanitized_Request [letv] Use sanitized_Request [lynda] Use sanitized_Request [metacafe] Use sanitized_Request [minhateca] Use sanitized_Request [miomio] Use sanitized_Request [meovideo] Use sanitized_Request [mofosex] Use sanitized_Request [moniker] Use sanitized_Request [mooshare] Use sanitized_Request [movieclips] Use sanitized_Request [mtv] Use sanitized_Request [myvideo] Use sanitized_Request [neteasemusic] Use sanitized_Request [nfb] Use sanitized_Request [niconico] Use sanitized_Request [noco] Use sanitized_Request [nosvideo] Use sanitized_Request [novamov] Use sanitized_Request [nowness] Use sanitized_Request [nuvid] Use sanitized_Request [played] Use sanitized_Request [pluralsight] Use sanitized_Request [pornhub] Use sanitized_Request [pornotube] Use sanitized_Request [primesharetv] Use sanitized_Request [promptfile] Use sanitized_Request [qqmusic] Use sanitized_Request [rtve] Use sanitized_Request [safari] Use sanitized_Request [sandia] Use sanitized_Request [shared] Use sanitized_Request [sharesix] Use sanitized_Request [sina] Use sanitized_Request [smotri] Use sanitized_Request [sohu] Use sanitized_Request [spankwire] Use sanitized_Request [sportdeutschland] Use sanitized_Request [streamcloud] Use sanitized_Request [streamcz] Use sanitized_Request [tapely] Use sanitized_Request [tube8] Use sanitized_Request [tubitv] Use sanitized_Request [twitch] Use sanitized_Request [twitter] Use sanitized_Request [udemy] Use sanitized_Request [vbox7] Use sanitized_Request [veoh] Use sanitized_Request [vessel] Use sanitized_Request [vevo] Use sanitized_Request [viddler] Use sanitized_Request [videomega] Use sanitized_Request [viewvster] Use sanitized_Request [viki] Use sanitized_Request [vk] Use sanitized_Request [vodlocker] Use sanitized_Request [voicerepublic] Use sanitized_Request [wistia] Use sanitized_Request [xfileshare] Use sanitized_Request [xtube] Use sanitized_Request [xvideos] Use sanitized_Request [yandexmusic] Use sanitized_Request [youku] Use sanitized_Request [youporn] Use sanitized_Request [youtube] Use sanitized_Request [patreon] Use sanitized_Request [extractor/common] Remove unused import [nfb] PEP 8
9 years ago
Switch codebase to use sanitized_Request instead of compat_urllib_request.Request [downloader/dash] Use sanitized_Request [downloader/http] Use sanitized_Request [atresplayer] Use sanitized_Request [bambuser] Use sanitized_Request [bliptv] Use sanitized_Request [brightcove] Use sanitized_Request [cbs] Use sanitized_Request [ceskatelevize] Use sanitized_Request [collegerama] Use sanitized_Request [extractor/common] Use sanitized_Request [crunchyroll] Use sanitized_Request [dailymotion] Use sanitized_Request [dcn] Use sanitized_Request [dramafever] Use sanitized_Request [dumpert] Use sanitized_Request [eitb] Use sanitized_Request [escapist] Use sanitized_Request [everyonesmixtape] Use sanitized_Request [extremetube] Use sanitized_Request [facebook] Use sanitized_Request [fc2] Use sanitized_Request [flickr] Use sanitized_Request [4tube] Use sanitized_Request [gdcvault] Use sanitized_Request [extractor/generic] Use sanitized_Request [hearthisat] Use sanitized_Request [hotnewhiphop] Use sanitized_Request [hypem] Use sanitized_Request [iprima] Use sanitized_Request [ivi] Use sanitized_Request [keezmovies] Use sanitized_Request [letv] Use sanitized_Request [lynda] Use sanitized_Request [metacafe] Use sanitized_Request [minhateca] Use sanitized_Request [miomio] Use sanitized_Request [meovideo] Use sanitized_Request [mofosex] Use sanitized_Request [moniker] Use sanitized_Request [mooshare] Use sanitized_Request [movieclips] Use sanitized_Request [mtv] Use sanitized_Request [myvideo] Use sanitized_Request [neteasemusic] Use sanitized_Request [nfb] Use sanitized_Request [niconico] Use sanitized_Request [noco] Use sanitized_Request [nosvideo] Use sanitized_Request [novamov] Use sanitized_Request [nowness] Use sanitized_Request [nuvid] Use sanitized_Request [played] Use sanitized_Request [pluralsight] Use sanitized_Request [pornhub] Use sanitized_Request [pornotube] Use sanitized_Request [primesharetv] Use sanitized_Request [promptfile] Use sanitized_Request [qqmusic] Use sanitized_Request [rtve] Use sanitized_Request [safari] Use sanitized_Request [sandia] Use sanitized_Request [shared] Use sanitized_Request [sharesix] Use sanitized_Request [sina] Use sanitized_Request [smotri] Use sanitized_Request [sohu] Use sanitized_Request [spankwire] Use sanitized_Request [sportdeutschland] Use sanitized_Request [streamcloud] Use sanitized_Request [streamcz] Use sanitized_Request [tapely] Use sanitized_Request [tube8] Use sanitized_Request [tubitv] Use sanitized_Request [twitch] Use sanitized_Request [twitter] Use sanitized_Request [udemy] Use sanitized_Request [vbox7] Use sanitized_Request [veoh] Use sanitized_Request [vessel] Use sanitized_Request [vevo] Use sanitized_Request [viddler] Use sanitized_Request [videomega] Use sanitized_Request [viewvster] Use sanitized_Request [viki] Use sanitized_Request [vk] Use sanitized_Request [vodlocker] Use sanitized_Request [voicerepublic] Use sanitized_Request [wistia] Use sanitized_Request [xfileshare] Use sanitized_Request [xtube] Use sanitized_Request [xvideos] Use sanitized_Request [yandexmusic] Use sanitized_Request [youku] Use sanitized_Request [youporn] Use sanitized_Request [youtube] Use sanitized_Request [patreon] Use sanitized_Request [extractor/common] Remove unused import [nfb] PEP 8
9 years ago
  1. from __future__ import unicode_literals
  2. import re
  3. import socket
  4. from .common import InfoExtractor
  5. from ..compat import (
  6. compat_etree_fromstring,
  7. compat_http_client,
  8. compat_urllib_error,
  9. compat_urllib_parse_unquote,
  10. compat_urllib_parse_unquote_plus,
  11. )
  12. from ..utils import (
  13. clean_html,
  14. error_to_compat_str,
  15. ExtractorError,
  16. get_element_by_id,
  17. int_or_none,
  18. js_to_json,
  19. limit_length,
  20. sanitized_Request,
  21. try_get,
  22. urlencode_postdata,
  23. )
  24. class FacebookIE(InfoExtractor):
  25. _VALID_URL = r'''(?x)
  26. (?:
  27. https?://
  28. (?:[\w-]+\.)?(?:facebook\.com|facebookcorewwwi\.onion)/
  29. (?:[^#]*?\#!/)?
  30. (?:
  31. (?:
  32. video/video\.php|
  33. photo\.php|
  34. video\.php|
  35. video/embed|
  36. story\.php
  37. )\?(?:.*?)(?:v|video_id|story_fbid)=|
  38. [^/]+/videos/(?:[^/]+/)?|
  39. [^/]+/posts/|
  40. groups/[^/]+/permalink/
  41. )|
  42. facebook:
  43. )
  44. (?P<id>[0-9]+)
  45. '''
  46. _LOGIN_URL = 'https://www.facebook.com/login.php?next=http%3A%2F%2Ffacebook.com%2Fhome.php&login_attempt=1'
  47. _CHECKPOINT_URL = 'https://www.facebook.com/checkpoint/?next=http%3A%2F%2Ffacebook.com%2Fhome.php&_fb_noscript=1'
  48. _NETRC_MACHINE = 'facebook'
  49. IE_NAME = 'facebook'
  50. _CHROME_USER_AGENT = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.97 Safari/537.36'
  51. _VIDEO_PAGE_TEMPLATE = 'https://www.facebook.com/video/video.php?v=%s'
  52. _TESTS = [{
  53. 'url': 'https://www.facebook.com/video.php?v=637842556329505&fref=nf',
  54. 'md5': '6a40d33c0eccbb1af76cf0485a052659',
  55. 'info_dict': {
  56. 'id': '637842556329505',
  57. 'ext': 'mp4',
  58. 'title': 're:Did you know Kei Nishikori is the first Asian man to ever reach a Grand Slam',
  59. 'uploader': 'Tennis on Facebook',
  60. 'upload_date': '20140908',
  61. 'timestamp': 1410199200,
  62. }
  63. }, {
  64. 'note': 'Video without discernible title',
  65. 'url': 'https://www.facebook.com/video.php?v=274175099429670',
  66. 'info_dict': {
  67. 'id': '274175099429670',
  68. 'ext': 'mp4',
  69. 'title': 'Asif Nawab Butt posted a video to his Timeline.',
  70. 'uploader': 'Asif Nawab Butt',
  71. 'upload_date': '20140506',
  72. 'timestamp': 1399398998,
  73. },
  74. 'expected_warnings': [
  75. 'title'
  76. ]
  77. }, {
  78. 'note': 'Video with DASH manifest',
  79. 'url': 'https://www.facebook.com/video.php?v=957955867617029',
  80. 'md5': 'b2c28d528273b323abe5c6ab59f0f030',
  81. 'info_dict': {
  82. 'id': '957955867617029',
  83. 'ext': 'mp4',
  84. 'title': 'When you post epic content on instagram.com/433 8 million followers, this is ...',
  85. 'uploader': 'Demy de Zeeuw',
  86. 'upload_date': '20160110',
  87. 'timestamp': 1452431627,
  88. },
  89. }, {
  90. 'url': 'https://www.facebook.com/maxlayn/posts/10153807558977570',
  91. 'md5': '037b1fa7f3c2d02b7a0d7bc16031ecc6',
  92. 'info_dict': {
  93. 'id': '544765982287235',
  94. 'ext': 'mp4',
  95. 'title': '"What are you doing running in the snow?"',
  96. 'uploader': 'FailArmy',
  97. },
  98. 'skip': 'Video gone',
  99. }, {
  100. 'url': 'https://m.facebook.com/story.php?story_fbid=1035862816472149&id=116132035111903',
  101. 'md5': '1deb90b6ac27f7efcf6d747c8a27f5e3',
  102. 'info_dict': {
  103. 'id': '1035862816472149',
  104. 'ext': 'mp4',
  105. 'title': 'What the Flock Is Going On In New Zealand Credit: ViralHog',
  106. 'uploader': 'S. Saint',
  107. },
  108. 'skip': 'Video gone',
  109. }, {
  110. 'note': 'swf params escaped',
  111. 'url': 'https://www.facebook.com/barackobama/posts/10153664894881749',
  112. 'md5': '97ba073838964d12c70566e0085c2b91',
  113. 'info_dict': {
  114. 'id': '10153664894881749',
  115. 'ext': 'mp4',
  116. 'title': 'Facebook video #10153664894881749',
  117. },
  118. }, {
  119. # have 1080P, but only up to 720p in swf params
  120. 'url': 'https://www.facebook.com/cnn/videos/10155529876156509/',
  121. 'md5': '0d9813160b146b3bc8744e006027fcc6',
  122. 'info_dict': {
  123. 'id': '10155529876156509',
  124. 'ext': 'mp4',
  125. 'title': 'Holocaust survivor becomes US citizen',
  126. 'timestamp': 1477818095,
  127. 'upload_date': '20161030',
  128. 'uploader': 'CNN',
  129. },
  130. }, {
  131. 'url': 'https://www.facebook.com/video.php?v=10204634152394104',
  132. 'only_matching': True,
  133. }, {
  134. 'url': 'https://www.facebook.com/amogood/videos/1618742068337349/?fref=nf',
  135. 'only_matching': True,
  136. }, {
  137. 'url': 'https://www.facebook.com/ChristyClarkForBC/videos/vb.22819070941/10153870694020942/?type=2&theater',
  138. 'only_matching': True,
  139. }, {
  140. 'url': 'facebook:544765982287235',
  141. 'only_matching': True,
  142. }, {
  143. 'url': 'https://www.facebook.com/groups/164828000315060/permalink/764967300301124/',
  144. 'only_matching': True,
  145. }, {
  146. 'url': 'https://zh-hk.facebook.com/peoplespower/videos/1135894589806027/',
  147. 'only_matching': True,
  148. }, {
  149. 'url': 'https://www.facebookcorewwwi.onion/video.php?v=274175099429670',
  150. 'only_matching': True,
  151. }]
  152. @staticmethod
  153. def _extract_url(webpage):
  154. mobj = re.search(
  155. r'<iframe[^>]+?src=(["\'])(?P<url>https://www\.facebook\.com/video/embed.+?)\1', webpage)
  156. if mobj is not None:
  157. return mobj.group('url')
  158. # Facebook API embed
  159. # see https://developers.facebook.com/docs/plugins/embedded-video-player
  160. mobj = re.search(r'''(?x)<div[^>]+
  161. class=(?P<q1>[\'"])[^\'"]*\bfb-(?:video|post)\b[^\'"]*(?P=q1)[^>]+
  162. data-href=(?P<q2>[\'"])(?P<url>(?:https?:)?//(?:www\.)?facebook.com/.+?)(?P=q2)''', webpage)
  163. if mobj is not None:
  164. return mobj.group('url')
  165. def _login(self):
  166. (useremail, password) = self._get_login_info()
  167. if useremail is None:
  168. return
  169. login_page_req = sanitized_Request(self._LOGIN_URL)
  170. self._set_cookie('facebook.com', 'locale', 'en_US')
  171. login_page = self._download_webpage(login_page_req, None,
  172. note='Downloading login page',
  173. errnote='Unable to download login page')
  174. lsd = self._search_regex(
  175. r'<input type="hidden" name="lsd" value="([^"]*)"',
  176. login_page, 'lsd')
  177. lgnrnd = self._search_regex(r'name="lgnrnd" value="([^"]*?)"', login_page, 'lgnrnd')
  178. login_form = {
  179. 'email': useremail,
  180. 'pass': password,
  181. 'lsd': lsd,
  182. 'lgnrnd': lgnrnd,
  183. 'next': 'http://facebook.com/home.php',
  184. 'default_persistent': '0',
  185. 'legacy_return': '1',
  186. 'timezone': '-60',
  187. 'trynum': '1',
  188. }
  189. request = sanitized_Request(self._LOGIN_URL, urlencode_postdata(login_form))
  190. request.add_header('Content-Type', 'application/x-www-form-urlencoded')
  191. try:
  192. login_results = self._download_webpage(request, None,
  193. note='Logging in', errnote='unable to fetch login page')
  194. if re.search(r'<form(.*)name="login"(.*)</form>', login_results) is not None:
  195. error = self._html_search_regex(
  196. r'(?s)<div[^>]+class=(["\']).*?login_error_box.*?\1[^>]*><div[^>]*>.*?</div><div[^>]*>(?P<error>.+?)</div>',
  197. login_results, 'login error', default=None, group='error')
  198. if error:
  199. raise ExtractorError('Unable to login: %s' % error, expected=True)
  200. self._downloader.report_warning('unable to log in: bad username/password, or exceeded login rate limit (~3/min). Check credentials or wait.')
  201. return
  202. fb_dtsg = self._search_regex(
  203. r'name="fb_dtsg" value="(.+?)"', login_results, 'fb_dtsg', default=None)
  204. h = self._search_regex(
  205. r'name="h"\s+(?:\w+="[^"]+"\s+)*?value="([^"]+)"', login_results, 'h', default=None)
  206. if not fb_dtsg or not h:
  207. return
  208. check_form = {
  209. 'fb_dtsg': fb_dtsg,
  210. 'h': h,
  211. 'name_action_selected': 'dont_save',
  212. }
  213. check_req = sanitized_Request(self._CHECKPOINT_URL, urlencode_postdata(check_form))
  214. check_req.add_header('Content-Type', 'application/x-www-form-urlencoded')
  215. check_response = self._download_webpage(check_req, None,
  216. note='Confirming login')
  217. if re.search(r'id="checkpointSubmitButton"', check_response) is not None:
  218. self._downloader.report_warning('Unable to confirm login, you have to login in your browser and authorize the login.')
  219. except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
  220. self._downloader.report_warning('unable to log in: %s' % error_to_compat_str(err))
  221. return
  222. def _real_initialize(self):
  223. self._login()
  224. def _extract_from_url(self, url, video_id, fatal_if_no_video=True):
  225. req = sanitized_Request(url)
  226. req.add_header('User-Agent', self._CHROME_USER_AGENT)
  227. webpage = self._download_webpage(req, video_id)
  228. video_data = None
  229. def extract_video_data(instances):
  230. for item in instances:
  231. if item[1][0] == 'VideoConfig':
  232. video_item = item[2][0]
  233. if video_item.get('video_id') == video_id:
  234. return video_item['videoData']
  235. server_js_data = self._parse_json(self._search_regex(
  236. r'handleServerJS\(({.+})(?:\);|,")', webpage,
  237. 'server js data', default='{}'), video_id, fatal=False)
  238. if server_js_data:
  239. video_data = extract_video_data(server_js_data.get('instances', []))
  240. if not video_data:
  241. server_js_data = self._parse_json(
  242. self._search_regex(
  243. r'bigPipe\.onPageletArrive\(({.+?})\)\s*;\s*}\s*\)\s*,\s*["\']onPageletArrive\s+stream_pagelet',
  244. webpage, 'js data', default='{}'),
  245. video_id, transform_source=js_to_json, fatal=False)
  246. if server_js_data:
  247. video_data = extract_video_data(try_get(
  248. server_js_data, lambda x: x['jsmods']['instances'],
  249. list) or [])
  250. if not video_data:
  251. if not fatal_if_no_video:
  252. return webpage, False
  253. m_msg = re.search(r'class="[^"]*uiInterstitialContent[^"]*"><div>(.*?)</div>', webpage)
  254. if m_msg is not None:
  255. raise ExtractorError(
  256. 'The video is not available, Facebook said: "%s"' % m_msg.group(1),
  257. expected=True)
  258. elif '>You must log in to continue' in webpage:
  259. self.raise_login_required()
  260. else:
  261. raise ExtractorError('Cannot parse data')
  262. formats = []
  263. for f in video_data:
  264. format_id = f['stream_type']
  265. if f and isinstance(f, dict):
  266. f = [f]
  267. if not f or not isinstance(f, list):
  268. continue
  269. for quality in ('sd', 'hd'):
  270. for src_type in ('src', 'src_no_ratelimit'):
  271. src = f[0].get('%s_%s' % (quality, src_type))
  272. if src:
  273. preference = -10 if format_id == 'progressive' else 0
  274. if quality == 'hd':
  275. preference += 5
  276. formats.append({
  277. 'format_id': '%s_%s_%s' % (format_id, quality, src_type),
  278. 'url': src,
  279. 'preference': preference,
  280. })
  281. dash_manifest = f[0].get('dash_manifest')
  282. if dash_manifest:
  283. formats.extend(self._parse_mpd_formats(
  284. compat_etree_fromstring(compat_urllib_parse_unquote_plus(dash_manifest))))
  285. if not formats:
  286. raise ExtractorError('Cannot find video formats')
  287. self._sort_formats(formats)
  288. video_title = self._html_search_regex(
  289. r'<h2\s+[^>]*class="uiHeaderTitle"[^>]*>([^<]*)</h2>', webpage, 'title',
  290. default=None)
  291. if not video_title:
  292. video_title = self._html_search_regex(
  293. r'(?s)<span class="fbPhotosPhotoCaption".*?id="fbPhotoPageCaption"><span class="hasCaption">(.*?)</span>',
  294. webpage, 'alternative title', default=None)
  295. if not video_title:
  296. video_title = self._html_search_meta(
  297. 'description', webpage, 'title')
  298. if video_title:
  299. video_title = limit_length(video_title, 80)
  300. else:
  301. video_title = 'Facebook video #%s' % video_id
  302. uploader = clean_html(get_element_by_id(
  303. 'fbPhotoPageAuthorName', webpage)) or self._search_regex(
  304. r'ownerName\s*:\s*"([^"]+)"', webpage, 'uploader', fatal=False)
  305. timestamp = int_or_none(self._search_regex(
  306. r'<abbr[^>]+data-utime=["\'](\d+)', webpage,
  307. 'timestamp', default=None))
  308. info_dict = {
  309. 'id': video_id,
  310. 'title': video_title,
  311. 'formats': formats,
  312. 'uploader': uploader,
  313. 'timestamp': timestamp,
  314. }
  315. return webpage, info_dict
  316. def _real_extract(self, url):
  317. video_id = self._match_id(url)
  318. real_url = self._VIDEO_PAGE_TEMPLATE % video_id if url.startswith('facebook:') else url
  319. webpage, info_dict = self._extract_from_url(real_url, video_id, fatal_if_no_video=False)
  320. if info_dict:
  321. return info_dict
  322. if '/posts/' in url:
  323. entries = [
  324. self.url_result('facebook:%s' % vid, FacebookIE.ie_key())
  325. for vid in self._parse_json(
  326. self._search_regex(
  327. r'(["\'])video_ids\1\s*:\s*(?P<ids>\[.+?\])',
  328. webpage, 'video ids', group='ids'),
  329. video_id)]
  330. return self.playlist_result(entries, video_id)
  331. else:
  332. _, info_dict = self._extract_from_url(
  333. self._VIDEO_PAGE_TEMPLATE % video_id,
  334. video_id, fatal_if_no_video=True)
  335. return info_dict
  336. class FacebookPluginsVideoIE(InfoExtractor):
  337. _VALID_URL = r'https?://(?:[\w-]+\.)?facebook\.com/plugins/video\.php\?.*?\bhref=(?P<id>https.+)'
  338. _TESTS = [{
  339. 'url': 'https://www.facebook.com/plugins/video.php?href=https%3A%2F%2Fwww.facebook.com%2Fgov.sg%2Fvideos%2F10154383743583686%2F&show_text=0&width=560',
  340. 'md5': '5954e92cdfe51fe5782ae9bda7058a07',
  341. 'info_dict': {
  342. 'id': '10154383743583686',
  343. 'ext': 'mp4',
  344. 'title': 'What to do during the haze?',
  345. 'uploader': 'Gov.sg',
  346. 'upload_date': '20160826',
  347. 'timestamp': 1472184808,
  348. },
  349. 'add_ie': [FacebookIE.ie_key()],
  350. }, {
  351. 'url': 'https://www.facebook.com/plugins/video.php?href=https%3A%2F%2Fwww.facebook.com%2Fvideo.php%3Fv%3D10204634152394104',
  352. 'only_matching': True,
  353. }, {
  354. 'url': 'https://www.facebook.com/plugins/video.php?href=https://www.facebook.com/gov.sg/videos/10154383743583686/&show_text=0&width=560',
  355. 'only_matching': True,
  356. }]
  357. def _real_extract(self, url):
  358. return self.url_result(
  359. compat_urllib_parse_unquote(self._match_id(url)),
  360. FacebookIE.ie_key())