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.

337 lines
14 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 json
  3. import re
  4. import socket
  5. from .common import InfoExtractor
  6. from ..compat import (
  7. compat_etree_fromstring,
  8. compat_http_client,
  9. compat_urllib_error,
  10. compat_urllib_parse_unquote,
  11. compat_urllib_parse_unquote_plus,
  12. )
  13. from ..utils import (
  14. error_to_compat_str,
  15. ExtractorError,
  16. limit_length,
  17. sanitized_Request,
  18. urlencode_postdata,
  19. get_element_by_id,
  20. clean_html,
  21. )
  22. class FacebookIE(InfoExtractor):
  23. _VALID_URL = r'''(?x)
  24. (?:
  25. https?://
  26. (?:\w+\.)?facebook\.com/
  27. (?:[^#]*?\#!/)?
  28. (?:
  29. (?:
  30. video/video\.php|
  31. photo\.php|
  32. video\.php|
  33. video/embed|
  34. story\.php
  35. )\?(?:.*?)(?:v|video_id|story_fbid)=|
  36. [^/]+/videos/(?:[^/]+/)?|
  37. [^/]+/posts/|
  38. groups/[^/]+/permalink/
  39. )|
  40. facebook:
  41. )
  42. (?P<id>[0-9]+)
  43. '''
  44. _LOGIN_URL = 'https://www.facebook.com/login.php?next=http%3A%2F%2Ffacebook.com%2Fhome.php&login_attempt=1'
  45. _CHECKPOINT_URL = 'https://www.facebook.com/checkpoint/?next=http%3A%2F%2Ffacebook.com%2Fhome.php&_fb_noscript=1'
  46. _NETRC_MACHINE = 'facebook'
  47. IE_NAME = 'facebook'
  48. _CHROME_USER_AGENT = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.97 Safari/537.36'
  49. _VIDEO_PAGE_TEMPLATE = 'https://www.facebook.com/video/video.php?v=%s'
  50. _TESTS = [{
  51. 'url': 'https://www.facebook.com/video.php?v=637842556329505&fref=nf',
  52. 'md5': '6a40d33c0eccbb1af76cf0485a052659',
  53. 'info_dict': {
  54. 'id': '637842556329505',
  55. 'ext': 'mp4',
  56. 'title': 're:Did you know Kei Nishikori is the first Asian man to ever reach a Grand Slam',
  57. 'uploader': 'Tennis on Facebook',
  58. }
  59. }, {
  60. 'note': 'Video without discernible title',
  61. 'url': 'https://www.facebook.com/video.php?v=274175099429670',
  62. 'info_dict': {
  63. 'id': '274175099429670',
  64. 'ext': 'mp4',
  65. 'title': 'Facebook video #274175099429670',
  66. 'uploader': 'Asif Nawab Butt',
  67. },
  68. 'expected_warnings': [
  69. 'title'
  70. ]
  71. }, {
  72. 'note': 'Video with DASH manifest',
  73. 'url': 'https://www.facebook.com/video.php?v=957955867617029',
  74. 'md5': '54706e4db4f5ad58fbad82dde1f1213f',
  75. 'info_dict': {
  76. 'id': '957955867617029',
  77. 'ext': 'mp4',
  78. 'title': 'When you post epic content on instagram.com/433 8 million followers, this is ...',
  79. 'uploader': 'Demy de Zeeuw',
  80. },
  81. }, {
  82. 'url': 'https://www.facebook.com/maxlayn/posts/10153807558977570',
  83. 'md5': '037b1fa7f3c2d02b7a0d7bc16031ecc6',
  84. 'info_dict': {
  85. 'id': '544765982287235',
  86. 'ext': 'mp4',
  87. 'title': '"What are you doing running in the snow?"',
  88. 'uploader': 'FailArmy',
  89. }
  90. }, {
  91. 'url': 'https://m.facebook.com/story.php?story_fbid=1035862816472149&id=116132035111903',
  92. 'md5': '1deb90b6ac27f7efcf6d747c8a27f5e3',
  93. 'info_dict': {
  94. 'id': '1035862816472149',
  95. 'ext': 'mp4',
  96. 'title': 'What the Flock Is Going On In New Zealand Credit: ViralHog',
  97. 'uploader': 'S. Saint',
  98. },
  99. }, {
  100. 'note': 'swf params escaped',
  101. 'url': 'https://www.facebook.com/barackobama/posts/10153664894881749',
  102. 'md5': '97ba073838964d12c70566e0085c2b91',
  103. 'info_dict': {
  104. 'id': '10153664894881749',
  105. 'ext': 'mp4',
  106. 'title': 'Facebook video #10153664894881749',
  107. },
  108. }, {
  109. 'url': 'https://www.facebook.com/video.php?v=10204634152394104',
  110. 'only_matching': True,
  111. }, {
  112. 'url': 'https://www.facebook.com/amogood/videos/1618742068337349/?fref=nf',
  113. 'only_matching': True,
  114. }, {
  115. 'url': 'https://www.facebook.com/ChristyClarkForBC/videos/vb.22819070941/10153870694020942/?type=2&theater',
  116. 'only_matching': True,
  117. }, {
  118. 'url': 'facebook:544765982287235',
  119. 'only_matching': True,
  120. }, {
  121. 'url': 'https://www.facebook.com/groups/164828000315060/permalink/764967300301124/',
  122. 'only_matching': True,
  123. }]
  124. @staticmethod
  125. def _extract_url(webpage):
  126. mobj = re.search(
  127. r'<iframe[^>]+?src=(["\'])(?P<url>https://www\.facebook\.com/video/embed.+?)\1', webpage)
  128. if mobj is not None:
  129. return mobj.group('url')
  130. # Facebook API embed
  131. # see https://developers.facebook.com/docs/plugins/embedded-video-player
  132. mobj = re.search(r'''(?x)<div[^>]+
  133. class=(?P<q1>[\'"])[^\'"]*\bfb-(?:video|post)\b[^\'"]*(?P=q1)[^>]+
  134. data-href=(?P<q2>[\'"])(?P<url>(?:https?:)?//(?:www\.)?facebook.com/.+?)(?P=q2)''', webpage)
  135. if mobj is not None:
  136. return mobj.group('url')
  137. def _login(self):
  138. (useremail, password) = self._get_login_info()
  139. if useremail is None:
  140. return
  141. login_page_req = sanitized_Request(self._LOGIN_URL)
  142. self._set_cookie('facebook.com', 'locale', 'en_US')
  143. login_page = self._download_webpage(login_page_req, None,
  144. note='Downloading login page',
  145. errnote='Unable to download login page')
  146. lsd = self._search_regex(
  147. r'<input type="hidden" name="lsd" value="([^"]*)"',
  148. login_page, 'lsd')
  149. lgnrnd = self._search_regex(r'name="lgnrnd" value="([^"]*?)"', login_page, 'lgnrnd')
  150. login_form = {
  151. 'email': useremail,
  152. 'pass': password,
  153. 'lsd': lsd,
  154. 'lgnrnd': lgnrnd,
  155. 'next': 'http://facebook.com/home.php',
  156. 'default_persistent': '0',
  157. 'legacy_return': '1',
  158. 'timezone': '-60',
  159. 'trynum': '1',
  160. }
  161. request = sanitized_Request(self._LOGIN_URL, urlencode_postdata(login_form))
  162. request.add_header('Content-Type', 'application/x-www-form-urlencoded')
  163. try:
  164. login_results = self._download_webpage(request, None,
  165. note='Logging in', errnote='unable to fetch login page')
  166. if re.search(r'<form(.*)name="login"(.*)</form>', login_results) is not None:
  167. error = self._html_search_regex(
  168. r'(?s)<div[^>]+class=(["\']).*?login_error_box.*?\1[^>]*><div[^>]*>.*?</div><div[^>]*>(?P<error>.+?)</div>',
  169. login_results, 'login error', default=None, group='error')
  170. if error:
  171. raise ExtractorError('Unable to login: %s' % error, expected=True)
  172. self._downloader.report_warning('unable to log in: bad username/password, or exceeded login rate limit (~3/min). Check credentials or wait.')
  173. return
  174. fb_dtsg = self._search_regex(
  175. r'name="fb_dtsg" value="(.+?)"', login_results, 'fb_dtsg', default=None)
  176. h = self._search_regex(
  177. r'name="h"\s+(?:\w+="[^"]+"\s+)*?value="([^"]+)"', login_results, 'h', default=None)
  178. if not fb_dtsg or not h:
  179. return
  180. check_form = {
  181. 'fb_dtsg': fb_dtsg,
  182. 'h': h,
  183. 'name_action_selected': 'dont_save',
  184. }
  185. check_req = sanitized_Request(self._CHECKPOINT_URL, urlencode_postdata(check_form))
  186. check_req.add_header('Content-Type', 'application/x-www-form-urlencoded')
  187. check_response = self._download_webpage(check_req, None,
  188. note='Confirming login')
  189. if re.search(r'id="checkpointSubmitButton"', check_response) is not None:
  190. self._downloader.report_warning('Unable to confirm login, you have to login in your browser and authorize the login.')
  191. except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
  192. self._downloader.report_warning('unable to log in: %s' % error_to_compat_str(err))
  193. return
  194. def _real_initialize(self):
  195. self._login()
  196. def _extract_from_url(self, url, video_id, fatal_if_no_video=True):
  197. req = sanitized_Request(url)
  198. req.add_header('User-Agent', self._CHROME_USER_AGENT)
  199. webpage = self._download_webpage(req, video_id)
  200. video_data = None
  201. BEFORE = '{swf.addParam(param[0], param[1]);});'
  202. AFTER = '.forEach(function(variable) {swf.addVariable(variable[0], variable[1]);});'
  203. PATTERN = re.escape(BEFORE) + '(?:\n|\\\\n)(.*?)' + re.escape(AFTER)
  204. for m in re.findall(PATTERN, webpage):
  205. swf_params = m.replace('\\\\', '\\').replace('\\"', '"')
  206. data = dict(json.loads(swf_params))
  207. params_raw = compat_urllib_parse_unquote(data['params'])
  208. video_data_candidate = json.loads(params_raw)['video_data']
  209. for _, f in video_data_candidate.items():
  210. if not f:
  211. continue
  212. if isinstance(f, dict):
  213. f = [f]
  214. if isinstance(f, list):
  215. continue
  216. if f[0].get('video_id') == video_id:
  217. video_data = video_data_candidate
  218. break
  219. def video_data_list2dict(video_data):
  220. ret = {}
  221. for item in video_data:
  222. format_id = item['stream_type']
  223. ret.setdefault(format_id, []).append(item)
  224. return ret
  225. if not video_data:
  226. server_js_data = self._parse_json(self._search_regex(
  227. r'handleServerJS\(({.+})\);', webpage, 'server js data', default='{}'), video_id)
  228. for item in server_js_data.get('instances', []):
  229. if item[1][0] == 'VideoConfig':
  230. video_data = video_data_list2dict(item[2][0]['videoData'])
  231. break
  232. if not video_data:
  233. if not fatal_if_no_video:
  234. return webpage, False
  235. m_msg = re.search(r'class="[^"]*uiInterstitialContent[^"]*"><div>(.*?)</div>', webpage)
  236. if m_msg is not None:
  237. raise ExtractorError(
  238. 'The video is not available, Facebook said: "%s"' % m_msg.group(1),
  239. expected=True)
  240. else:
  241. raise ExtractorError('Cannot parse data')
  242. formats = []
  243. for format_id, f in video_data.items():
  244. if f and isinstance(f, dict):
  245. f = [f]
  246. if not f or not isinstance(f, list):
  247. continue
  248. for quality in ('sd', 'hd'):
  249. for src_type in ('src', 'src_no_ratelimit'):
  250. src = f[0].get('%s_%s' % (quality, src_type))
  251. if src:
  252. preference = -10 if format_id == 'progressive' else 0
  253. if quality == 'hd':
  254. preference += 5
  255. formats.append({
  256. 'format_id': '%s_%s_%s' % (format_id, quality, src_type),
  257. 'url': src,
  258. 'preference': preference,
  259. })
  260. dash_manifest = f[0].get('dash_manifest')
  261. if dash_manifest:
  262. formats.extend(self._parse_mpd_formats(
  263. compat_etree_fromstring(compat_urllib_parse_unquote_plus(dash_manifest))))
  264. if not formats:
  265. raise ExtractorError('Cannot find video formats')
  266. self._sort_formats(formats)
  267. video_title = self._html_search_regex(
  268. r'<h2\s+[^>]*class="uiHeaderTitle"[^>]*>([^<]*)</h2>', webpage, 'title',
  269. default=None)
  270. if not video_title:
  271. video_title = self._html_search_regex(
  272. r'(?s)<span class="fbPhotosPhotoCaption".*?id="fbPhotoPageCaption"><span class="hasCaption">(.*?)</span>',
  273. webpage, 'alternative title', default=None)
  274. video_title = limit_length(video_title, 80)
  275. if not video_title:
  276. video_title = 'Facebook video #%s' % video_id
  277. uploader = clean_html(get_element_by_id('fbPhotoPageAuthorName', webpage))
  278. info_dict = {
  279. 'id': video_id,
  280. 'title': video_title,
  281. 'formats': formats,
  282. 'uploader': uploader,
  283. }
  284. return webpage, info_dict
  285. def _real_extract(self, url):
  286. video_id = self._match_id(url)
  287. real_url = self._VIDEO_PAGE_TEMPLATE % video_id if url.startswith('facebook:') else url
  288. webpage, info_dict = self._extract_from_url(real_url, video_id, fatal_if_no_video=False)
  289. if info_dict:
  290. return info_dict
  291. if '/posts/' in url:
  292. entries = [
  293. self.url_result('facebook:%s' % vid, FacebookIE.ie_key())
  294. for vid in self._parse_json(
  295. self._search_regex(
  296. r'(["\'])video_ids\1\s*:\s*(?P<ids>\[.+?\])',
  297. webpage, 'video ids', group='ids'),
  298. video_id)]
  299. return self.playlist_result(entries, video_id)
  300. else:
  301. _, info_dict = self._extract_from_url(
  302. self._VIDEO_PAGE_TEMPLATE % video_id,
  303. video_id, fatal_if_no_video=True)
  304. return info_dict