Skip to content

Commit b060ddb

Browse files
committed
add: implement WikipediaMeaning class
1 parent a69d8ad commit b060ddb

File tree

4 files changed

+80
-1
lines changed

4 files changed

+80
-1
lines changed

config.default.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,9 @@ export default {
401401
meanings: {
402402
letrasmus: {
403403
enabled: true
404+
},
405+
wikipedia: {
406+
enabled: true
404407
}
405408
},
406409
audio: {

src/managers/meaningManager.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,11 @@ export default class MeaningManager {
105105
}
106106
}
107107

108-
for (const [name, source] of this.meaningSources) {
108+
const sortedSources = Array.from(this.meaningSources.entries()).sort(
109+
(a, b) => (b[1].priority || 0) - (a[1].priority || 0)
110+
)
111+
112+
for (const [name, source] of sortedSources) {
109113
if (name !== sourceName) {
110114
const meaning = await source.getMeaning(trackInfo, language)
111115
if (meaning && meaning.loadType !== 'empty') {

src/meanings/letrasmus.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ const searchLetras = async (query, limit = 10) => {
231231
export default class LetrasMusMeaning {
232232
constructor(nodelink) {
233233
this.nodelink = nodelink
234+
this.priority = 70
234235
}
235236

236237
async setup() {

src/meanings/wikipedia.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { logger, makeRequest } from '../utils.js'
2+
3+
export default class WikipediaMeaning {
4+
constructor(nodelink) {
5+
this.nodelink = nodelink
6+
this.priority = 90
7+
}
8+
9+
async setup() {
10+
return true
11+
}
12+
13+
_cleanText(text) {
14+
if (!text) return ''
15+
return text.replace(/<!--[\s\S]*?-->/g, '').trim()
16+
}
17+
18+
async getMeaning(trackInfo, language) {
19+
const lang = language || 'en'
20+
const queries = []
21+
22+
if (trackInfo.title) {
23+
queries.push({ type: 'track', query: `${trackInfo.title} (song)` })
24+
queries.push({ type: 'track', query: trackInfo.title })
25+
}
26+
27+
if (trackInfo.author) {
28+
queries.push({ type: 'artist', query: trackInfo.author })
29+
}
30+
31+
for (const item of queries) {
32+
const { type, query } = item
33+
const encodedQuery = encodeURIComponent(query)
34+
const url = `https://${lang}.wikipedia.org/w/api.php?action=query&format=json&prop=extracts|description&titles=${encodedQuery}&redirects=1&explaintext=1`
35+
36+
try {
37+
const { body, statusCode } = await makeRequest(url, { method: 'GET' })
38+
39+
if (statusCode !== 200 || !body || !body.query || !body.query.pages) {
40+
continue
41+
}
42+
43+
const pages = body.query.pages
44+
const pageId = Object.keys(pages)[0]
45+
46+
if (pageId === '-1') continue
47+
48+
const page = pages[pageId]
49+
const extract = this._cleanText(page.extract)
50+
51+
if (extract && extract.length > 0 && extract !== '\n') {
52+
return {
53+
loadType: 'meaning',
54+
data: {
55+
title: page.title,
56+
description: page.description || null,
57+
paragraphs: extract.split('\n').filter((line) => line.trim().length > 0),
58+
url: `https://${lang}.wikipedia.org/wiki/${encodeURIComponent(page.title.replace(/ /g, '_'))}`,
59+
provider: 'wikipedia',
60+
type: type
61+
}
62+
}
63+
}
64+
} catch (e) {
65+
logger('debug', 'WikipediaMeaning', `Failed to fetch for query "${query}": ${e.message}`)
66+
}
67+
}
68+
69+
return { loadType: 'empty', data: {} }
70+
}
71+
}

0 commit comments

Comments
 (0)