113 lines
3.0 KiB
TypeScript
113 lines
3.0 KiB
TypeScript
import { Mwn } from 'npm:mwn'
|
|
import process from "node:process";
|
|
|
|
async function getWorkshopVideos(bot: Mwn): Promise<string[]> {
|
|
const response = await bot.request({
|
|
action: 'query',
|
|
list: 'allimages',
|
|
aiprefix: 'WCS ', // Search prefix
|
|
ailimit: 'max', // Maximum number of results
|
|
});
|
|
|
|
if (response.query === undefined) {
|
|
throw new Error("Did not receive response to file query")
|
|
}
|
|
|
|
// Extract and print the file titles
|
|
return response.query.allimages.map(image => image.title);
|
|
}
|
|
|
|
interface VideoDescription {
|
|
teachers: string[]
|
|
location: string
|
|
event: string
|
|
level: string
|
|
date: string
|
|
patterns: string
|
|
notes: string
|
|
}
|
|
|
|
function parseTemplate(description: string): Record<string, string> {
|
|
|
|
const templateRegex = /{{(.*DanceWorkshopDescription.*)}}/s;
|
|
|
|
const match = description.match(templateRegex);
|
|
|
|
if (!match) {
|
|
throw new Error("Could not find template")
|
|
}
|
|
|
|
const propertyRegex = /\|\s*(?<key>[^|]*?)\s*=(?<value>[^|]*?)\s*$/gm
|
|
const properties = match[1].matchAll(propertyRegex).map(v => ({ [v.groups['key']]: v.groups['value'] }))
|
|
return properties
|
|
}
|
|
|
|
function parseSummary(description: string, name: string): VideoDescription {
|
|
const properties = parseTemplate(description)
|
|
|
|
const b: Partial<VideoDescription> = {};
|
|
|
|
if (Object.hasOwn(properties, 'teachers')) {
|
|
console.warn(`Page ${name} has old template usage [contains 'teachers' key]`)
|
|
const t = properties['teachers'].split('&').map(item => item.trim());
|
|
b.teachers = t
|
|
} else {
|
|
b.teachers = [properties['leader'], properties['follower']].filter(v => v !== undefined)
|
|
}
|
|
|
|
b.location = properties['location']
|
|
b.date = properties['date']
|
|
b.level = properties['level']
|
|
|
|
b.event = properties['event'] || b.location
|
|
|
|
if (!Object.hasOwn(properties, 'patterns')) {
|
|
console.warn(`Page ${name} has old template usage [no 'patterns' key]`)
|
|
const patternsRegex = /===\s*Shown Patterns\s*===\s*(?<patterns>(^\s*\*+.*$\s+)+)/gm
|
|
const match = description.match(patternsRegex)
|
|
if (!match) {
|
|
throw new Error("Could not find \"Shown Patterns\"")
|
|
}
|
|
b.patterns = match.groups['patterns']
|
|
}
|
|
if (!Object.hasOwn(properties, 'notes')) {
|
|
console.warn(`Page ${name} has old template usage [no 'patterns' key]`)
|
|
const notesRegex = /===\s*Notes\s*===\s*(?<notes>(^\s*:+.*$\s+)+)/gm
|
|
const match = description.match(notesRegex)
|
|
if (!match) {
|
|
throw new Error("Could not find \"Notes\"")
|
|
}
|
|
b.notes = match.groups['notes']
|
|
}
|
|
|
|
return b
|
|
}
|
|
|
|
|
|
async function main() {
|
|
const bot = new Mwn({
|
|
apiUrl: 'https://dancing.thasky.one/api.php',
|
|
username: process.env.BOTNAME,
|
|
|
|
password: process.env.BOTPW,
|
|
|
|
userAgent: 'mwn bot',
|
|
|
|
});
|
|
|
|
try {
|
|
await bot.login();
|
|
const relevantFiles = await getWorkshopVideos(bot)
|
|
const file_content = await bot.read(relevantFiles[0])
|
|
const content = file_content.revisions[0].content
|
|
console.log(content)
|
|
parseSummary(content, relevantFiles[0])
|
|
} catch (error) {
|
|
console.error('Error:', error);
|
|
} finally {
|
|
await bot.logout();
|
|
}
|
|
}
|
|
|
|
main();
|