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.

442 lines
18 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. # no title
  194. 'url': 'https://www.facebook.com/onlycleverentertainment/videos/1947995502095005/',
  195. 'only_matching': True,
  196. }]
  197. @staticmethod
  198. def _extract_url(webpage):
  199. mobj = re.search(
  200. r'<iframe[^>]+?src=(["\'])(?P<url>https://www\.facebook\.com/video/embed.+?)\1', webpage)
  201. if mobj is not None:
  202. return mobj.group('url')
  203. # Facebook API embed
  204. # see https://developers.facebook.com/docs/plugins/embedded-video-player
  205. mobj = re.search(r'''(?x)<div[^>]+
  206. class=(?P<q1>[\'"])[^\'"]*\bfb-(?:video|post)\b[^\'"]*(?P=q1)[^>]+
  207. data-href=(?P<q2>[\'"])(?P<url>(?:https?:)?//(?:www\.)?facebook.com/.+?)(?P=q2)''', webpage)
  208. if mobj is not None:
  209. return mobj.group('url')
  210. def _login(self):
  211. (useremail, password) = self._get_login_info()
  212. if useremail is None:
  213. return
  214. login_page_req = sanitized_Request(self._LOGIN_URL)
  215. self._set_cookie('facebook.com', 'locale', 'en_US')
  216. login_page = self._download_webpage(login_page_req, None,
  217. note='Downloading login page',
  218. errnote='Unable to download login page')
  219. lsd = self._search_regex(
  220. r'<input type="hidden" name="lsd" value="([^"]*)"',
  221. login_page, 'lsd')
  222. lgnrnd = self._search_regex(r'name="lgnrnd" value="([^"]*?)"', login_page, 'lgnrnd')
  223. login_form = {
  224. 'email': useremail,
  225. 'pass': password,
  226. 'lsd': lsd,
  227. 'lgnrnd': lgnrnd,
  228. 'next': 'http://facebook.com/home.php',
  229. 'default_persistent': '0',
  230. 'legacy_return': '1',
  231. 'timezone': '-60',
  232. 'trynum': '1',
  233. }
  234. request = sanitized_Request(self._LOGIN_URL, urlencode_postdata(login_form))
  235. request.add_header('Content-Type', 'application/x-www-form-urlencoded')
  236. try:
  237. login_results = self._download_webpage(request, None,
  238. note='Logging in', errnote='unable to fetch login page')
  239. if re.search(r'<form(.*)name="login"(.*)</form>', login_results) is not None:
  240. error = self._html_search_regex(
  241. r'(?s)<div[^>]+class=(["\']).*?login_error_box.*?\1[^>]*><div[^>]*>.*?</div><div[^>]*>(?P<error>.+?)</div>',
  242. login_results, 'login error', default=None, group='error')
  243. if error:
  244. raise ExtractorError('Unable to login: %s' % error, expected=True)
  245. self._downloader.report_warning('unable to log in: bad username/password, or exceeded login rate limit (~3/min). Check credentials or wait.')
  246. return
  247. fb_dtsg = self._search_regex(
  248. r'name="fb_dtsg" value="(.+?)"', login_results, 'fb_dtsg', default=None)
  249. h = self._search_regex(
  250. r'name="h"\s+(?:\w+="[^"]+"\s+)*?value="([^"]+)"', login_results, 'h', default=None)
  251. if not fb_dtsg or not h:
  252. return
  253. check_form = {
  254. 'fb_dtsg': fb_dtsg,
  255. 'h': h,
  256. 'name_action_selected': 'dont_save',
  257. }
  258. check_req = sanitized_Request(self._CHECKPOINT_URL, urlencode_postdata(check_form))
  259. check_req.add_header('Content-Type', 'application/x-www-form-urlencoded')
  260. check_response = self._download_webpage(check_req, None,
  261. note='Confirming login')
  262. if re.search(r'id="checkpointSubmitButton"', check_response) is not None:
  263. self._downloader.report_warning('Unable to confirm login, you have to login in your browser and authorize the login.')
  264. except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
  265. self._downloader.report_warning('unable to log in: %s' % error_to_compat_str(err))
  266. return
  267. def _real_initialize(self):
  268. self._login()
  269. def _extract_from_url(self, url, video_id, fatal_if_no_video=True):
  270. req = sanitized_Request(url)
  271. req.add_header('User-Agent', self._CHROME_USER_AGENT)
  272. webpage = self._download_webpage(req, video_id)
  273. video_data = None
  274. def extract_video_data(instances):
  275. for item in instances:
  276. if item[1][0] == 'VideoConfig':
  277. video_item = item[2][0]
  278. if video_item.get('video_id'):
  279. return video_item['videoData']
  280. server_js_data = self._parse_json(self._search_regex(
  281. r'handleServerJS\(({.+})(?:\);|,")', webpage,
  282. 'server js data', default='{}'), video_id, fatal=False)
  283. if server_js_data:
  284. video_data = extract_video_data(server_js_data.get('instances', []))
  285. if not video_data:
  286. server_js_data = self._parse_json(
  287. self._search_regex(
  288. r'bigPipe\.onPageletArrive\(({.+?})\)\s*;\s*}\s*\)\s*,\s*["\']onPageletArrive\s+(?:stream_pagelet|pagelet_group_mall|permalink_video_pagelet)',
  289. webpage, 'js data', default='{}'),
  290. video_id, transform_source=js_to_json, fatal=False)
  291. if server_js_data:
  292. video_data = extract_video_data(try_get(
  293. server_js_data, lambda x: x['jsmods']['instances'],
  294. list) or [])
  295. if not video_data:
  296. if not fatal_if_no_video:
  297. return webpage, False
  298. m_msg = re.search(r'class="[^"]*uiInterstitialContent[^"]*"><div>(.*?)</div>', webpage)
  299. if m_msg is not None:
  300. raise ExtractorError(
  301. 'The video is not available, Facebook said: "%s"' % m_msg.group(1),
  302. expected=True)
  303. elif '>You must log in to continue' in webpage:
  304. self.raise_login_required()
  305. else:
  306. raise ExtractorError('Cannot parse data')
  307. formats = []
  308. for f in video_data:
  309. format_id = f['stream_type']
  310. if f and isinstance(f, dict):
  311. f = [f]
  312. if not f or not isinstance(f, list):
  313. continue
  314. for quality in ('sd', 'hd'):
  315. for src_type in ('src', 'src_no_ratelimit'):
  316. src = f[0].get('%s_%s' % (quality, src_type))
  317. if src:
  318. preference = -10 if format_id == 'progressive' else 0
  319. if quality == 'hd':
  320. preference += 5
  321. formats.append({
  322. 'format_id': '%s_%s_%s' % (format_id, quality, src_type),
  323. 'url': src,
  324. 'preference': preference,
  325. })
  326. dash_manifest = f[0].get('dash_manifest')
  327. if dash_manifest:
  328. formats.extend(self._parse_mpd_formats(
  329. compat_etree_fromstring(compat_urllib_parse_unquote_plus(dash_manifest))))
  330. if not formats:
  331. raise ExtractorError('Cannot find video formats')
  332. self._sort_formats(formats)
  333. video_title = self._html_search_regex(
  334. r'<h2\s+[^>]*class="uiHeaderTitle"[^>]*>([^<]*)</h2>', webpage,
  335. 'title', default=None)
  336. if not video_title:
  337. video_title = self._html_search_regex(
  338. r'(?s)<span class="fbPhotosPhotoCaption".*?id="fbPhotoPageCaption"><span class="hasCaption">(.*?)</span>',
  339. webpage, 'alternative title', default=None)
  340. if not video_title:
  341. video_title = self._html_search_meta(
  342. 'description', webpage, 'title', default=None)
  343. if video_title:
  344. video_title = limit_length(video_title, 80)
  345. else:
  346. video_title = 'Facebook video #%s' % video_id
  347. uploader = clean_html(get_element_by_id(
  348. 'fbPhotoPageAuthorName', webpage)) or self._search_regex(
  349. r'ownerName\s*:\s*"([^"]+)"', webpage, 'uploader', fatal=False)
  350. timestamp = int_or_none(self._search_regex(
  351. r'<abbr[^>]+data-utime=["\'](\d+)', webpage,
  352. 'timestamp', default=None))
  353. info_dict = {
  354. 'id': video_id,
  355. 'title': video_title,
  356. 'formats': formats,
  357. 'uploader': uploader,
  358. 'timestamp': timestamp,
  359. }
  360. return webpage, info_dict
  361. def _real_extract(self, url):
  362. video_id = self._match_id(url)
  363. real_url = self._VIDEO_PAGE_TEMPLATE % video_id if url.startswith('facebook:') else url
  364. webpage, info_dict = self._extract_from_url(real_url, video_id, fatal_if_no_video=False)
  365. if info_dict:
  366. return info_dict
  367. if '/posts/' in url:
  368. entries = [
  369. self.url_result('facebook:%s' % vid, FacebookIE.ie_key())
  370. for vid in self._parse_json(
  371. self._search_regex(
  372. r'(["\'])video_ids\1\s*:\s*(?P<ids>\[.+?\])',
  373. webpage, 'video ids', group='ids'),
  374. video_id)]
  375. return self.playlist_result(entries, video_id)
  376. else:
  377. _, info_dict = self._extract_from_url(
  378. self._VIDEO_PAGE_TEMPLATE % video_id,
  379. video_id, fatal_if_no_video=True)
  380. return info_dict
  381. class FacebookPluginsVideoIE(InfoExtractor):
  382. _VALID_URL = r'https?://(?:[\w-]+\.)?facebook\.com/plugins/video\.php\?.*?\bhref=(?P<id>https.+)'
  383. _TESTS = [{
  384. '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',
  385. 'md5': '5954e92cdfe51fe5782ae9bda7058a07',
  386. 'info_dict': {
  387. 'id': '10154383743583686',
  388. 'ext': 'mp4',
  389. 'title': 'What to do during the haze?',
  390. 'uploader': 'Gov.sg',
  391. 'upload_date': '20160826',
  392. 'timestamp': 1472184808,
  393. },
  394. 'add_ie': [FacebookIE.ie_key()],
  395. }, {
  396. 'url': 'https://www.facebook.com/plugins/video.php?href=https%3A%2F%2Fwww.facebook.com%2Fvideo.php%3Fv%3D10204634152394104',
  397. 'only_matching': True,
  398. }, {
  399. 'url': 'https://www.facebook.com/plugins/video.php?href=https://www.facebook.com/gov.sg/videos/10154383743583686/&show_text=0&width=560',
  400. 'only_matching': True,
  401. }]
  402. def _real_extract(self, url):
  403. return self.url_result(
  404. compat_urllib_parse_unquote(self._match_id(url)),
  405. FacebookIE.ie_key())