Skip to content

Commit

Permalink
Fix language support for news articles
Browse files Browse the repository at this point in the history
  • Loading branch information
Tibowl committed Oct 11, 2021
1 parent f83ef59 commit 3e8c530
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 21 deletions.
44 changes: 31 additions & 13 deletions src/commands/news/news.ts
Expand Up @@ -4,14 +4,15 @@ import Command from "../../utils/Command"
import client from "../../main"
import { Colors, findFuzzy, getNewsEmbed, parseNewsContent, sendMessage, simplePaginator } from "../../utils/Utils"
import config from "../../data/config.json"
import { NewsLang } from "../../utils/Types"

export default class News extends Command {
constructor(name: string) {
super({
name,
category: "News",
usage: "news [language or id]",
help: `Check up on latest news. You can follow the English ones with \`${config.prefix}follow add news_en-us\`
usage: "news [language] [id]",
help: `Check up on latest news. You can follow the English ones with \`${config.prefix}follow add news_en-us\` (see \`${config.prefix}help follow\` for the others)
Supported languages: ${client.newsManager.getLanguages().map(l => `\`${l}\``).join(", ")}`,
aliases: ["new", "n"]
})
Expand All @@ -20,18 +21,18 @@ Supported languages: ${client.newsManager.getLanguages().map(l => `\`${l}\``).jo
async run(message: Message, args: string[]): Promise<Message | Message[] | undefined> {
const { newsManager } = client

if (args.length == 0 || !args[0].match(/^\d+/)) {
let lang = findFuzzy([
...newsManager.getLanguages(),
...newsManager.getLanguages().map(l => newsManager.getLanguageName(l))
], args[0] ?? "en-us") ?? "en-us"
let idMatch = args[0], langMatch = "en-us"
if (args[0]?.match(/^\d+/)) {
idMatch = args[0]
langMatch = args[1]
} else {
idMatch = args[1]
langMatch = args[0]
}

for (const l of newsManager.getLanguages())
if (lang == newsManager.getLanguageName(l)) {
lang = l
break
}
const lang = this.getFuzzyLang(langMatch)

if (!idMatch) {
const stored = newsManager
.getNews(lang)
.map(art => `[\`${art.post_id}\`](${art.lang == "bbs-zh-cn" ? `https://bbs.mihoyo.com/ys/article/${art.post_id}` : `https://www.hoyolab.com/article/${art.post_id}`}): ${art.subject}`)
Expand All @@ -47,12 +48,29 @@ Supported languages: ${client.newsManager.getLanguages().map(l => `\`${l}\``).jo
return sendMessage(message, embed)
}

const post = newsManager.getNewsById(args[0])
const post = newsManager.getNewsByIdLang(idMatch, lang)
if (!post)
return sendMessage(message, `Couldn't find article in cache. Try to see if it exists on the forum: <https://www.hoyolab.com/article/${args[0]}>`)

await simplePaginator(message, (relativePage, currentPage, maxPages) => getNewsEmbed(post, relativePage, currentPage, maxPages), parseNewsContent(post.content).length)

return undefined
}

getFuzzyLang(search: string): NewsLang {
const { newsManager } = client

let lang = findFuzzy([
...newsManager.getLanguages(),
...newsManager.getLanguages().map(l => newsManager.getLanguageName(l))
], search ?? "en-us") ?? "en-us"

for (const l of newsManager.getLanguages())
if (lang == newsManager.getLanguageName(l)) {
lang = l
break
}

return lang as NewsLang
}
}
17 changes: 9 additions & 8 deletions src/utils/NewsManager.ts
Expand Up @@ -61,7 +61,7 @@ export default class NewsManager {
this.sql.exec("CREATE INDEX IF NOT EXISTS news_created_at ON news (created_at)")

this.addNewsStatement = this.sql.prepare("INSERT OR REPLACE INTO news VALUES (@post_id, @lang, @type, @subject, @created_at, @nickname, @image_url, @content)")
this.getNewsByIdStatement = this.sql.prepare("SELECT * FROM news WHERE post_id = @post_id")
this.getNewsByIdLangStatement = this.sql.prepare("SELECT * FROM news WHERE post_id = @post_id AND lang = @lang")
this.getNewsStatement = this.sql.prepare("SELECT * FROM news WHERE lang = @lang ORDER BY created_at DESC, post_id DESC LIMIT 20")
}

Expand Down Expand Up @@ -111,14 +111,14 @@ export default class NewsManager {
const articles: News[] = data.data.list
for (const article of articles.reverse()) {
const post_id = article.post.post_id
if (this.getNewsById(post_id)) continue
if (this.getNewsByIdLang(post_id, langid)) continue

Logger.info(`Fetching new post: ${language} ${post_id} - ${article.post.subject}`)
let fetched
if (langid == "bbs-zh-cn")
fetched = await fetch(`https://bbs-api.mihoyo.com/post/wapi/getPostFull?gids=2&post_id=${post_id}&read=1`, { headers: { Referer: "https://bbs.mihoyo.com/" }, timeout: 29000 })
fetched = await fetch(`https://bbs-api.mihoyo.com/post/wapi/getPostFull?gids=2&post_id=${post_id}&read=1`, { headers: { "x-rpc-language": langid, Referer: "https://bbs.mihoyo.com/" }, timeout: 29000 })
else
fetched = await fetch(`https://bbs-api-os.hoyolab.com/community/post/wapi/getPostFull?gids=2&post_id=${post_id}&read=1`, { headers: { Referer: "https://www.hoyolab.com/" }, timeout: 29000 })
fetched = await fetch(`https://bbs-api-os.hoyolab.com/community/post/wapi/getPostFull?gids=2&post_id=${post_id}&read=1`, { headers: { "x-rpc-language": langid, Referer: "https://bbs.mihoyo.com/" }, timeout: 29000 })

const postdata = await fetched.json()
this.lastFetched = Date.now()
Expand Down Expand Up @@ -167,10 +167,11 @@ export default class NewsManager {
})
}

private getNewsByIdStatement: SQLite.Statement
getNewsById(post_id: string): StoredNews {
return this.getNewsByIdStatement.get({
post_id
private getNewsByIdLangStatement: SQLite.Statement
getNewsByIdLang(post_id: string, lang: NewsLang): StoredNews {
return this.getNewsByIdLangStatement.get({
post_id,
lang
})
}

Expand Down

0 comments on commit 3e8c530

Please sign in to comment.