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.

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