|
| 1 | +import { genericUserAgent } from "../../config.js"; |
| 2 | + |
| 3 | +const genericHeaders = { |
| 4 | + "user-agent": genericUserAgent, |
| 5 | + "accept-language": "en-US,en;q=0.9", |
| 6 | +} |
| 7 | + |
| 8 | +const getVideoInfo = async (id, dispatcher, quality) => { |
| 9 | + const html = await fetch(`https://www.nicovideo.jp/watch/${id}`, { |
| 10 | + dispatcher, |
| 11 | + headers: genericHeaders |
| 12 | + }).then(r => r.text()).catch(() => {}); |
| 13 | + |
| 14 | + if (!(html.includes("accessRightKey") |
| 15 | + || !(html.includes('<meta name="server-response" content="')))) { |
| 16 | + return { error: "fetch.fail" }; |
| 17 | + } |
| 18 | + |
| 19 | + const rawContent = |
| 20 | + html.split('<meta name="server-response" content="')[1] |
| 21 | + .split('" />')[0] |
| 22 | + .replaceAll(""", '"'); |
| 23 | + |
| 24 | + const data = JSON.parse(rawContent)?.data?.response; |
| 25 | + |
| 26 | + if (!data) { |
| 27 | + return { error: "fetch.fail" }; |
| 28 | + } |
| 29 | + |
| 30 | + const audio = data.media?.domand?.audios.find(audio => audio.isAvailable); |
| 31 | + const bestVideo = data.media?.domand?.videos.find(video => video.isAvailable); |
| 32 | + |
| 33 | + const preferredVideo = data.media?.domand?.videos.find(video => |
| 34 | + video.isAvailable && video.label.split('p')[0] === quality |
| 35 | + ); |
| 36 | + |
| 37 | + const video = preferredVideo || bestVideo; |
| 38 | + |
| 39 | + return { |
| 40 | + watchTrackId: data.client?.watchTrackId, |
| 41 | + accessRightKey: data.media?.domand?.accessRightKey, |
| 42 | + |
| 43 | + video, |
| 44 | + |
| 45 | + outputs: [[video.id, audio.id]], |
| 46 | + |
| 47 | + author: data.owner?.nickname, |
| 48 | + title: data.video?.title, |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +const getHls = async (dispatcher, id, trackId, accessRightKey, outputs) => { |
| 53 | + const response = await fetch( |
| 54 | + `https://nvapi.nicovideo.jp/v1/watch/${id}/access-rights/hls?actionTrackId=${trackId}`, |
| 55 | + { |
| 56 | + method: "POST", |
| 57 | + dispatcher, |
| 58 | + headers: { |
| 59 | + ...genericHeaders, |
| 60 | + "content-type": "application/json", |
| 61 | + "x-access-right-key": accessRightKey, |
| 62 | + "x-frontend-id": "6", |
| 63 | + "x-frontend-version": "0", |
| 64 | + "x-request-with": "nicovideo", |
| 65 | + }, |
| 66 | + body: JSON.stringify({ |
| 67 | + outputs, |
| 68 | + }) |
| 69 | + } |
| 70 | + ).then(r => r.json()).catch(() => {}); |
| 71 | + |
| 72 | + if (!response?.data?.contentUrl) return; |
| 73 | + return response.data.contentUrl; |
| 74 | +} |
| 75 | + |
| 76 | +export default async function ({ id, dispatcher, quality }) { |
| 77 | + const { |
| 78 | + watchTrackId, |
| 79 | + accessRightKey, |
| 80 | + video, |
| 81 | + outputs, |
| 82 | + author, |
| 83 | + title, |
| 84 | + error |
| 85 | + } = await getVideoInfo(id, dispatcher, quality); |
| 86 | + |
| 87 | + if (error) { |
| 88 | + return { error }; |
| 89 | + } |
| 90 | + |
| 91 | + if (!watchTrackId || !accessRightKey || !outputs) { |
| 92 | + return { error: "fetch.empty" }; |
| 93 | + } |
| 94 | + |
| 95 | + const hlsUrl = await getHls( |
| 96 | + dispatcher, |
| 97 | + id, |
| 98 | + watchTrackId, |
| 99 | + accessRightKey, |
| 100 | + outputs |
| 101 | + ); |
| 102 | + |
| 103 | + if (!hlsUrl) { |
| 104 | + return { error: "fetch.empty" }; |
| 105 | + } |
| 106 | + |
| 107 | + return { |
| 108 | + urls: hlsUrl, |
| 109 | + isHLS: true, |
| 110 | + filenameAttributes: { |
| 111 | + service: "nicovideo", |
| 112 | + id, |
| 113 | + title, |
| 114 | + author, |
| 115 | + resolution: `${video.width}x${video.height}`, |
| 116 | + qualityLabel: `${video.label}`, |
| 117 | + extension: "mp4" |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments