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.

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