facebook.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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. float_or_none,
  18. get_element_by_id,
  19. int_or_none,
  20. js_to_json,
  21. limit_length,
  22. parse_count,
  23. qualities,
  24. sanitized_Request,
  25. try_get,
  26. urlencode_postdata,
  27. urljoin,
  28. )
  29. class FacebookIE(InfoExtractor):
  30. _VALID_URL = r'''(?x)
  31. (?:
  32. https?://
  33. (?:[\w-]+\.)?(?:facebook\.com|facebookcorewwwi\.onion)/
  34. (?:[^#]*?\#!/)?
  35. (?:
  36. (?:
  37. video/video\.php|
  38. photo\.php|
  39. video\.php|
  40. video/embed|
  41. story\.php|
  42. watch/?
  43. )\?(?:.*?)(?:v|video_id|story_fbid)=|
  44. [^/]+/videos/(?:[^/]+/)?|
  45. [^/]+/posts/|
  46. groups/[^/]+/permalink/
  47. )|
  48. facebook:
  49. )
  50. (?P<id>[0-9]+)
  51. '''
  52. _LOGIN_URL = 'https://www.facebook.com/login.php?next=http%3A%2F%2Ffacebook.com%2Fhome.php&login_attempt=1'
  53. _CHECKPOINT_URL = 'https://www.facebook.com/checkpoint/?next=http%3A%2F%2Ffacebook.com%2Fhome.php&_fb_noscript=1'
  54. _NETRC_MACHINE = 'facebook'
  55. IE_NAME = 'facebook'
  56. _VIDEO_PAGE_TEMPLATE = 'https://www.facebook.com/video/video.php?v=%s'
  57. _VIDEO_PAGE_TAHOE_TEMPLATE = 'https://www.facebook.com/video/tahoe/async/%s/?chain=true&isvideo=true&payloadtype=primary'
  58. _TESTS = [{
  59. 'url': 'https://www.facebook.com/video.php?v=637842556329505&fref=nf',
  60. 'md5': '6a40d33c0eccbb1af76cf0485a052659',
  61. 'info_dict': {
  62. 'id': '637842556329505',
  63. 'ext': 'mp4',
  64. 'title': 're:Did you know Kei Nishikori is the first Asian man to ever reach a Grand Slam',
  65. 'uploader': 'Tennis on Facebook',
  66. 'upload_date': '20140908',
  67. 'timestamp': 1410199200,
  68. },
  69. 'skip': 'Requires logging in',
  70. }, {
  71. # data.video
  72. 'url': 'https://www.facebook.com/video.php?v=274175099429670',
  73. 'info_dict': {
  74. 'id': '274175099429670',
  75. 'ext': 'mp4',
  76. 'title': 're:^Asif Nawab Butt posted a video',
  77. 'uploader': 'Asif Nawab Butt',
  78. 'upload_date': '20140506',
  79. 'timestamp': 1399398998,
  80. 'thumbnail': r're:^https?://.*',
  81. },
  82. 'expected_warnings': [
  83. 'title'
  84. ]
  85. }, {
  86. 'note': 'Video with DASH manifest',
  87. 'url': 'https://www.facebook.com/video.php?v=957955867617029',
  88. 'md5': 'b2c28d528273b323abe5c6ab59f0f030',
  89. 'info_dict': {
  90. 'id': '957955867617029',
  91. 'ext': 'mp4',
  92. 'title': 'When you post epic content on instagram.com/433 8 million followers, this is ...',
  93. 'uploader': 'Demy de Zeeuw',
  94. 'upload_date': '20160110',
  95. 'timestamp': 1452431627,
  96. },
  97. 'skip': 'Requires logging in',
  98. }, {
  99. 'url': 'https://www.facebook.com/maxlayn/posts/10153807558977570',
  100. 'md5': '037b1fa7f3c2d02b7a0d7bc16031ecc6',
  101. 'info_dict': {
  102. 'id': '544765982287235',
  103. 'ext': 'mp4',
  104. 'title': '"What are you doing running in the snow?"',
  105. 'uploader': 'FailArmy',
  106. },
  107. 'skip': 'Video gone',
  108. }, {
  109. 'url': 'https://m.facebook.com/story.php?story_fbid=1035862816472149&id=116132035111903',
  110. 'md5': '1deb90b6ac27f7efcf6d747c8a27f5e3',
  111. 'info_dict': {
  112. 'id': '1035862816472149',
  113. 'ext': 'mp4',
  114. 'title': 'What the Flock Is Going On In New Zealand Credit: ViralHog',
  115. 'uploader': 'S. Saint',
  116. },
  117. 'skip': 'Video gone',
  118. }, {
  119. 'note': 'swf params escaped',
  120. 'url': 'https://www.facebook.com/barackobama/posts/10153664894881749',
  121. 'md5': '97ba073838964d12c70566e0085c2b91',
  122. 'info_dict': {
  123. 'id': '10153664894881749',
  124. 'ext': 'mp4',
  125. 'title': 'Average time to confirm recent Supreme Court nominees: 67 days Longest it\'s t...',
  126. 'thumbnail': r're:^https?://.*',
  127. 'timestamp': 1456259628,
  128. 'upload_date': '20160223',
  129. 'uploader': 'Barack Obama',
  130. },
  131. }, {
  132. # have 1080P, but only up to 720p in swf params
  133. # data.video.story.attachments[].media
  134. 'url': 'https://www.facebook.com/cnn/videos/10155529876156509/',
  135. 'md5': '9571fae53d4165bbbadb17a94651dcdc',
  136. 'info_dict': {
  137. 'id': '10155529876156509',
  138. 'ext': 'mp4',
  139. 'title': 'She survived the holocaust — and years later, she’s getting her citizenship s...',
  140. 'timestamp': 1477818095,
  141. 'upload_date': '20161030',
  142. 'uploader': 'CNN',
  143. 'thumbnail': r're:^https?://.*',
  144. 'view_count': int,
  145. },
  146. }, {
  147. # bigPipe.onPageletArrive ... onPageletArrive pagelet_group_mall
  148. # data.node.comet_sections.content.story.attachments[].style_type_renderer.attachment.media
  149. 'url': 'https://www.facebook.com/yaroslav.korpan/videos/1417995061575415/',
  150. 'info_dict': {
  151. 'id': '1417995061575415',
  152. 'ext': 'mp4',
  153. 'title': 'md5:1db063d6a8c13faa8da727817339c857',
  154. 'timestamp': 1486648217,
  155. 'upload_date': '20170209',
  156. 'uploader': 'Yaroslav Korpan',
  157. },
  158. 'params': {
  159. 'skip_download': True,
  160. },
  161. }, {
  162. 'url': 'https://www.facebook.com/LaGuiaDelVaron/posts/1072691702860471',
  163. 'info_dict': {
  164. 'id': '1072691702860471',
  165. 'ext': 'mp4',
  166. 'title': 'md5:ae2d22a93fbb12dad20dc393a869739d',
  167. 'timestamp': 1477305000,
  168. 'upload_date': '20161024',
  169. 'uploader': 'La Guía Del Varón',
  170. 'thumbnail': r're:^https?://.*',
  171. },
  172. 'params': {
  173. 'skip_download': True,
  174. },
  175. }, {
  176. # data.node.comet_sections.content.story.attachments[].style_type_renderer.attachment.media
  177. 'url': 'https://www.facebook.com/groups/1024490957622648/permalink/1396382447100162/',
  178. 'info_dict': {
  179. 'id': '1396382447100162',
  180. 'ext': 'mp4',
  181. 'title': 'md5:19a428bbde91364e3de815383b54a235',
  182. 'timestamp': 1486035494,
  183. 'upload_date': '20170202',
  184. 'uploader': 'Elisabeth Ahtn',
  185. },
  186. 'params': {
  187. 'skip_download': True,
  188. },
  189. }, {
  190. 'url': 'https://www.facebook.com/video.php?v=10204634152394104',
  191. 'only_matching': True,
  192. }, {
  193. 'url': 'https://www.facebook.com/amogood/videos/1618742068337349/?fref=nf',
  194. 'only_matching': True,
  195. }, {
  196. # data.mediaset.currMedia.edges
  197. 'url': 'https://www.facebook.com/ChristyClarkForBC/videos/vb.22819070941/10153870694020942/?type=2&theater',
  198. 'only_matching': True,
  199. }, {
  200. # data.video.story.attachments[].media
  201. 'url': 'facebook:544765982287235',
  202. 'only_matching': True,
  203. }, {
  204. # data.node.comet_sections.content.story.attachments[].style_type_renderer.attachment.media
  205. 'url': 'https://www.facebook.com/groups/164828000315060/permalink/764967300301124/',
  206. 'only_matching': True,
  207. }, {
  208. # data.video.creation_story.attachments[].media
  209. 'url': 'https://zh-hk.facebook.com/peoplespower/videos/1135894589806027/',
  210. 'only_matching': True,
  211. }, {
  212. # data.video
  213. 'url': 'https://www.facebookcorewwwi.onion/video.php?v=274175099429670',
  214. 'only_matching': True,
  215. }, {
  216. # no title
  217. 'url': 'https://www.facebook.com/onlycleverentertainment/videos/1947995502095005/',
  218. 'only_matching': True,
  219. }, {
  220. # data.video
  221. 'url': 'https://www.facebook.com/WatchESLOne/videos/359649331226507/',
  222. 'info_dict': {
  223. 'id': '359649331226507',
  224. 'ext': 'mp4',
  225. 'title': '#ESLOne VoD - Birmingham Finals Day#1 Fnatic vs. @Evil Geniuses',
  226. 'uploader': 'ESL One Dota 2',
  227. },
  228. 'params': {
  229. 'skip_download': True,
  230. },
  231. }, {
  232. # data.node.comet_sections.content.story.attachments[].style_type_renderer.attachment.all_subattachments.nodes[].media
  233. 'url': 'https://www.facebook.com/100033620354545/videos/106560053808006/',
  234. 'info_dict': {
  235. 'id': '106560053808006',
  236. },
  237. 'playlist_count': 2,
  238. }, {
  239. # data.video.story.attachments[].media
  240. 'url': 'https://www.facebook.com/watch/?v=647537299265662',
  241. 'only_matching': True,
  242. }, {
  243. # data.node.comet_sections.content.story.attachments[].style_type_renderer.attachment.all_subattachments.nodes[].media
  244. 'url': 'https://www.facebook.com/PankajShahLondon/posts/10157667649866271',
  245. 'info_dict': {
  246. 'id': '10157667649866271',
  247. },
  248. 'playlist_count': 3,
  249. }, {
  250. # data.nodes[].comet_sections.content.story.attachments[].style_type_renderer.attachment.media
  251. 'url': 'https://m.facebook.com/Alliance.Police.Department/posts/4048563708499330',
  252. 'info_dict': {
  253. 'id': '117576630041613',
  254. 'ext': 'mp4',
  255. # TODO: title can be extracted from video page
  256. 'title': 'Facebook video #117576630041613',
  257. 'uploader_id': '189393014416438',
  258. 'upload_date': '20201123',
  259. 'timestamp': 1606162592,
  260. },
  261. 'skip': 'Requires logging in',
  262. }, {
  263. # node.comet_sections.content.story.attached_story.attachments.style_type_renderer.attachment.media
  264. 'url': 'https://www.facebook.com/groups/ateistiskselskab/permalink/10154930137678856/',
  265. 'info_dict': {
  266. 'id': '211567722618337',
  267. 'ext': 'mp4',
  268. 'title': 'Facebook video #211567722618337',
  269. 'uploader_id': '127875227654254',
  270. 'upload_date': '20161122',
  271. 'timestamp': 1479793574,
  272. },
  273. }]
  274. _SUPPORTED_PAGLETS_REGEX = r'(?:pagelet_group_mall|permalink_video_pagelet|hyperfeed_story_id_[0-9a-f]+)'
  275. @staticmethod
  276. def _extract_urls(webpage):
  277. urls = []
  278. for mobj in re.finditer(
  279. r'<iframe[^>]+?src=(["\'])(?P<url>https?://www\.facebook\.com/(?:video/embed|plugins/video\.php).+?)\1',
  280. webpage):
  281. urls.append(mobj.group('url'))
  282. # Facebook API embed
  283. # see https://developers.facebook.com/docs/plugins/embedded-video-player
  284. for mobj in re.finditer(r'''(?x)<div[^>]+
  285. class=(?P<q1>[\'"])[^\'"]*\bfb-(?:video|post)\b[^\'"]*(?P=q1)[^>]+
  286. data-href=(?P<q2>[\'"])(?P<url>(?:https?:)?//(?:www\.)?facebook.com/.+?)(?P=q2)''', webpage):
  287. urls.append(mobj.group('url'))
  288. return urls
  289. def _login(self):
  290. useremail, password = self._get_login_info()
  291. if useremail is None:
  292. return
  293. login_page_req = sanitized_Request(self._LOGIN_URL)
  294. self._set_cookie('facebook.com', 'locale', 'en_US')
  295. login_page = self._download_webpage(login_page_req, None,
  296. note='Downloading login page',
  297. errnote='Unable to download login page')
  298. lsd = self._search_regex(
  299. r'<input type="hidden" name="lsd" value="([^"]*)"',
  300. login_page, 'lsd')
  301. lgnrnd = self._search_regex(r'name="lgnrnd" value="([^"]*?)"', login_page, 'lgnrnd')
  302. login_form = {
  303. 'email': useremail,
  304. 'pass': password,
  305. 'lsd': lsd,
  306. 'lgnrnd': lgnrnd,
  307. 'next': 'http://facebook.com/home.php',
  308. 'default_persistent': '0',
  309. 'legacy_return': '1',
  310. 'timezone': '-60',
  311. 'trynum': '1',
  312. }
  313. request = sanitized_Request(self._LOGIN_URL, urlencode_postdata(login_form))
  314. request.add_header('Content-Type', 'application/x-www-form-urlencoded')
  315. try:
  316. login_results = self._download_webpage(request, None,
  317. note='Logging in', errnote='unable to fetch login page')
  318. if re.search(r'<form(.*)name="login"(.*)</form>', login_results) is not None:
  319. error = self._html_search_regex(
  320. r'(?s)<div[^>]+class=(["\']).*?login_error_box.*?\1[^>]*><div[^>]*>.*?</div><div[^>]*>(?P<error>.+?)</div>',
  321. login_results, 'login error', default=None, group='error')
  322. if error:
  323. raise ExtractorError('Unable to login: %s' % error, expected=True)
  324. self._downloader.report_warning('unable to log in: bad username/password, or exceeded login rate limit (~3/min). Check credentials or wait.')
  325. return
  326. fb_dtsg = self._search_regex(
  327. r'name="fb_dtsg" value="(.+?)"', login_results, 'fb_dtsg', default=None)
  328. h = self._search_regex(
  329. r'name="h"\s+(?:\w+="[^"]+"\s+)*?value="([^"]+)"', login_results, 'h', default=None)
  330. if not fb_dtsg or not h:
  331. return
  332. check_form = {
  333. 'fb_dtsg': fb_dtsg,
  334. 'h': h,
  335. 'name_action_selected': 'dont_save',
  336. }
  337. check_req = sanitized_Request(self._CHECKPOINT_URL, urlencode_postdata(check_form))
  338. check_req.add_header('Content-Type', 'application/x-www-form-urlencoded')
  339. check_response = self._download_webpage(check_req, None,
  340. note='Confirming login')
  341. if re.search(r'id="checkpointSubmitButton"', check_response) is not None:
  342. self._downloader.report_warning('Unable to confirm login, you have to login in your browser and authorize the login.')
  343. except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
  344. self._downloader.report_warning('unable to log in: %s' % error_to_compat_str(err))
  345. return
  346. def _real_initialize(self):
  347. self._login()
  348. def _extract_from_url(self, url, video_id):
  349. webpage = self._download_webpage(
  350. url.replace('://m.facebook.com/', '://www.facebook.com/'), video_id)
  351. video_data = None
  352. def extract_video_data(instances):
  353. video_data = []
  354. for item in instances:
  355. if try_get(item, lambda x: x[1][0]) == 'VideoConfig':
  356. video_item = item[2][0]
  357. if video_item.get('video_id'):
  358. video_data.append(video_item['videoData'])
  359. return video_data
  360. server_js_data = self._parse_json(self._search_regex(
  361. [r'handleServerJS\(({.+})(?:\);|,")', r'\bs\.handle\(({.+?})\);'],
  362. webpage, 'server js data', default='{}'), video_id, fatal=False)
  363. if server_js_data:
  364. video_data = extract_video_data(server_js_data.get('instances', []))
  365. def extract_from_jsmods_instances(js_data):
  366. if js_data:
  367. return extract_video_data(try_get(
  368. js_data, lambda x: x['jsmods']['instances'], list) or [])
  369. def extract_dash_manifest(video, formats):
  370. dash_manifest = video.get('dash_manifest')
  371. if dash_manifest:
  372. formats.extend(self._parse_mpd_formats(
  373. compat_etree_fromstring(compat_urllib_parse_unquote_plus(dash_manifest))))
  374. def process_formats(formats):
  375. # Downloads with browser's User-Agent are rate limited. Working around
  376. # with non-browser User-Agent.
  377. for f in formats:
  378. f.setdefault('http_headers', {})['User-Agent'] = 'facebookexternalhit/1.1'
  379. self._sort_formats(formats)
  380. if not video_data:
  381. server_js_data = self._parse_json(self._search_regex([
  382. r'bigPipe\.onPageletArrive\(({.+?})\)\s*;\s*}\s*\)\s*,\s*["\']onPageletArrive\s+' + self._SUPPORTED_PAGLETS_REGEX,
  383. r'bigPipe\.onPageletArrive\(({.*?id\s*:\s*"%s".*?})\);' % self._SUPPORTED_PAGLETS_REGEX
  384. ], webpage, 'js data', default='{}'), video_id, js_to_json, False)
  385. video_data = extract_from_jsmods_instances(server_js_data)
  386. if not video_data:
  387. graphql_data = self._parse_json(self._search_regex(
  388. r'handleWithCustomApplyEach\([^,]+,\s*({.*?"(?:dash_manifest|playable_url(?:_quality_hd)?)"\s*:\s*"[^"]+".*?})\);',
  389. webpage, 'graphql data', default='{}'), video_id, fatal=False) or {}
  390. for require in (graphql_data.get('require') or []):
  391. if require[0] == 'RelayPrefetchedStreamCache':
  392. entries = []
  393. def parse_graphql_video(video):
  394. formats = []
  395. q = qualities(['sd', 'hd'])
  396. for (suffix, format_id) in [('', 'sd'), ('_quality_hd', 'hd')]:
  397. playable_url = video.get('playable_url' + suffix)
  398. if not playable_url:
  399. continue
  400. formats.append({
  401. 'format_id': format_id,
  402. 'quality': q(format_id),
  403. 'url': playable_url,
  404. })
  405. extract_dash_manifest(video, formats)
  406. process_formats(formats)
  407. v_id = video.get('videoId') or video.get('id') or video_id
  408. info = {
  409. 'id': v_id,
  410. 'formats': formats,
  411. 'thumbnail': try_get(video, lambda x: x['thumbnailImage']['uri']),
  412. 'uploader_id': try_get(video, lambda x: x['owner']['id']),
  413. 'timestamp': int_or_none(video.get('publish_time')),
  414. 'duration': float_or_none(video.get('playable_duration_in_ms'), 1000),
  415. }
  416. description = try_get(video, lambda x: x['savable_description']['text'])
  417. title = video.get('name')
  418. if title:
  419. info.update({
  420. 'title': title,
  421. 'description': description,
  422. })
  423. else:
  424. info['title'] = description or 'Facebook video #%s' % v_id
  425. entries.append(info)
  426. def parse_attachment(attachment, key='media'):
  427. media = attachment.get(key) or {}
  428. if media.get('__typename') == 'Video':
  429. return parse_graphql_video(media)
  430. data = try_get(require, lambda x: x[3][1]['__bbox']['result']['data'], dict) or {}
  431. nodes = data.get('nodes') or []
  432. node = data.get('node') or {}
  433. if not nodes and node:
  434. nodes.append(node)
  435. for node in nodes:
  436. story = try_get(node, lambda x: x['comet_sections']['content']['story'], dict) or {}
  437. attachments = try_get(story, [
  438. lambda x: x['attached_story']['attachments'],
  439. lambda x: x['attachments']
  440. ], list) or []
  441. for attachment in attachments:
  442. attachment = try_get(attachment, lambda x: x['style_type_renderer']['attachment'], dict)
  443. ns = try_get(attachment, lambda x: x['all_subattachments']['nodes'], list) or []
  444. for n in ns:
  445. parse_attachment(n)
  446. parse_attachment(attachment)
  447. edges = try_get(data, lambda x: x['mediaset']['currMedia']['edges'], list) or []
  448. for edge in edges:
  449. parse_attachment(edge, key='node')
  450. video = data.get('video') or {}
  451. if video:
  452. attachments = try_get(video, [
  453. lambda x: x['story']['attachments'],
  454. lambda x: x['creation_story']['attachments']
  455. ], list) or []
  456. for attachment in attachments:
  457. parse_attachment(attachment)
  458. if not entries:
  459. parse_graphql_video(video)
  460. return self.playlist_result(entries, video_id)
  461. if not video_data:
  462. m_msg = re.search(r'class="[^"]*uiInterstitialContent[^"]*"><div>(.*?)</div>', webpage)
  463. if m_msg is not None:
  464. raise ExtractorError(
  465. 'The video is not available, Facebook said: "%s"' % m_msg.group(1),
  466. expected=True)
  467. elif '>You must log in to continue' in webpage:
  468. self.raise_login_required()
  469. # Video info not in first request, do a secondary request using
  470. # tahoe player specific URL
  471. tahoe_data = self._download_webpage(
  472. self._VIDEO_PAGE_TAHOE_TEMPLATE % video_id, video_id,
  473. data=urlencode_postdata({
  474. '__a': 1,
  475. '__pc': self._search_regex(
  476. r'pkg_cohort["\']\s*:\s*["\'](.+?)["\']', webpage,
  477. 'pkg cohort', default='PHASED:DEFAULT'),
  478. '__rev': self._search_regex(
  479. r'client_revision["\']\s*:\s*(\d+),', webpage,
  480. 'client revision', default='3944515'),
  481. 'fb_dtsg': self._search_regex(
  482. r'"DTSGInitialData"\s*,\s*\[\]\s*,\s*{\s*"token"\s*:\s*"([^"]+)"',
  483. webpage, 'dtsg token', default=''),
  484. }),
  485. headers={
  486. 'Content-Type': 'application/x-www-form-urlencoded',
  487. })
  488. tahoe_js_data = self._parse_json(
  489. self._search_regex(
  490. r'for\s+\(\s*;\s*;\s*\)\s*;(.+)', tahoe_data,
  491. 'tahoe js data', default='{}'),
  492. video_id, fatal=False)
  493. video_data = extract_from_jsmods_instances(tahoe_js_data)
  494. if not video_data:
  495. raise ExtractorError('Cannot parse data')
  496. if len(video_data) > 1:
  497. entries = []
  498. for v in video_data:
  499. video_url = v[0].get('video_url')
  500. if not video_url:
  501. continue
  502. entries.append(self.url_result(urljoin(
  503. url, video_url), self.ie_key(), v[0].get('video_id')))
  504. return self.playlist_result(entries, video_id)
  505. video_data = video_data[0]
  506. formats = []
  507. subtitles = {}
  508. for f in video_data:
  509. format_id = f['stream_type']
  510. if f and isinstance(f, dict):
  511. f = [f]
  512. if not f or not isinstance(f, list):
  513. continue
  514. for quality in ('sd', 'hd'):
  515. for src_type in ('src', 'src_no_ratelimit'):
  516. src = f[0].get('%s_%s' % (quality, src_type))
  517. if src:
  518. preference = -10 if format_id == 'progressive' else 0
  519. if quality == 'hd':
  520. preference += 5
  521. formats.append({
  522. 'format_id': '%s_%s_%s' % (format_id, quality, src_type),
  523. 'url': src,
  524. 'preference': preference,
  525. })
  526. extract_dash_manifest(f[0], formats)
  527. subtitles_src = f[0].get('subtitles_src')
  528. if subtitles_src:
  529. subtitles.setdefault('en', []).append({'url': subtitles_src})
  530. if not formats:
  531. raise ExtractorError('Cannot find video formats')
  532. process_formats(formats)
  533. video_title = self._html_search_regex(
  534. r'<h2\s+[^>]*class="uiHeaderTitle"[^>]*>([^<]*)</h2>', webpage,
  535. 'title', default=None)
  536. if not video_title:
  537. video_title = self._html_search_regex(
  538. r'(?s)<span class="fbPhotosPhotoCaption".*?id="fbPhotoPageCaption"><span class="hasCaption">(.*?)</span>',
  539. webpage, 'alternative title', default=None)
  540. if not video_title:
  541. video_title = self._html_search_meta(
  542. 'description', webpage, 'title', default=None)
  543. if video_title:
  544. video_title = limit_length(video_title, 80)
  545. else:
  546. video_title = 'Facebook video #%s' % video_id
  547. uploader = clean_html(get_element_by_id(
  548. 'fbPhotoPageAuthorName', webpage)) or self._search_regex(
  549. r'ownerName\s*:\s*"([^"]+)"', webpage, 'uploader',
  550. default=None) or self._og_search_title(webpage, fatal=False)
  551. timestamp = int_or_none(self._search_regex(
  552. r'<abbr[^>]+data-utime=["\'](\d+)', webpage,
  553. 'timestamp', default=None))
  554. thumbnail = self._html_search_meta(['og:image', 'twitter:image'], webpage)
  555. view_count = parse_count(self._search_regex(
  556. r'\bviewCount\s*:\s*["\']([\d,.]+)', webpage, 'view count',
  557. default=None))
  558. info_dict = {
  559. 'id': video_id,
  560. 'title': video_title,
  561. 'formats': formats,
  562. 'uploader': uploader,
  563. 'timestamp': timestamp,
  564. 'thumbnail': thumbnail,
  565. 'view_count': view_count,
  566. 'subtitles': subtitles,
  567. }
  568. return info_dict
  569. def _real_extract(self, url):
  570. video_id = self._match_id(url)
  571. real_url = self._VIDEO_PAGE_TEMPLATE % video_id if url.startswith('facebook:') else url
  572. return self._extract_from_url(real_url, video_id)
  573. class FacebookPluginsVideoIE(InfoExtractor):
  574. _VALID_URL = r'https?://(?:[\w-]+\.)?facebook\.com/plugins/video\.php\?.*?\bhref=(?P<id>https.+)'
  575. _TESTS = [{
  576. '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',
  577. 'md5': '5954e92cdfe51fe5782ae9bda7058a07',
  578. 'info_dict': {
  579. 'id': '10154383743583686',
  580. 'ext': 'mp4',
  581. 'title': 'What to do during the haze?',
  582. 'uploader': 'Gov.sg',
  583. 'upload_date': '20160826',
  584. 'timestamp': 1472184808,
  585. },
  586. 'add_ie': [FacebookIE.ie_key()],
  587. }, {
  588. 'url': 'https://www.facebook.com/plugins/video.php?href=https%3A%2F%2Fwww.facebook.com%2Fvideo.php%3Fv%3D10204634152394104',
  589. 'only_matching': True,
  590. }, {
  591. 'url': 'https://www.facebook.com/plugins/video.php?href=https://www.facebook.com/gov.sg/videos/10154383743583686/&show_text=0&width=560',
  592. 'only_matching': True,
  593. }]
  594. def _real_extract(self, url):
  595. return self.url_result(
  596. compat_urllib_parse_unquote(self._match_id(url)),
  597. FacebookIE.ie_key())