Worked on parsing
This commit is contained in:
95
main.ts
95
main.ts
@@ -15,47 +15,98 @@ async function getWorkshopVideos(bot: Mwn): Promise<string[]> {
|
||||
|
||||
// Extract and print the file titles
|
||||
return response.query.allimages.map(image => image.title);
|
||||
|
||||
|
||||
console.log('Files starting with "ABC ":', files);
|
||||
const file_content = await bot.read(files[0])
|
||||
console.dir(file_content.revisions[0].content)
|
||||
console.dir(await bot.parseTitle(files[0]))
|
||||
}
|
||||
|
||||
interface VideoDescription {
|
||||
teachers: string[]
|
||||
location: string
|
||||
event: string
|
||||
level: string
|
||||
date: string
|
||||
patterns: string
|
||||
notes: string
|
||||
}
|
||||
|
||||
function parseSummary(description: string): VideoDescription {
|
||||
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,
|
||||
async function main() {
|
||||
const bot = new Mwn({
|
||||
apiUrl: 'https://dancing.thasky.one/api.php',
|
||||
username: process.env.BOTNAME,
|
||||
|
||||
password: process.env.BOTPW,
|
||||
password: process.env.BOTPW,
|
||||
|
||||
userAgent: 'mwn bot',
|
||||
userAgent: 'mwn bot',
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
try {
|
||||
await bot.login();
|
||||
const relevantFiles = await getWorkshopVideos(bot)
|
||||
} catch (error) {
|
||||
console.error('Error:', error);
|
||||
} finally {
|
||||
await bot.logout();
|
||||
}
|
||||
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();
|
||||
|
||||
Reference in New Issue
Block a user