logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

loom.py (18482B)


  1. import json
  2. import textwrap
  3. import urllib.parse
  4. import uuid
  5. from .common import InfoExtractor
  6. from ..utils import (
  7. ExtractorError,
  8. determine_ext,
  9. filter_dict,
  10. get_first,
  11. int_or_none,
  12. parse_iso8601,
  13. update_url,
  14. url_or_none,
  15. variadic,
  16. )
  17. from ..utils.traversal import traverse_obj
  18. class LoomIE(InfoExtractor):
  19. IE_NAME = 'loom'
  20. _VALID_URL = r'https?://(?:www\.)?loom\.com/(?:share|embed)/(?P<id>[\da-f]{32})'
  21. _EMBED_REGEX = [rf'<iframe[^>]+\bsrc=["\'](?P<url>{_VALID_URL})']
  22. _TESTS = [{
  23. # m3u8 raw-url, mp4 transcoded-url, cdn url == raw-url, json subs only
  24. 'url': 'https://www.loom.com/share/43d05f362f734614a2e81b4694a3a523',
  25. 'md5': 'bfc2d7e9c2e0eb4813212230794b6f42',
  26. 'info_dict': {
  27. 'id': '43d05f362f734614a2e81b4694a3a523',
  28. 'ext': 'mp4',
  29. 'title': 'A Ruler for Windows - 28 March 2022',
  30. 'uploader': 'wILLIAM PIP',
  31. 'upload_date': '20220328',
  32. 'timestamp': 1648454238,
  33. 'duration': 27,
  34. },
  35. }, {
  36. # webm raw-url, mp4 transcoded-url, cdn url == transcoded-url, no subs
  37. 'url': 'https://www.loom.com/share/c43a642f815f4378b6f80a889bb73d8d',
  38. 'md5': '70f529317be8cf880fcc2c649a531900',
  39. 'info_dict': {
  40. 'id': 'c43a642f815f4378b6f80a889bb73d8d',
  41. 'ext': 'webm',
  42. 'title': 'Lilah Nielsen Intro Video',
  43. 'uploader': 'Lilah Nielsen',
  44. 'upload_date': '20200826',
  45. 'timestamp': 1598480716,
  46. 'duration': 20,
  47. },
  48. }, {
  49. # m3u8 raw-url, mp4 transcoded-url, cdn url == raw-url, vtt sub and json subs
  50. 'url': 'https://www.loom.com/share/9458bcbf79784162aa62ffb8dd66201b',
  51. 'md5': '51737ec002969dd28344db4d60b9cbbb',
  52. 'info_dict': {
  53. 'id': '9458bcbf79784162aa62ffb8dd66201b',
  54. 'ext': 'mp4',
  55. 'title': 'Sharing screen with gpt-4',
  56. 'description': 'Sharing screen with GPT 4 vision model and asking questions to guide through blender.',
  57. 'uploader': 'Suneel Matham',
  58. 'chapters': 'count:3',
  59. 'upload_date': '20231109',
  60. 'timestamp': 1699518978,
  61. 'duration': 93,
  62. },
  63. }, {
  64. # mpd raw-url, mp4 transcoded-url, cdn url == raw-url, no subs
  65. 'url': 'https://www.loom.com/share/24351eb8b317420289b158e4b7e96ff2',
  66. 'info_dict': {
  67. 'id': '24351eb8b317420289b158e4b7e96ff2',
  68. 'ext': 'webm',
  69. 'title': 'OMFG clown',
  70. 'description': 'md5:285c5ee9d62aa087b7e3271b08796815',
  71. 'uploader': 'MrPumkin B',
  72. 'upload_date': '20210924',
  73. 'timestamp': 1632519618,
  74. 'duration': 210,
  75. },
  76. 'params': {'skip_download': 'dash'},
  77. }, {
  78. # password-protected
  79. 'url': 'https://www.loom.com/share/50e26e8aeb7940189dff5630f95ce1f4',
  80. 'md5': '5cc7655e7d55d281d203f8ffd14771f7',
  81. 'info_dict': {
  82. 'id': '50e26e8aeb7940189dff5630f95ce1f4',
  83. 'ext': 'mp4',
  84. 'title': 'iOS Mobile Upload',
  85. 'uploader': 'Simon Curran',
  86. 'upload_date': '20200520',
  87. 'timestamp': 1590000123,
  88. 'duration': 35,
  89. },
  90. 'params': {'videopassword': 'seniorinfants2'},
  91. }, {
  92. # embed, transcoded-url endpoint sends empty JSON response, split video and audio HLS formats
  93. 'url': 'https://www.loom.com/embed/ddcf1c1ad21f451ea7468b1e33917e4e',
  94. 'md5': 'b321d261656848c184a94e3b93eae28d',
  95. 'info_dict': {
  96. 'id': 'ddcf1c1ad21f451ea7468b1e33917e4e',
  97. 'ext': 'mp4',
  98. 'title': 'CF Reset User\'s Password',
  99. 'uploader': 'Aimee Heintz',
  100. 'upload_date': '20220707',
  101. 'timestamp': 1657216459,
  102. 'duration': 181,
  103. },
  104. 'params': {'format': 'bestvideo'}, # Test video-only fixup
  105. 'expected_warnings': ['Failed to parse JSON'],
  106. }]
  107. _WEBPAGE_TESTS = [{
  108. 'url': 'https://www.loom.com/community/e1229802a8694a09909e8ba0fbb6d073-pg',
  109. 'md5': 'ec838cd01b576cf0386f32e1ae424609',
  110. 'info_dict': {
  111. 'id': 'e1229802a8694a09909e8ba0fbb6d073',
  112. 'ext': 'mp4',
  113. 'title': 'Rexie Jane Cimafranca - Founder\'s Presentation',
  114. 'uploader': 'Rexie Cimafranca',
  115. 'upload_date': '20230213',
  116. 'duration': 247,
  117. 'timestamp': 1676274030,
  118. },
  119. }]
  120. _GRAPHQL_VARIABLES = {
  121. 'GetVideoSource': {
  122. 'acceptableMimes': ['DASH', 'M3U8', 'MP4'],
  123. },
  124. }
  125. _GRAPHQL_QUERIES = {
  126. 'GetVideoSSR': textwrap.dedent('''\
  127. query GetVideoSSR($videoId: ID!, $password: String) {
  128. getVideo(id: $videoId, password: $password) {
  129. __typename
  130. ... on PrivateVideo {
  131. id
  132. status
  133. message
  134. __typename
  135. }
  136. ... on VideoPasswordMissingOrIncorrect {
  137. id
  138. message
  139. __typename
  140. }
  141. ... on RegularUserVideo {
  142. id
  143. __typename
  144. createdAt
  145. description
  146. download_enabled
  147. folder_id
  148. is_protected
  149. needs_password
  150. owner {
  151. display_name
  152. __typename
  153. }
  154. privacy
  155. s3_id
  156. name
  157. video_properties {
  158. avgBitRate
  159. client
  160. camera_enabled
  161. client_version
  162. duration
  163. durationMs
  164. format
  165. height
  166. microphone_enabled
  167. os
  168. os_version
  169. recordingClient
  170. recording_type
  171. recording_version
  172. screen_type
  173. tab_audio
  174. trim_duration
  175. width
  176. __typename
  177. }
  178. playable_duration
  179. source_duration
  180. visibility
  181. }
  182. }
  183. }\n'''),
  184. 'GetVideoSource': textwrap.dedent('''\
  185. query GetVideoSource($videoId: ID!, $password: String, $acceptableMimes: [CloudfrontVideoAcceptableMime]) {
  186. getVideo(id: $videoId, password: $password) {
  187. ... on RegularUserVideo {
  188. id
  189. nullableRawCdnUrl(acceptableMimes: $acceptableMimes, password: $password) {
  190. url
  191. __typename
  192. }
  193. __typename
  194. }
  195. __typename
  196. }
  197. }\n'''),
  198. 'FetchVideoTranscript': textwrap.dedent('''\
  199. query FetchVideoTranscript($videoId: ID!, $password: String) {
  200. fetchVideoTranscript(videoId: $videoId, password: $password) {
  201. ... on VideoTranscriptDetails {
  202. id
  203. video_id
  204. source_url
  205. captions_source_url
  206. __typename
  207. }
  208. ... on GenericError {
  209. message
  210. __typename
  211. }
  212. __typename
  213. }
  214. }\n'''),
  215. 'FetchChapters': textwrap.dedent('''\
  216. query FetchChapters($videoId: ID!, $password: String) {
  217. fetchVideoChapters(videoId: $videoId, password: $password) {
  218. ... on VideoChapters {
  219. video_id
  220. content
  221. __typename
  222. }
  223. ... on EmptyChaptersPayload {
  224. content
  225. __typename
  226. }
  227. ... on InvalidRequestWarning {
  228. message
  229. __typename
  230. }
  231. ... on Error {
  232. message
  233. __typename
  234. }
  235. __typename
  236. }
  237. }\n'''),
  238. }
  239. _APOLLO_GRAPHQL_VERSION = '0a1856c'
  240. def _call_graphql_api(self, operations, video_id, note=None, errnote=None):
  241. password = self.get_param('videopassword')
  242. return self._download_json(
  243. 'https://www.loom.com/graphql', video_id, note or 'Downloading GraphQL JSON',
  244. errnote or 'Failed to download GraphQL JSON', headers={
  245. 'Accept': 'application/json',
  246. 'Content-Type': 'application/json',
  247. 'x-loom-request-source': f'loom_web_{self._APOLLO_GRAPHQL_VERSION}',
  248. 'apollographql-client-name': 'web',
  249. 'apollographql-client-version': self._APOLLO_GRAPHQL_VERSION,
  250. }, data=json.dumps([{
  251. 'operationName': operation_name,
  252. 'variables': {
  253. 'videoId': video_id,
  254. 'password': password,
  255. **self._GRAPHQL_VARIABLES.get(operation_name, {}),
  256. },
  257. 'query': self._GRAPHQL_QUERIES[operation_name],
  258. } for operation_name in variadic(operations)], separators=(',', ':')).encode())
  259. def _call_url_api(self, endpoint, video_id):
  260. response = self._download_json(
  261. f'https://www.loom.com/api/campaigns/sessions/{video_id}/{endpoint}', video_id,
  262. f'Downloading {endpoint} JSON', f'Failed to download {endpoint} JSON', fatal=False,
  263. headers={'Accept': 'application/json', 'Content-Type': 'application/json'},
  264. data=json.dumps({
  265. 'anonID': str(uuid.uuid4()),
  266. 'deviceID': None,
  267. 'force_original': False, # HTTP error 401 if True
  268. 'password': self.get_param('videopassword'),
  269. }, separators=(',', ':')).encode())
  270. return traverse_obj(response, ('url', {url_or_none}))
  271. def _extract_formats(self, video_id, metadata, gql_data):
  272. formats = []
  273. video_properties = traverse_obj(metadata, ('video_properties', {
  274. 'width': ('width', {int_or_none}),
  275. 'height': ('height', {int_or_none}),
  276. 'acodec': ('microphone_enabled', {lambda x: 'none' if x is False else None}),
  277. }))
  278. def get_formats(format_url, format_id, quality):
  279. if not format_url:
  280. return
  281. ext = determine_ext(format_url)
  282. query = urllib.parse.urlparse(format_url).query
  283. if ext == 'm3u8':
  284. # Extract pre-merged HLS formats to avoid buggy parsing of metadata in split playlists
  285. format_url = format_url.replace('-split.m3u8', '.m3u8')
  286. m3u8_formats = self._extract_m3u8_formats(
  287. format_url, video_id, 'mp4', m3u8_id=f'hls-{format_id}', fatal=False, quality=quality)
  288. # Sometimes only split video/audio formats are available, need to fixup video-only formats
  289. is_not_premerged = 'none' in traverse_obj(m3u8_formats, (..., 'vcodec'))
  290. for fmt in m3u8_formats:
  291. if is_not_premerged and fmt.get('vcodec') != 'none':
  292. fmt['acodec'] = 'none'
  293. yield {
  294. **fmt,
  295. 'url': update_url(fmt['url'], query=query),
  296. 'extra_param_to_segment_url': query,
  297. }
  298. elif ext == 'mpd':
  299. dash_formats = self._extract_mpd_formats(
  300. format_url, video_id, mpd_id=f'dash-{format_id}', fatal=False)
  301. for fmt in dash_formats:
  302. yield {
  303. **fmt,
  304. 'extra_param_to_segment_url': query,
  305. 'quality': quality,
  306. }
  307. else:
  308. yield {
  309. 'url': format_url,
  310. 'ext': ext,
  311. 'format_id': f'http-{format_id}',
  312. 'quality': quality,
  313. **video_properties,
  314. }
  315. raw_url = self._call_url_api('raw-url', video_id)
  316. formats.extend(get_formats(raw_url, 'raw', quality=1)) # original quality
  317. transcoded_url = self._call_url_api('transcoded-url', video_id)
  318. formats.extend(get_formats(transcoded_url, 'transcoded', quality=-1)) # transcoded quality
  319. cdn_url = get_first(gql_data, ('data', 'getVideo', 'nullableRawCdnUrl', 'url', {url_or_none}))
  320. # cdn_url is usually a dupe, but the raw-url/transcoded-url endpoints could return errors
  321. valid_urls = [update_url(url, query=None) for url in (raw_url, transcoded_url) if url]
  322. if cdn_url and update_url(cdn_url, query=None) not in valid_urls:
  323. formats.extend(get_formats(cdn_url, 'cdn', quality=0)) # could be original or transcoded
  324. return formats
  325. def _real_extract(self, url):
  326. video_id = self._match_id(url)
  327. metadata = get_first(
  328. self._call_graphql_api('GetVideoSSR', video_id, 'Downloading GraphQL metadata JSON'),
  329. ('data', 'getVideo', {dict})) or {}
  330. if metadata.get('__typename') == 'VideoPasswordMissingOrIncorrect':
  331. if not self.get_param('videopassword'):
  332. raise ExtractorError(
  333. 'This video is password-protected, use the --video-password option', expected=True)
  334. raise ExtractorError('Invalid video password', expected=True)
  335. gql_data = self._call_graphql_api(['FetchChapters', 'FetchVideoTranscript', 'GetVideoSource'], video_id)
  336. duration = traverse_obj(metadata, ('video_properties', 'duration', {int_or_none}))
  337. return {
  338. 'id': video_id,
  339. 'duration': duration,
  340. 'chapters': self._extract_chapters_from_description(
  341. get_first(gql_data, ('data', 'fetchVideoChapters', 'content', {str})), duration) or None,
  342. 'formats': self._extract_formats(video_id, metadata, gql_data),
  343. 'subtitles': filter_dict({
  344. 'en': traverse_obj(gql_data, (
  345. ..., 'data', 'fetchVideoTranscript',
  346. ('source_url', 'captions_source_url'), {
  347. 'url': {url_or_none},
  348. })) or None,
  349. }),
  350. **traverse_obj(metadata, {
  351. 'title': ('name', {str}),
  352. 'description': ('description', {str}),
  353. 'uploader': ('owner', 'display_name', {str}),
  354. 'timestamp': ('createdAt', {parse_iso8601}),
  355. }),
  356. }
  357. class LoomFolderIE(InfoExtractor):
  358. IE_NAME = 'loom:folder'
  359. _VALID_URL = r'https?://(?:www\.)?loom\.com/share/folder/(?P<id>[\da-f]{32})'
  360. _TESTS = [{
  361. # 2 subfolders, no videos in root
  362. 'url': 'https://www.loom.com/share/folder/997db4db046f43e5912f10dc5f817b5c',
  363. 'playlist_mincount': 16,
  364. 'info_dict': {
  365. 'id': '997db4db046f43e5912f10dc5f817b5c',
  366. 'title': 'Blending Lessons',
  367. },
  368. }, {
  369. # only videos, no subfolders
  370. 'url': 'https://www.loom.com/share/folder/9a8a87f6b6f546d9a400c8e7575ff7f2',
  371. 'playlist_mincount': 12,
  372. 'info_dict': {
  373. 'id': '9a8a87f6b6f546d9a400c8e7575ff7f2',
  374. 'title': 'List A- a, i, o',
  375. },
  376. }, {
  377. # videos in root and empty subfolder
  378. 'url': 'https://www.loom.com/share/folder/886e534218c24fd292e97e9563078cc4',
  379. 'playlist_mincount': 21,
  380. 'info_dict': {
  381. 'id': '886e534218c24fd292e97e9563078cc4',
  382. 'title': 'Medicare Agent Training videos',
  383. },
  384. }, {
  385. # videos in root and videos in subfolders
  386. 'url': 'https://www.loom.com/share/folder/b72c4ecdf04745da9403926d80a40c38',
  387. 'playlist_mincount': 21,
  388. 'info_dict': {
  389. 'id': 'b72c4ecdf04745da9403926d80a40c38',
  390. 'title': 'Quick Altos Q & A Tutorials',
  391. },
  392. }, {
  393. # recursive folder extraction
  394. 'url': 'https://www.loom.com/share/folder/8b458a94e0e4449b8df9ea7a68fafc4e',
  395. 'playlist_count': 23,
  396. 'info_dict': {
  397. 'id': '8b458a94e0e4449b8df9ea7a68fafc4e',
  398. 'title': 'Sezer Texting Guide',
  399. },
  400. }, {
  401. # more than 50 videos in 1 folder
  402. 'url': 'https://www.loom.com/share/folder/e056a91d290d47ca9b00c9d1df56c463',
  403. 'playlist_mincount': 61,
  404. 'info_dict': {
  405. 'id': 'e056a91d290d47ca9b00c9d1df56c463',
  406. 'title': 'User Videos',
  407. },
  408. }, {
  409. # many subfolders
  410. 'url': 'https://www.loom.com/share/folder/c2dde8cc67454f0e99031677279d8954',
  411. 'playlist_mincount': 75,
  412. 'info_dict': {
  413. 'id': 'c2dde8cc67454f0e99031677279d8954',
  414. 'title': 'Honors 1',
  415. },
  416. }, {
  417. 'url': 'https://www.loom.com/share/folder/bae17109a68146c7803454f2893c8cf8/Edpuzzle',
  418. 'only_matching': True,
  419. }]
  420. def _extract_folder_data(self, folder_id):
  421. return self._download_json(
  422. f'https://www.loom.com/v1/folders/{folder_id}', folder_id,
  423. 'Downloading folder info JSON', query={'limit': '10000'})
  424. def _extract_folder_entries(self, folder_id, initial_folder_data=None):
  425. folder_data = initial_folder_data or self._extract_folder_data(folder_id)
  426. for video in traverse_obj(folder_data, ('videos', lambda _, v: v['id'])):
  427. video_id = video['id']
  428. yield self.url_result(
  429. f'https://www.loom.com/share/{video_id}', LoomIE, video_id, video.get('name'))
  430. # Recurse into subfolders
  431. for subfolder_id in traverse_obj(folder_data, (
  432. 'folders', lambda _, v: v['id'] != folder_id, 'id', {str})):
  433. yield from self._extract_folder_entries(subfolder_id)
  434. def _real_extract(self, url):
  435. playlist_id = self._match_id(url)
  436. playlist_data = self._extract_folder_data(playlist_id)
  437. return self.playlist_result(
  438. self._extract_folder_entries(playlist_id, playlist_data), playlist_id,
  439. traverse_obj(playlist_data, ('folder', 'name', {str.strip})))