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