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

xiaohongshu.py (4250B)


  1. from .common import InfoExtractor
  2. from ..utils import (
  3. float_or_none,
  4. int_or_none,
  5. js_to_json,
  6. url_or_none,
  7. )
  8. from ..utils.traversal import traverse_obj
  9. class XiaoHongShuIE(InfoExtractor):
  10. _VALID_URL = r'https?://www\.xiaohongshu\.com/(?:explore|discovery/item)/(?P<id>[\da-f]+)'
  11. IE_DESC = '小红书'
  12. _TESTS = [{
  13. 'url': 'https://www.xiaohongshu.com/explore/6411cf99000000001300b6d9',
  14. 'md5': '2a87a77ddbedcaeeda8d7eae61b61228',
  15. 'info_dict': {
  16. 'id': '6411cf99000000001300b6d9',
  17. 'ext': 'mp4',
  18. 'uploader_id': '5c31698d0000000007018a31',
  19. 'description': '#今日快乐今日发[话题]# #吃货薯看这里[话题]# #香妃蛋糕[话题]# #小五卷蛋糕[话题]# #新手蛋糕卷[话题]#',
  20. 'title': '香妃蛋糕也太香了吧🔥不需要卷❗️绝对的友好',
  21. 'tags': ['今日快乐今日发', '吃货薯看这里', '香妃蛋糕', '小五卷蛋糕', '新手蛋糕卷'],
  22. 'duration': 101.726,
  23. 'thumbnail': r're:https?://sns-webpic-qc\.xhscdn\.com/\d+/[a-z0-9]+/[\w]+',
  24. },
  25. }, {
  26. 'url': 'https://www.xiaohongshu.com/discovery/item/674051740000000007027a15?xsec_token=CBgeL8Dxd1ZWBhwqRd568gAZ_iwG-9JIf9tnApNmteU2E=',
  27. 'info_dict': {
  28. 'id': '674051740000000007027a15',
  29. 'ext': 'mp4',
  30. 'title': '相互喜欢就可以了',
  31. 'uploader_id': '63439913000000001901f49a',
  32. 'duration': 28.073,
  33. 'description': '#广州[话题]# #深圳[话题]# #香港[话题]# #街头采访[话题]# #是你喜欢的类型[话题]#',
  34. 'thumbnail': r're:https?://sns-webpic-qc\.xhscdn\.com/\d+/[\da-f]+/[^/]+',
  35. 'tags': ['广州', '深圳', '香港', '街头采访', '是你喜欢的类型'],
  36. },
  37. }]
  38. def _real_extract(self, url):
  39. display_id = self._match_id(url)
  40. webpage = self._download_webpage(url, display_id)
  41. initial_state = self._search_json(
  42. r'window\.__INITIAL_STATE__\s*=', webpage, 'initial state', display_id, transform_source=js_to_json)
  43. note_info = traverse_obj(initial_state, ('note', 'noteDetailMap', display_id, 'note'))
  44. video_info = traverse_obj(note_info, ('video', 'media', 'stream', ('h264', 'av1', 'h265'), ...))
  45. formats = []
  46. for info in video_info:
  47. format_info = traverse_obj(info, {
  48. 'fps': ('fps', {int_or_none}),
  49. 'width': ('width', {int_or_none}),
  50. 'height': ('height', {int_or_none}),
  51. 'vcodec': ('videoCodec', {str}),
  52. 'acodec': ('audioCodec', {str}),
  53. 'abr': ('audioBitrate', {int_or_none}),
  54. 'vbr': ('videoBitrate', {int_or_none}),
  55. 'audio_channels': ('audioChannels', {int_or_none}),
  56. 'tbr': ('avgBitrate', {int_or_none}),
  57. 'format': ('qualityType', {str}),
  58. 'filesize': ('size', {int_or_none}),
  59. 'duration': ('duration', {float_or_none(scale=1000)}),
  60. })
  61. formats.extend(traverse_obj(info, (('mediaUrl', ('backupUrls', ...)), {
  62. lambda u: url_or_none(u) and {'url': u, **format_info}})))
  63. thumbnails = []
  64. for image_info in traverse_obj(note_info, ('imageList', ...)):
  65. thumbnail_info = traverse_obj(image_info, {
  66. 'height': ('height', {int_or_none}),
  67. 'width': ('width', {int_or_none}),
  68. })
  69. for thumb_url in traverse_obj(image_info, (('urlDefault', 'urlPre'), {url_or_none})):
  70. thumbnails.append({
  71. 'url': thumb_url,
  72. **thumbnail_info,
  73. })
  74. return {
  75. 'id': display_id,
  76. 'formats': formats,
  77. 'thumbnails': thumbnails,
  78. 'title': self._html_search_meta(['og:title'], webpage, default=None),
  79. **traverse_obj(note_info, {
  80. 'title': ('title', {str}),
  81. 'description': ('desc', {str}),
  82. 'tags': ('tagList', ..., 'name', {str}),
  83. 'uploader_id': ('user', 'userId', {str}),
  84. }),
  85. }