Worked on parsing
This commit is contained in:
67
main.ts
67
main.ts
@@ -1,5 +1,6 @@
|
|||||||
import { Mwn } from 'npm:mwn'
|
import { Mwn } from 'npm:mwn'
|
||||||
import process from "node:process";
|
import process from "node:process";
|
||||||
|
import { parseSummary } from "./parse.ts";
|
||||||
|
|
||||||
async function getWorkshopVideos(bot: Mwn): Promise<string[]> {
|
async function getWorkshopVideos(bot: Mwn): Promise<string[]> {
|
||||||
const response = await bot.request({
|
const response = await bot.request({
|
||||||
@@ -14,10 +15,10 @@ async function getWorkshopVideos(bot: Mwn): Promise<string[]> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Extract and print the file titles
|
// 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[]
|
teachers: string[]
|
||||||
location: string
|
location: string
|
||||||
event: string
|
event: string
|
||||||
@@ -27,62 +28,6 @@ interface VideoDescription {
|
|||||||
notes: 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() {
|
async function main() {
|
||||||
const bot = new Mwn({
|
const bot = new Mwn({
|
||||||
@@ -99,7 +44,13 @@ async function main() {
|
|||||||
await bot.login();
|
await bot.login();
|
||||||
const relevantFiles = await getWorkshopVideos(bot)
|
const relevantFiles = await getWorkshopVideos(bot)
|
||||||
const file_content = await bot.read(relevantFiles[0])
|
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
|
const content = file_content.revisions[0].content
|
||||||
|
if (content === undefined) {
|
||||||
|
throw new Error("Latest revision has no content")
|
||||||
|
}
|
||||||
console.log(content)
|
console.log(content)
|
||||||
parseSummary(content, relevantFiles[0])
|
parseSummary(content, relevantFiles[0])
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
@@ -1,6 +0,0 @@
|
|||||||
import { assertEquals } from "@std/assert";
|
|
||||||
import { add } from "./main.ts";
|
|
||||||
|
|
||||||
Deno.test(function addTest() {
|
|
||||||
assertEquals(add(2, 3), 5);
|
|
||||||
});
|
|
||||||
67
parse.ts
Normal file
67
parse.ts
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
import { VideoDescription } from "./main.ts";
|
||||||
|
|
||||||
|
export 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 => {
|
||||||
|
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<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+)+)/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*(?<notes>(^\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)
|
||||||
|
}
|
||||||
25
parse_test.ts
Normal file
25
parse_test.ts
Normal file
@@ -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")
|
||||||
|
});
|
||||||
15
test/new_template.mw
Normal file
15
test/new_template.mw
Normal file
@@ -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
|
||||||
|
}}
|
||||||
18
test/old_template.mw
Normal file
18
test/old_template.mw
Normal file
@@ -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]]
|
||||||
Reference in New Issue
Block a user