Skip to content

Commit

Permalink
🎨 [annict] armにないデータでも大まかに引っ張られるように
Browse files Browse the repository at this point in the history
  • Loading branch information
ci7lus committed Oct 1, 2022
1 parent b6f1c31 commit 71e469a
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/miraktest-annict/annictAPI.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import axios from "axios"
import { MeProgramsGetRequestQuery } from "./annict.js/types/me/programs"
import { WorksGetRequestQuery } from "./annict.js/types/works"
import { getSdk } from "./gql"
import { GraphQLClient } from "./graphqlRequestLoader"

Expand All @@ -15,6 +16,21 @@ export class AnnictRESTAPI {
})
}

async getWorks(params: Partial<WorksGetRequestQuery>) {
return this.client.get<{
works: {
id: number
title: string
}[]
}>("works", {
params: {
...params,
filter_title: params.filter_title?.join(","),
filter_season: params.filter_season?.join(","),
},
})
}

async getMyPrograms(params: MeProgramsGetRequestQuery) {
return this.client.get<{
next_page: 2
Expand Down
2 changes: 1 addition & 1 deletion src/miraktest-annict/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const ANNICT_META = {
id: ANNICT_PLUGIN_ID,
name: "Annict",
author: "ci7lus",
version: "0.3.5",
version: "0.3.6",
description: "視聴中の番組をAnnictで記録する",
authorUrl: "https://github.com/ci7lus",
url: "https://github.com/ci7lus/miraktest-plugins/tree/master/src/miraktest-annict",
Expand Down
42 changes: 42 additions & 0 deletions src/miraktest-annict/findWork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ import { ARM } from "./types"
dayjs.extend(isSameOrAfter)
dayjs.extend(isSameOrBefore)

export const getAnnictSeasonByMonth = (n: number) => {
if (n <= 3) {
return "winter"
} else if (n <= 6) {
return "spring"
} else if (n <= 9) {
return "summer"
} else {
return "autumn"
}
}

export const detectProgramInfo = async ({
rest,
channel,
Expand Down Expand Up @@ -65,6 +77,36 @@ export const detectProgramInfo = async ({
number: syobocalProgram.Count,
},
}
} else {
const workReq = await SyobocalAPI.TitleLookup({
TID: syobocalProgram.TID.toString(),
})
const syobocalWork = workReq.slice(0).shift()
if (!syobocalWork) {
return
}
const season = getAnnictSeasonByMonth(syobocalWork.FirstMonth)
for (const term of [
syobocalWork.Title,
syobocalWork.TitleYomi,
syobocalWork.ShortTitle,
syobocalWork.TitleEN,
].filter((s): s is string => !!s)) {
const annictWorkSearchReq = await rest.getWorks({
filter_title: [term],
filter_season: [`${syobocalWork.FirstYear}-${season}`],
})
const annictWork = annictWorkSearchReq.data.works.slice(0).shift()
if (annictWork) {
return {
annictId: annictWork.id,
episode: {
title: syobocalProgram.SubTitle,
number: syobocalProgram.Count,
},
}
}
}
}
} else {
console.warn("番組が見つかりませんでした")
Expand Down
49 changes: 49 additions & 0 deletions src/miraktest-annict/syobocalAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,35 @@ type ProgLookupResponse =
}
}

type TitleItem = {
Title: string
ShortTitle?: string
TitleYomi?: string
TitleEN?: string
FirstYear: number
FirstMonth: number
}

type TitleLookupResponse =
| {
TitleItems: {
TitleItem: TitleItem[]
}
Result: {
Code: 200
Message: ""
}
}
| {
TitleItems: {
TitleItem: TitleItem[]
}
Result: {
Code: 404
Message: "条件に一致するデータは存在しません"
}
}

export class SyobocalAPI {
static async ProgLookup(params: {
TID?: string
Expand Down Expand Up @@ -72,4 +101,24 @@ export class SyobocalAPI {
return [ProgLookupResponse.ProgItems.ProgItem].flat()
})
}
static async TitleLookup(params: { TID?: string }) {
return client
.get<string>("db.php", {
params: {
Command: "TitleLookup",
...params,
},
})
.then((r) => {
const {
TitleLookupResponse,
}: {
TitleLookupResponse: TitleLookupResponse
} = new XMLParser().parse(r.data)
if (TitleLookupResponse.Result.Code === 404) {
return []
}
return [TitleLookupResponse.TitleItems.TitleItem].flat()
})
}
}

0 comments on commit 71e469a

Please sign in to comment.