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

imgur.py (16003B)


  1. import functools
  2. import re
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. ExtractorError,
  6. determine_ext,
  7. float_or_none,
  8. int_or_none,
  9. js_to_json,
  10. mimetype2ext,
  11. parse_iso8601,
  12. str_or_none,
  13. strip_or_none,
  14. traverse_obj,
  15. url_or_none,
  16. )
  17. class ImgurBaseIE(InfoExtractor):
  18. _CLIENT_ID = '546c25a59c58ad7'
  19. @classmethod
  20. def _imgur_result(cls, item_id):
  21. return cls.url_result(f'https://imgur.com/{item_id}', ImgurIE, item_id)
  22. def _call_api(self, endpoint, video_id, **kwargs):
  23. return self._download_json(
  24. f'https://api.imgur.com/post/v1/{endpoint}/{video_id}?client_id={self._CLIENT_ID}&include=media,account',
  25. video_id, **kwargs)
  26. @staticmethod
  27. def get_description(s):
  28. if 'Discover the magic of the internet at Imgur' in s:
  29. return None
  30. return s or None
  31. class ImgurIE(ImgurBaseIE):
  32. _VALID_URL = r'https?://(?:i\.)?imgur\.com/(?!(?:a|gallery|t|topic|r)/)(?:[^/?#]+-)?(?P<id>[a-zA-Z0-9]+)'
  33. _TESTS = [{
  34. 'url': 'https://imgur.com/A61SaA1',
  35. 'info_dict': {
  36. 'id': 'A61SaA1',
  37. 'ext': 'mp4',
  38. 'title': 'MRW gifv is up and running without any bugs',
  39. 'timestamp': 1416446068,
  40. 'upload_date': '20141120',
  41. 'dislike_count': int,
  42. 'comment_count': int,
  43. 'release_timestamp': 1416446068,
  44. 'release_date': '20141120',
  45. 'like_count': int,
  46. 'thumbnail': 'https://i.imgur.com/A61SaA1h.jpg',
  47. },
  48. }, {
  49. # Test with URL slug
  50. 'url': 'https://imgur.com/mrw-gifv-is-up-running-without-any-bugs-A61SaA1',
  51. 'info_dict': {
  52. 'id': 'A61SaA1',
  53. 'ext': 'mp4',
  54. 'title': 'MRW gifv is up and running without any bugs',
  55. 'timestamp': 1416446068,
  56. 'upload_date': '20141120',
  57. 'dislike_count': int,
  58. 'comment_count': int,
  59. 'release_timestamp': 1416446068,
  60. 'release_date': '20141120',
  61. 'like_count': int,
  62. 'thumbnail': 'https://i.imgur.com/A61SaA1h.jpg',
  63. },
  64. }, {
  65. 'url': 'https://i.imgur.com/A61SaA1.gifv',
  66. 'only_matching': True,
  67. }, {
  68. 'url': 'https://i.imgur.com/crGpqCV.mp4',
  69. 'only_matching': True,
  70. }, {
  71. 'url': 'https://i.imgur.com/jxBXAMC.gifv',
  72. 'info_dict': {
  73. 'id': 'jxBXAMC',
  74. 'ext': 'mp4',
  75. 'title': 'Fahaka puffer feeding',
  76. 'timestamp': 1533835503,
  77. 'upload_date': '20180809',
  78. 'release_date': '20180809',
  79. 'like_count': int,
  80. 'duration': 30.0,
  81. 'comment_count': int,
  82. 'release_timestamp': 1533835503,
  83. 'thumbnail': 'https://i.imgur.com/jxBXAMCh.jpg',
  84. 'dislike_count': int,
  85. },
  86. }, {
  87. # needs Accept header, ref: https://github.com/yt-dlp/yt-dlp/issues/9458
  88. 'url': 'https://imgur.com/zV03bd5',
  89. 'md5': '59df97884e8ba76143ff6b640a0e2904',
  90. 'info_dict': {
  91. 'id': 'zV03bd5',
  92. 'ext': 'mp4',
  93. 'title': 'Ive - Liz',
  94. 'timestamp': 1710491255,
  95. 'upload_date': '20240315',
  96. 'like_count': int,
  97. 'dislike_count': int,
  98. 'duration': 56.92,
  99. 'comment_count': int,
  100. 'release_timestamp': 1710491255,
  101. 'release_date': '20240315',
  102. 'thumbnail': 'https://i.imgur.com/zV03bd5h.jpg',
  103. },
  104. }]
  105. def _real_extract(self, url):
  106. video_id = self._match_id(url)
  107. data = self._call_api('media', video_id)
  108. if not traverse_obj(data, ('media', 0, (
  109. ('type', {lambda t: t == 'video' or None}),
  110. ('metadata', 'is_animated'))), get_all=False):
  111. raise ExtractorError(f'{video_id} is not a video or animated image', expected=True)
  112. webpage = self._download_webpage(
  113. f'https://i.imgur.com/{video_id}.gifv', video_id, fatal=False) or ''
  114. formats = []
  115. media_fmt = traverse_obj(data, ('media', 0, {
  116. 'url': ('url', {url_or_none}),
  117. 'ext': ('ext', {str}),
  118. 'width': ('width', {int_or_none}),
  119. 'height': ('height', {int_or_none}),
  120. 'filesize': ('size', {int_or_none}),
  121. 'acodec': ('metadata', 'has_sound', {lambda b: None if b else 'none'}),
  122. }))
  123. media_url = media_fmt.get('url')
  124. if media_url:
  125. if not media_fmt.get('ext'):
  126. media_fmt['ext'] = mimetype2ext(traverse_obj(
  127. data, ('media', 0, 'mime_type'))) or determine_ext(media_url)
  128. if traverse_obj(data, ('media', 0, 'type')) == 'image':
  129. media_fmt['acodec'] = 'none'
  130. media_fmt.setdefault('preference', -10)
  131. formats.append(media_fmt)
  132. video_elements = self._search_regex(
  133. r'(?s)<div class="video-elements">(.*?)</div>',
  134. webpage, 'video elements', default=None)
  135. if video_elements:
  136. def og_get_size(media_type):
  137. return {
  138. p: int_or_none(self._og_search_property(f'{media_type}:{p}', webpage, default=None))
  139. for p in ('width', 'height')
  140. }
  141. size = og_get_size('video')
  142. if not any(size.values()):
  143. size = og_get_size('image')
  144. formats = traverse_obj(
  145. re.finditer(r'<source\s+src="(?P<src>[^"]+)"\s+type="(?P<type>[^"]+)"', video_elements),
  146. (..., {
  147. 'format_id': ('type', {lambda s: s.partition('/')[2]}),
  148. 'url': ('src', {self._proto_relative_url}),
  149. 'ext': ('type', {mimetype2ext}),
  150. }))
  151. for f in formats:
  152. f.update(size)
  153. # We can get the original gif format from the webpage as well
  154. gif_json = traverse_obj(self._search_json(
  155. r'var\s+videoItem\s*=', webpage, 'GIF info', video_id,
  156. transform_source=js_to_json, fatal=False), {
  157. 'url': ('gifUrl', {self._proto_relative_url}),
  158. 'filesize': ('size', {int_or_none}),
  159. })
  160. if gif_json:
  161. gif_json.update(size)
  162. gif_json.update({
  163. 'format_id': 'gif',
  164. 'preference': -10, # gifs < videos
  165. 'ext': 'gif',
  166. 'acodec': 'none',
  167. 'vcodec': 'gif',
  168. 'container': 'gif',
  169. })
  170. formats.append(gif_json)
  171. search = functools.partial(self._html_search_meta, html=webpage, default=None)
  172. twitter_fmt = {
  173. 'format_id': 'twitter',
  174. 'url': url_or_none(search('twitter:player:stream')),
  175. 'ext': mimetype2ext(search('twitter:player:stream:content_type')),
  176. 'width': int_or_none(search('twitter:width')),
  177. 'height': int_or_none(search('twitter:height')),
  178. }
  179. if twitter_fmt['url']:
  180. formats.append(twitter_fmt)
  181. if not formats:
  182. self.raise_no_formats(
  183. f'No sources found for video {video_id}. Maybe a plain image?', expected=True)
  184. self._remove_duplicate_formats(formats)
  185. return {
  186. 'title': self._og_search_title(webpage, default=None),
  187. 'description': self.get_description(self._og_search_description(webpage, default='')),
  188. **traverse_obj(data, {
  189. 'uploader_id': ('account_id', {lambda a: str(a) if int_or_none(a) else None}),
  190. 'uploader': ('account', 'username', {lambda x: strip_or_none(x) or None}),
  191. 'uploader_url': ('account', 'avatar_url', {url_or_none}),
  192. 'like_count': ('upvote_count', {int_or_none}),
  193. 'dislike_count': ('downvote_count', {int_or_none}),
  194. 'comment_count': ('comment_count', {int_or_none}),
  195. 'age_limit': ('is_mature', {lambda x: 18 if x else None}),
  196. 'timestamp': (('updated_at', 'created_at'), {parse_iso8601}),
  197. 'release_timestamp': ('created_at', {parse_iso8601}),
  198. }, get_all=False),
  199. **traverse_obj(data, ('media', 0, 'metadata', {
  200. 'title': ('title', {lambda x: strip_or_none(x) or None}),
  201. 'description': ('description', {self.get_description}),
  202. 'duration': ('duration', {float_or_none}),
  203. 'timestamp': (('updated_at', 'created_at'), {parse_iso8601}),
  204. 'release_timestamp': ('created_at', {parse_iso8601}),
  205. }), get_all=False),
  206. 'id': video_id,
  207. 'formats': formats,
  208. 'thumbnails': [{
  209. 'url': thumbnail_url,
  210. 'http_headers': {'Accept': '*/*'},
  211. }] if (thumbnail_url := search(['thumbnailUrl', 'twitter:image', 'og:image'])) else None,
  212. 'http_headers': {'Accept': '*/*'},
  213. }
  214. class ImgurGalleryBaseIE(ImgurBaseIE):
  215. _GALLERY = True
  216. def _real_extract(self, url):
  217. gallery_id = self._match_id(url)
  218. data = self._call_api('albums', gallery_id, fatal=False, expected_status=404)
  219. info = traverse_obj(data, {
  220. 'title': ('title', {lambda x: strip_or_none(x) or None}),
  221. 'description': ('description', {self.get_description}),
  222. })
  223. if traverse_obj(data, 'is_album'):
  224. items = traverse_obj(data, (
  225. 'media', lambda _, v: v.get('type') == 'video' or v['metadata']['is_animated'],
  226. 'id', {lambda x: str_or_none(x) or None}))
  227. # if a gallery with exactly one video, apply album metadata to video
  228. media_id = None
  229. if self._GALLERY and len(items) == 1:
  230. media_id = items[0]
  231. if not media_id:
  232. result = self.playlist_result(
  233. map(self._imgur_result, items), gallery_id)
  234. result.update(info)
  235. return result
  236. gallery_id = media_id
  237. result = self._imgur_result(gallery_id)
  238. info['_type'] = 'url_transparent'
  239. result.update(info)
  240. return result
  241. class ImgurGalleryIE(ImgurGalleryBaseIE):
  242. IE_NAME = 'imgur:gallery'
  243. _VALID_URL = r'https?://(?:i\.)?imgur\.com/(?:gallery|(?:t(?:opic)?|r)/[^/?#]+)/(?:[^/?#]+-)?(?P<id>[a-zA-Z0-9]+)'
  244. _TESTS = [{
  245. # TODO: static images - replace with animated/video gallery
  246. 'url': 'http://imgur.com/topic/Aww/ll5Vk',
  247. 'only_matching': True,
  248. }, {
  249. 'url': 'https://imgur.com/gallery/YcAQlkx',
  250. 'add_ies': ['Imgur'],
  251. 'info_dict': {
  252. 'id': 'YcAQlkx',
  253. 'ext': 'mp4',
  254. 'title': 'Classic Steve Carell gif...cracks me up everytime....damn the repost downvotes....',
  255. 'timestamp': 1358554297,
  256. 'upload_date': '20130119',
  257. 'uploader_id': '1648642',
  258. 'uploader': 'wittyusernamehere',
  259. 'release_timestamp': 1358554297,
  260. 'thumbnail': 'https://i.imgur.com/YcAQlkxh.jpg',
  261. 'release_date': '20130119',
  262. 'uploader_url': 'https://i.imgur.com/N5Flb2v_d.png?maxwidth=290&fidelity=grand',
  263. 'comment_count': int,
  264. 'dislike_count': int,
  265. 'like_count': int,
  266. },
  267. }, {
  268. # Test with slug
  269. 'url': 'https://imgur.com/gallery/classic-steve-carell-gif-cracks-me-up-everytime-repost-downvotes-YcAQlkx',
  270. 'add_ies': ['Imgur'],
  271. 'info_dict': {
  272. 'id': 'YcAQlkx',
  273. 'ext': 'mp4',
  274. 'title': 'Classic Steve Carell gif...cracks me up everytime....damn the repost downvotes....',
  275. 'timestamp': 1358554297,
  276. 'upload_date': '20130119',
  277. 'uploader_id': '1648642',
  278. 'uploader': 'wittyusernamehere',
  279. 'release_timestamp': 1358554297,
  280. 'release_date': '20130119',
  281. 'thumbnail': 'https://i.imgur.com/YcAQlkxh.jpg',
  282. 'uploader_url': 'https://i.imgur.com/N5Flb2v_d.png?maxwidth=290&fidelity=grand',
  283. 'comment_count': int,
  284. 'dislike_count': int,
  285. 'like_count': int,
  286. },
  287. }, {
  288. # TODO: static image - replace with animated/video gallery
  289. 'url': 'http://imgur.com/topic/Funny/N8rOudd',
  290. 'only_matching': True,
  291. }, {
  292. 'url': 'http://imgur.com/r/aww/VQcQPhM',
  293. 'add_ies': ['Imgur'],
  294. 'info_dict': {
  295. 'id': 'VQcQPhM',
  296. 'ext': 'mp4',
  297. 'title': 'The boss is here',
  298. 'timestamp': 1476494751,
  299. 'upload_date': '20161015',
  300. 'uploader_id': '19138530',
  301. 'uploader': 'thematrixcam',
  302. 'comment_count': int,
  303. 'dislike_count': int,
  304. 'uploader_url': 'https://i.imgur.com/qCjr5Pi_d.png?maxwidth=290&fidelity=grand',
  305. 'release_timestamp': 1476494751,
  306. 'like_count': int,
  307. 'release_date': '20161015',
  308. 'thumbnail': 'https://i.imgur.com/VQcQPhMh.jpg',
  309. },
  310. },
  311. # from https://github.com/ytdl-org/youtube-dl/pull/16674
  312. {
  313. 'url': 'https://imgur.com/t/unmuted/6lAn9VQ',
  314. 'info_dict': {
  315. 'id': '6lAn9VQ',
  316. 'title': 'Penguins !',
  317. },
  318. 'playlist_count': 3,
  319. }, {
  320. 'url': 'https://imgur.com/t/unmuted/penguins-penguins-6lAn9VQ',
  321. 'info_dict': {
  322. 'id': '6lAn9VQ',
  323. 'title': 'Penguins !',
  324. },
  325. 'playlist_count': 3,
  326. }, {
  327. 'url': 'https://imgur.com/t/unmuted/kx2uD3C',
  328. 'add_ies': ['Imgur'],
  329. 'info_dict': {
  330. 'id': 'ZVMv45i',
  331. 'ext': 'mp4',
  332. 'title': 'Intruder',
  333. 'timestamp': 1528129683,
  334. 'upload_date': '20180604',
  335. 'release_timestamp': 1528129683,
  336. 'release_date': '20180604',
  337. 'like_count': int,
  338. 'dislike_count': int,
  339. 'comment_count': int,
  340. 'duration': 30.03,
  341. 'thumbnail': 'https://i.imgur.com/ZVMv45ih.jpg',
  342. },
  343. }, {
  344. 'url': 'https://imgur.com/t/unmuted/wXSK0YH',
  345. 'add_ies': ['Imgur'],
  346. 'info_dict': {
  347. 'id': 'JCAP4io',
  348. 'ext': 'mp4',
  349. 'title': 're:I got the blues$',
  350. 'description': 'Luka’s vocal stylings.\n\nFP edit: don’t encourage me. I’ll never stop posting Luka and friends.',
  351. 'timestamp': 1527809525,
  352. 'upload_date': '20180531',
  353. 'like_count': int,
  354. 'dislike_count': int,
  355. 'duration': 30.03,
  356. 'comment_count': int,
  357. 'release_timestamp': 1527809525,
  358. 'thumbnail': 'https://i.imgur.com/JCAP4ioh.jpg',
  359. 'release_date': '20180531',
  360. },
  361. }]
  362. class ImgurAlbumIE(ImgurGalleryBaseIE):
  363. IE_NAME = 'imgur:album'
  364. _VALID_URL = r'https?://(?:i\.)?imgur\.com/a/(?:[^/?#]+-)?(?P<id>[a-zA-Z0-9]+)'
  365. _GALLERY = False
  366. _TESTS = [{
  367. # TODO: only static images - replace with animated/video gallery
  368. 'url': 'http://imgur.com/a/j6Orj',
  369. 'only_matching': True,
  370. },
  371. # from https://github.com/ytdl-org/youtube-dl/pull/21693
  372. {
  373. 'url': 'https://imgur.com/a/iX265HX',
  374. 'info_dict': {
  375. 'id': 'iX265HX',
  376. 'title': 'enen-no-shouboutai',
  377. },
  378. 'playlist_count': 2,
  379. }, {
  380. # Test with URL slug
  381. 'url': 'https://imgur.com/a/enen-no-shouboutai-iX265HX',
  382. 'info_dict': {
  383. 'id': 'iX265HX',
  384. 'title': 'enen-no-shouboutai',
  385. },
  386. 'playlist_count': 2,
  387. }, {
  388. 'url': 'https://imgur.com/a/8pih2Ed',
  389. 'info_dict': {
  390. 'id': '8pih2Ed',
  391. },
  392. 'playlist_mincount': 1,
  393. }]