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.

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