From 6fc14bb5952041c691a97486e08135bd917852fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20W=C3=B6lfer?= Date: Tue, 6 May 2025 01:46:50 +0200 Subject: [PATCH] Worked on parsing --- main.ts | 67 ++++++-------------------------------------- main_test.ts | 6 ---- parse.ts | 67 ++++++++++++++++++++++++++++++++++++++++++++ parse_test.ts | 25 +++++++++++++++++ test/new_template.mw | 15 ++++++++++ test/old_template.mw | 18 ++++++++++++ 6 files changed, 134 insertions(+), 64 deletions(-) delete mode 100644 main_test.ts create mode 100644 parse.ts create mode 100644 parse_test.ts create mode 100644 test/new_template.mw create mode 100644 test/old_template.mw diff --git a/main.ts b/main.ts index d6f1c8d..46a3608 100644 --- a/main.ts +++ b/main.ts @@ -1,5 +1,6 @@ import { Mwn } from 'npm:mwn' import process from "node:process"; +import { parseSummary } from "./parse.ts"; async function getWorkshopVideos(bot: Mwn): Promise { const response = await bot.request({ @@ -14,10 +15,10 @@ async function getWorkshopVideos(bot: Mwn): Promise { } // Extract and print the file titles - return response.query.allimages.map(image => image.title); + return response.query.allimages.map(function (image: { title: string }) { return image.title }); } -interface VideoDescription { +export interface VideoDescription { teachers: string[] location: string event: string @@ -27,62 +28,6 @@ interface VideoDescription { notes: string } -function parseTemplate(description: string): Record { - - const templateRegex = /{{(.*DanceWorkshopDescription.*)}}/s; - - const match = description.match(templateRegex); - - if (!match) { - throw new Error("Could not find template") - } - - const propertyRegex = /\|\s*(?[^|]*?)\s*=(?[^|]*?)\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 = {}; - - 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*(?(^\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*(?(^\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({ @@ -99,7 +44,13 @@ async function main() { await bot.login(); const relevantFiles = await getWorkshopVideos(bot) const file_content = await bot.read(relevantFiles[0]) + if (file_content.revisions === undefined) { + throw new Error("Page has no revisions") + } const content = file_content.revisions[0].content + if (content === undefined) { + throw new Error("Latest revision has no content") + } console.log(content) parseSummary(content, relevantFiles[0]) } catch (error) { diff --git a/main_test.ts b/main_test.ts deleted file mode 100644 index 3d981e9..0000000 --- a/main_test.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { assertEquals } from "@std/assert"; -import { add } from "./main.ts"; - -Deno.test(function addTest() { - assertEquals(add(2, 3), 5); -}); diff --git a/parse.ts b/parse.ts new file mode 100644 index 0000000..a73ae62 --- /dev/null +++ b/parse.ts @@ -0,0 +1,67 @@ +import { VideoDescription } from "./main.ts"; + +export function parseTemplate(description: string): Record { + + const templateRegex = /{{(.*DanceWorkshopDescription.*)}}/s; + + const match = description.match(templateRegex); + + if (!match) { + throw new Error("Could not find template") + } + + const propertyRegex = /\|\s*(?[^|]*?)\s*=(?[^|]*?)\s*$/gm + + const properties = match[1].matchAll(propertyRegex).map(v => { + if (!v.groups) { throw new Error("Faulty regex") }; + return ({ [v.groups['key']]: v.groups['value'] }) + }) + return Object.assign({}, ...Array.from(properties)) +} + +export function parseSummary(description: string, name: string): VideoDescription { + const properties = parseTemplate(description) + + const b: Partial = {}; + + 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*(?(^\s*[:*]+.*$\s+)+)/m + const match = description.match(patternsRegex) + if (!match) { + throw new Error("Could not find \"Shown Patterns\"") + } + if (!match.groups) { + throw new Error("Faulty regex") + } + b.patterns = match.groups['patterns'] + } + if (!Object.hasOwn(properties, 'notes')) { + console.warn(`Page ${name} has old template usage [no 'notes' key]`) + const notesRegex = /===\s*Notes\s*===\s*(?(^\s*[:*]+.*$\s+)+)/m + const match = description.match(notesRegex) + if (!match) { + throw new Error("Could not find \"Notes\"") + } + if (!match.groups) { + throw new Error("Faulty regex") + } + b.notes = match.groups['notes'] + } + + return (b as VideoDescription) +} diff --git a/parse_test.ts b/parse_test.ts new file mode 100644 index 0000000..5d59388 --- /dev/null +++ b/parse_test.ts @@ -0,0 +1,25 @@ +import { assertArrayIncludes, assertEquals, assertStringIncludes } from "@std/assert"; +import { parseSummary } from "./parse.ts"; + +Deno.test(async function oldTemplate() { + const filePath = './test/old_template.mw'; + + const fileContents = await Deno.readTextFile(filePath); + + const desc = parseSummary(fileContents, "Old Template"); + assertEquals(desc.date, "2024-12-06") + assertArrayIncludes(desc.teachers, ["Sebas"]) + assertStringIncludes(desc.patterns, "Sugar Tuck with Caress") +}); +Deno.test(async function newTemplate() { + const filePath = './test/new_template.mw'; + + const fileContents = await Deno.readTextFile(filePath); + + const desc = parseSummary(fileContents, "New Template"); + + assertEquals(desc.date, "2023-12-02") + assertArrayIncludes(desc.teachers, ["Sebastian Spindler"]) + console.dir(desc) + assertStringIncludes(desc.patterns, "Blocking Free Spin") +}); diff --git a/test/new_template.mw b/test/new_template.mw new file mode 100644 index 0000000..c594657 --- /dev/null +++ b/test/new_template.mw @@ -0,0 +1,15 @@ +{{ DanceWorkshopDescription +| date=2023-12-02 +| event=DanceFusion +| location=Aachen +| level=Fortgeschritten +| leader=Sebastian Spindler +| follower=Viola Löhndorf +| patterns= +* Sugar push with change of hands +* Blocking Free Spin +* Underarm Turn +| notes= +* Hit is a sudden accent after an acceleration +* Melt is a slow movement after a hit +}} diff --git a/test/old_template.mw b/test/old_template.mw new file mode 100644 index 0000000..ec5b5a1 --- /dev/null +++ b/test/old_template.mw @@ -0,0 +1,18 @@ +== Summary == +{{ DanceWorkshopDescription +| date=2024-12-06 +| location=DanceFusion Aachen +| level=Fortgeschritten +| teachers=Sebas & ?? +}} + +=== Shown Patterns === +* Sugar Push (Right-to-Right/Left-to-Left) with other hand-pair styling +* Sugar Tuck with Caress +** Finish with Neck/Shoulder-lead turn + +=== Notes === +: Take followers hand like carrying a pizza +: Move hand behind their head (or your head), let go + +[[Category:Integration Needed]]