Skip to content

Commit

Permalink
Add niconico views fallback.
Browse files Browse the repository at this point in the history
  • Loading branch information
Duosion committed Mar 6, 2024
1 parent 87a43f5 commit 416e937
Show file tree
Hide file tree
Showing 3 changed files with 201 additions and 2 deletions.
150 changes: 150 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"graphql": "^16.8.1",
"graphql-hooks": "^7.0.0",
"graphql-hooks-memcache": "^3.2.0",
"linkedom": "^0.16.8",
"material-symbols": "^0.14.7",
"negotiator": "^0.6.3",
"next": "^14.1.0",
Expand Down
52 changes: 50 additions & 2 deletions src/lib/platforms/Niconico.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { Platform, VideoId, VideoThumbnails } from "./types";
import { defaultFetchHeaders } from ".";
import { parseHTML } from "linkedom";

const nicoNicoVideoDomain = "https://www.nicovideo.jp/watch/"

const nicoNicoAPIDomain = "https://nvapi.nicovideo.jp/v1/"
const headers = {
Expand All @@ -10,6 +13,51 @@ const headers = {

class NiconicoPlatform implements Platform {

// fallback to
async getViewsFallback(
videoId: VideoId
): Promise<number | null> {
console.log('niconico views fallback')
const result = await fetch(nicoNicoVideoDomain + videoId, {
method: 'GET',
})
if (!result) return null

const text = await result.text()

const parsedHTML = parseHTML(text)
// parse data-api-data
const dataElement = parsedHTML.document.getElementById("js-initial-watch-data")
if (!dataElement) { return null }

const videoData = JSON.parse(dataElement.getAttribute("data-api-data") || '[]')?.video

const rawViews = videoData?.count?.view
return rawViews == undefined ? null : Number.parseInt(rawViews)
}

getThumbnailsFallback(
videoId: VideoId
): Promise<VideoThumbnails | null> {
return fetch(nicoNicoVideoDomain + videoId)
.then(response => response.text())
.then(text => {
const parsedHTML = parseHTML(text)
// parse data-api-data
const dataElement = parsedHTML.document.getElementById("js-initial-watch-data")
if (!dataElement) { return null }

const videoData = JSON.parse(dataElement.getAttribute("data-api-data") || '[]')?.video

const thumbnail = videoData?.thumbnail?.url
return thumbnail == undefined ? null : {
default: thumbnail,
quality: thumbnail
}
})
.catch(_ => { return null })
}

// https://niconicolibs.github.io/api/nvapi/#tag/Video
async getViews(
videoId: VideoId
Expand All @@ -20,7 +68,7 @@ class NiconicoPlatform implements Platform {
.then(videoData => {
return videoData['data']['items'][0]['video']['count']['view']
})
.catch(_ => { return null })
.catch(_ => { return this.getViewsFallback(videoId) })
}

getThumbnails(
Expand All @@ -37,7 +85,7 @@ class NiconicoPlatform implements Platform {
quality: thumbnails['largeUrl'] || defaultThumbnail
}
})
.catch(_ => { return null })
.catch(_ => { return this.getThumbnailsFallback(videoId) })
}

}
Expand Down

0 comments on commit 416e937

Please sign in to comment.