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.

373 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 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. error_to_compat_str,
  14. ExtractorError,
  15. int_or_none,
  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|facebookcorewwwi\.onion)/
  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. 'upload_date': '20140908',
  59. 'timestamp': 1410199200,
  60. }
  61. }, {
  62. 'note': 'Video without discernible title',
  63. 'url': 'https://www.facebook.com/video.php?v=274175099429670',
  64. 'info_dict': {
  65. 'id': '274175099429670',
  66. 'ext': 'mp4',
  67. 'title': 'Facebook video #274175099429670',
  68. 'uploader': 'Asif Nawab Butt',
  69. 'upload_date': '20140506',
  70. 'timestamp': 1399398998,
  71. },
  72. 'expected_warnings': [
  73. 'title'
  74. ]
  75. }, {
  76. 'note': 'Video with DASH manifest',
  77. 'url': 'https://www.facebook.com/video.php?v=957955867617029',
  78. 'md5': 'b2c28d528273b323abe5c6ab59f0f030',
  79. 'info_dict': {
  80. 'id': '957955867617029',
  81. 'ext': 'mp4',
  82. 'title': 'When you post epic content on instagram.com/433 8 million followers, this is ...',
  83. 'uploader': 'Demy de Zeeuw',
  84. 'upload_date': '20160110',
  85. 'timestamp': 1452431627,
  86. },
  87. }, {
  88. 'url': 'https://www.facebook.com/maxlayn/posts/10153807558977570',
  89. 'md5': '037b1fa7f3c2d02b7a0d7bc16031ecc6',
  90. 'info_dict': {
  91. 'id': '544765982287235',
  92. 'ext': 'mp4',
  93. 'title': '"What are you doing running in the snow?"',
  94. 'uploader': 'FailArmy',
  95. },
  96. 'skip': 'Video gone',
  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. 'skip': 'Video gone',
  107. }, {
  108. 'note': 'swf params escaped',
  109. 'url': 'https://www.facebook.com/barackobama/posts/10153664894881749',
  110. 'md5': '97ba073838964d12c70566e0085c2b91',
  111. 'info_dict': {
  112. 'id': '10153664894881749',
  113. 'ext': 'mp4',
  114. 'title': 'Facebook video #10153664894881749',
  115. },
  116. }, {
  117. # have 1080P, but only up to 720p in swf params
  118. 'url': 'https://www.facebook.com/cnn/videos/10155529876156509/',
  119. 'md5': '0d9813160b146b3bc8744e006027fcc6',
  120. 'info_dict': {
  121. 'id': '10155529876156509',
  122. 'ext': 'mp4',
  123. 'title': 'Holocaust survivor becomes US citizen',
  124. 'timestamp': 1477818095,
  125. 'upload_date': '20161030',
  126. 'uploader': 'CNN',
  127. },
  128. }, {
  129. 'url': 'https://www.facebook.com/video.php?v=10204634152394104',
  130. 'only_matching': True,
  131. }, {
  132. 'url': 'https://www.facebook.com/amogood/videos/1618742068337349/?fref=nf',
  133. 'only_matching': True,
  134. }, {
  135. 'url': 'https://www.facebook.com/ChristyClarkForBC/videos/vb.22819070941/10153870694020942/?type=2&theater',
  136. 'only_matching': True,
  137. }, {
  138. 'url': 'facebook:544765982287235',
  139. 'only_matching': True,
  140. }, {
  141. 'url': 'https://www.facebook.com/groups/164828000315060/permalink/764967300301124/',
  142. 'only_matching': True,
  143. }, {
  144. 'url': 'https://zh-hk.facebook.com/peoplespower/videos/1135894589806027/',
  145. 'only_matching': True,
  146. }, {
  147. 'url': 'https://www.facebookcorewwwi.onion/video.php?v=274175099429670',
  148. 'only_matching': True,
  149. }]
  150. @staticmethod
  151. def _extract_url(webpage):
  152. mobj = re.search(
  153. r'<iframe[^>]+?src=(["\'])(?P<url>https://www\.facebook\.com/video/embed.+?)\1', webpage)
  154. if mobj is not None:
  155. return mobj.group('url')
  156. # Facebook API embed
  157. # see https://developers.facebook.com/docs/plugins/embedded-video-player
  158. mobj = re.search(r'''(?x)<div[^>]+
  159. class=(?P<q1>[\'"])[^\'"]*\bfb-(?:video|post)\b[^\'"]*(?P=q1)[^>]+
  160. data-href=(?P<q2>[\'"])(?P<url>(?:https?:)?//(?:www\.)?facebook.com/.+?)(?P=q2)''', webpage)
  161. if mobj is not None:
  162. return mobj.group('url')
  163. def _login(self):
  164. (useremail, password) = self._get_login_info()
  165. if useremail is None:
  166. return
  167. login_page_req = sanitized_Request(self._LOGIN_URL)
  168. self._set_cookie('facebook.com', 'locale', 'en_US')
  169. login_page = self._download_webpage(login_page_req, None,
  170. note='Downloading login page',
  171. errnote='Unable to download login page')
  172. lsd = self._search_regex(
  173. r'<input type="hidden" name="lsd" value="([^"]*)"',
  174. login_page, 'lsd')
  175. lgnrnd = self._search_regex(r'name="lgnrnd" value="([^"]*?)"', login_page, 'lgnrnd')
  176. login_form = {
  177. 'email': useremail,
  178. 'pass': password,
  179. 'lsd': lsd,
  180. 'lgnrnd': lgnrnd,
  181. 'next': 'http://facebook.com/home.php',
  182. 'default_persistent': '0',
  183. 'legacy_return': '1',
  184. 'timezone': '-60',
  185. 'trynum': '1',
  186. }
  187. request = sanitized_Request(self._LOGIN_URL, urlencode_postdata(login_form))
  188. request.add_header('Content-Type', 'application/x-www-form-urlencoded')
  189. try:
  190. login_results = self._download_webpage(request, None,
  191. note='Logging in', errnote='unable to fetch login page')
  192. if re.search(r'<form(.*)name="login"(.*)</form>', login_results) is not None:
  193. error = self._html_search_regex(
  194. r'(?s)<div[^>]+class=(["\']).*?login_error_box.*?\1[^>]*><div[^>]*>.*?</div><div[^>]*>(?P<error>.+?)</div>',
  195. login_results, 'login error', default=None, group='error')
  196. if error:
  197. raise ExtractorError('Unable to login: %s' % error, expected=True)
  198. self._downloader.report_warning('unable to log in: bad username/password, or exceeded login rate limit (~3/min). Check credentials or wait.')
  199. return
  200. fb_dtsg = self._search_regex(
  201. r'name="fb_dtsg" value="(.+?)"', login_results, 'fb_dtsg', default=None)
  202. h = self._search_regex(
  203. r'name="h"\s+(?:\w+="[^"]+"\s+)*?value="([^"]+)"', login_results, 'h', default=None)
  204. if not fb_dtsg or not h:
  205. return
  206. check_form = {
  207. 'fb_dtsg': fb_dtsg,
  208. 'h': h,
  209. 'name_action_selected': 'dont_save',
  210. }
  211. check_req = sanitized_Request(self._CHECKPOINT_URL, urlencode_postdata(check_form))
  212. check_req.add_header('Content-Type', 'application/x-www-form-urlencoded')
  213. check_response = self._download_webpage(check_req, None,
  214. note='Confirming login')
  215. if re.search(r'id="checkpointSubmitButton"', check_response) is not None:
  216. self._downloader.report_warning('Unable to confirm login, you have to login in your browser and authorize the login.')
  217. except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
  218. self._downloader.report_warning('unable to log in: %s' % error_to_compat_str(err))
  219. return
  220. def _real_initialize(self):
  221. self._login()
  222. def _extract_from_url(self, url, video_id, fatal_if_no_video=True):
  223. req = sanitized_Request(url)
  224. req.add_header('User-Agent', self._CHROME_USER_AGENT)
  225. webpage = self._download_webpage(req, video_id)
  226. video_data = None
  227. server_js_data = self._parse_json(self._search_regex(
  228. r'handleServerJS\(({.+})(?:\);|,")', webpage, 'server js data', default='{}'), video_id)
  229. for item in server_js_data.get('instances', []):
  230. if item[1][0] == 'VideoConfig':
  231. video_item = item[2][0]
  232. if video_item.get('video_id') == video_id:
  233. video_data = video_item['videoData']
  234. break
  235. if not video_data:
  236. if not fatal_if_no_video:
  237. return webpage, False
  238. m_msg = re.search(r'class="[^"]*uiInterstitialContent[^"]*"><div>(.*?)</div>', webpage)
  239. if m_msg is not None:
  240. raise ExtractorError(
  241. 'The video is not available, Facebook said: "%s"' % m_msg.group(1),
  242. expected=True)
  243. elif '>You must log in to continue' in webpage:
  244. self.raise_login_required()
  245. else:
  246. raise ExtractorError('Cannot parse data')
  247. formats = []
  248. for f in video_data:
  249. format_id = f['stream_type']
  250. if f and isinstance(f, dict):
  251. f = [f]
  252. if not f or not isinstance(f, list):
  253. continue
  254. for quality in ('sd', 'hd'):
  255. for src_type in ('src', 'src_no_ratelimit'):
  256. src = f[0].get('%s_%s' % (quality, src_type))
  257. if src:
  258. preference = -10 if format_id == 'progressive' else 0
  259. if quality == 'hd':
  260. preference += 5
  261. formats.append({
  262. 'format_id': '%s_%s_%s' % (format_id, quality, src_type),
  263. 'url': src,
  264. 'preference': preference,
  265. })
  266. dash_manifest = f[0].get('dash_manifest')
  267. if dash_manifest:
  268. formats.extend(self._parse_mpd_formats(
  269. compat_etree_fromstring(compat_urllib_parse_unquote_plus(dash_manifest))))
  270. if not formats:
  271. raise ExtractorError('Cannot find video formats')
  272. self._sort_formats(formats)
  273. video_title = self._html_search_regex(
  274. r'<h2\s+[^>]*class="uiHeaderTitle"[^>]*>([^<]*)</h2>', webpage, 'title',
  275. default=None)
  276. if not video_title:
  277. video_title = self._html_search_regex(
  278. r'(?s)<span class="fbPhotosPhotoCaption".*?id="fbPhotoPageCaption"><span class="hasCaption">(.*?)</span>',
  279. webpage, 'alternative title', default=None)
  280. video_title = limit_length(video_title, 80)
  281. if not video_title:
  282. video_title = 'Facebook video #%s' % video_id
  283. uploader = clean_html(get_element_by_id('fbPhotoPageAuthorName', webpage))
  284. timestamp = int_or_none(self._search_regex(
  285. r'<abbr[^>]+data-utime=["\'](\d+)', webpage,
  286. 'timestamp', default=None))
  287. info_dict = {
  288. 'id': video_id,
  289. 'title': video_title,
  290. 'formats': formats,
  291. 'uploader': uploader,
  292. 'timestamp': timestamp,
  293. }
  294. return webpage, info_dict
  295. def _real_extract(self, url):
  296. video_id = self._match_id(url)
  297. real_url = self._VIDEO_PAGE_TEMPLATE % video_id if url.startswith('facebook:') else url
  298. webpage, info_dict = self._extract_from_url(real_url, video_id, fatal_if_no_video=False)
  299. if info_dict:
  300. return info_dict
  301. if '/posts/' in url:
  302. entries = [
  303. self.url_result('facebook:%s' % vid, FacebookIE.ie_key())
  304. for vid in self._parse_json(
  305. self._search_regex(
  306. r'(["\'])video_ids\1\s*:\s*(?P<ids>\[.+?\])',
  307. webpage, 'video ids', group='ids'),
  308. video_id)]
  309. return self.playlist_result(entries, video_id)
  310. else:
  311. _, info_dict = self._extract_from_url(
  312. self._VIDEO_PAGE_TEMPLATE % video_id,
  313. video_id, fatal_if_no_video=True)
  314. return info_dict
  315. class FacebookPluginsVideoIE(InfoExtractor):
  316. _VALID_URL = r'https?://(?:[\w-]+\.)?facebook\.com/plugins/video\.php\?.*?\bhref=(?P<id>https.+)'
  317. _TESTS = [{
  318. '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',
  319. 'md5': '5954e92cdfe51fe5782ae9bda7058a07',
  320. 'info_dict': {
  321. 'id': '10154383743583686',
  322. 'ext': 'mp4',
  323. 'title': 'What to do during the haze?',
  324. 'uploader': 'Gov.sg',
  325. 'upload_date': '20160826',
  326. 'timestamp': 1472184808,
  327. },
  328. 'add_ie': [FacebookIE.ie_key()],
  329. }, {
  330. 'url': 'https://www.facebook.com/plugins/video.php?href=https%3A%2F%2Fwww.facebook.com%2Fvideo.php%3Fv%3D10204634152394104',
  331. 'only_matching': True,
  332. }, {
  333. 'url': 'https://www.facebook.com/plugins/video.php?href=https://www.facebook.com/gov.sg/videos/10154383743583686/&show_text=0&width=560',
  334. 'only_matching': True,
  335. }]
  336. def _real_extract(self, url):
  337. return self.url_result(
  338. compat_urllib_parse_unquote(self._match_id(url)),
  339. FacebookIE.ie_key())