-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
api: add support for xiaohongshu (rednote) #1064
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
9bdcb9d
api/utils: update getRedirectingURL to accept more statuses & dispatcher
wukko 63b2681
api/match-action: always proxy photos
wukko ed8f435
api/processing: add support for xiaohongshu
wukko ad6f29a
api/tests: add xiaohongshu tests
wukko cd466a4
api/tests/bsky: fix expected photo test status
wukko cd0a2a4
api/tests/pinterest: update expected photo status
wukko de5eca1
api/utils: replace redirectStatuses array with a set
wukko 3cbed87
api/xiaohongshu: update initial state extraction regex
wukko 4963c9f
api/xiaohongshu: remove duplicated extraction error
wukko e39b0ae
api/xiaohongshu: deduplicate h264 stream extraction
wukko 7488c74
api/xiaohongshu: clean up the h265-h264 if statement
wukko 9f0f885
web/settings/video: update h265 toggle strings
wukko 73f458a
docs/api: update tiktokH265 description
wukko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| import { extract, normalizeURL } from "../url.js"; | ||
| import { genericUserAgent } from "../../config.js"; | ||
| import { createStream } from "../../stream/manage.js"; | ||
| import { getRedirectingURL } from "../../misc/utils.js"; | ||
|
|
||
| const https = (url) => { | ||
| return url.replace(/^http:/i, 'https:'); | ||
| } | ||
|
|
||
| export default async function ({ id, token, shareId, h265, isAudioOnly, dispatcher }) { | ||
| let noteId = id; | ||
| let xsecToken = token; | ||
|
|
||
| if (!noteId) { | ||
| const extractedURL = await getRedirectingURL( | ||
| `https://xhslink.com/a/${shareId}`, | ||
| dispatcher | ||
| ); | ||
|
|
||
| if (extractedURL) { | ||
| const { patternMatch } = extract(normalizeURL(extractedURL)); | ||
|
|
||
| if (patternMatch) { | ||
| noteId = patternMatch.id; | ||
| xsecToken = patternMatch.token; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (!noteId || !xsecToken) return { error: "fetch.short_link" }; | ||
|
|
||
| const res = await fetch(`https://www.xiaohongshu.com/explore/${noteId}?xsec_token=${xsecToken}`, { | ||
| headers: { | ||
| "user-agent": genericUserAgent, | ||
| }, | ||
| dispatcher, | ||
| }); | ||
|
|
||
| const html = await res.text(); | ||
|
|
||
| let note; | ||
| try { | ||
| const initialState = html | ||
| .split('<script>window.__INITIAL_STATE__=')[1] | ||
| .split('</script>')[0] | ||
| .replace(/:undefined/g, ":null"); | ||
wukko marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| const data = JSON.parse(initialState); | ||
|
|
||
| const noteInfo = data?.note?.noteDetailMap; | ||
| if (!noteInfo) throw "no note detail map"; | ||
|
|
||
| const currentNote = noteInfo[noteId]; | ||
| if (!currentNote) throw "no current note in detail map"; | ||
|
|
||
| note = currentNote.note; | ||
| } catch { | ||
| return { error: "fetch.empty" }; | ||
| } | ||
wukko marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| if (!note) return { error: "fetch.empty" }; | ||
|
|
||
| const video = note.video; | ||
| const images = note.imageList; | ||
|
|
||
| const filenameBase = `xiaohongshu_${noteId}`; | ||
|
|
||
| if (video) { | ||
| const videoFilename = `${filenameBase}.mp4`; | ||
| const audioFilename = `${filenameBase}_audio`; | ||
|
|
||
| let videoURL; | ||
|
|
||
| if (h265 && !isAudioOnly && video.consumer?.originVideoKey) { | ||
| videoURL = `https://sns-video-bd.xhscdn.com/${video.consumer.originVideoKey}`; | ||
| } | ||
|
|
||
| if (!videoURL) { | ||
wukko marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const h264Streams = video.media?.stream?.h264; | ||
| if (!h264Streams) return { error: "fetch.empty" }; | ||
|
|
||
| if (h264Streams.length > 1) { | ||
| videoURL = h264Streams.reduce((a, b) => Number(a?.videoBitrate) > Number(b?.videoBitrate) ? a : b).masterUrl; | ||
| } else { | ||
| videoURL = h264Streams[0].masterUrl; | ||
| } | ||
wukko marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| if (!videoURL) return { error: "fetch.empty" }; | ||
|
|
||
| return { | ||
| urls: https(videoURL), | ||
| filename: videoFilename, | ||
| audioFilename: audioFilename, | ||
| } | ||
| } | ||
|
|
||
| if (!images || images.length === 0) { | ||
| return { error: "fetch.empty" }; | ||
| } | ||
|
|
||
| if (images.length === 1) { | ||
| return { | ||
| isPhoto: true, | ||
| urls: https(images[0].urlDefault), | ||
| filename: `${filenameBase}.jpg`, | ||
| } | ||
| } | ||
|
|
||
| const picker = images.map((image, i) => { | ||
| return { | ||
| type: "photo", | ||
| url: createStream({ | ||
| service: "xiaohongshu", | ||
| type: "proxy", | ||
| url: https(image.urlDefault), | ||
| filename: `${filenameBase}_${i + 1}.jpg`, | ||
| }) | ||
| } | ||
| }); | ||
|
|
||
| return { picker }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.