-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from Mtillmann/psc-etc
adds Podlove XML and JSON and mp4chaps and improves testing
- Loading branch information
Showing
13 changed files
with
322 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { FormatBase } from "./FormatBase.js"; | ||
import { secondsToTimestamp, timestampToSeconds } from "../util.js"; | ||
|
||
export class MP4Chaps extends FormatBase { | ||
|
||
filename = 'mp4chaps.txt'; | ||
mimeType = 'text/plain'; | ||
|
||
detect(inputString) { | ||
return /^\d\d:\d\d:\d\d.\d\d?\d?\s/.test(inputString.trim()); | ||
} | ||
|
||
parse(string) { | ||
if (!this.detect(string)) { | ||
throw new Error('MP4Chaps *MUST* begin with 00:00:00, received: ' + string.substr(0, 10) + '...'); | ||
} | ||
this.chapters = this.stringToLines(string).map(line => { | ||
line = line.split(' '); | ||
const startTime = timestampToSeconds(line.shift(line)); | ||
const [title, href] = line.join(' ').split('<'); | ||
const chapter = { | ||
startTime, | ||
title : title.trim() | ||
} | ||
|
||
if(href){ | ||
chapter.href = href.replace('>', ''); | ||
} | ||
|
||
return chapter; | ||
}); | ||
} | ||
|
||
toString(){ | ||
return this.chapters.map((chapter) => { | ||
const line = []; | ||
line.push(secondsToTimestamp(chapter.startTime, {milliseconds: true})); | ||
line.push(chapter.title); | ||
if(chapter.href){ | ||
line.push(`<${chapter.href}>`); | ||
} | ||
return line.join(' '); | ||
}).join("\n"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { secondsToTimestamp, timestampToSeconds } from '../util.js'; | ||
import { FormatBase } from './FormatBase.js'; | ||
|
||
|
||
export class PodloveJson extends FormatBase{ | ||
|
||
filename = 'podlove-chapters.json'; | ||
mimeType = 'application/json'; | ||
|
||
detect(inputString) { | ||
try { | ||
const data = JSON.parse(inputString); | ||
const { errors } = this.test(data); | ||
if (errors.length > 0) { | ||
throw new Error('data test failed'); | ||
} | ||
} catch (e) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
test(data) { | ||
if (!Array.isArray(data)) { | ||
return { errors: ['JSON Structure: must be an array'] }; | ||
} | ||
|
||
if (data.length === 0) { | ||
return { errors: ['JSON Structure: must not be empty'] }; | ||
} | ||
|
||
if(!data.every(chapter => 'start' in chapter)){ | ||
return { errors: ['JSON Structure: every chapter must have a start property'] }; | ||
} | ||
|
||
return { errors: [] }; | ||
} | ||
|
||
|
||
parse(string) { | ||
const data = JSON.parse(string); | ||
const {errors} = this.test(data); | ||
if (errors.length > 0) { | ||
throw new Error(errors.join('')); | ||
} | ||
|
||
this.chapters = data.map(raw => { | ||
const {start, title, image, href} = raw; | ||
const chapter = { | ||
startTime: timestampToSeconds(start) | ||
} | ||
if(title){ | ||
chapter.title = title; | ||
} | ||
if(image){ | ||
chapter.img = image; | ||
} | ||
if(href){ | ||
chapter.href = href; | ||
} | ||
return chapter; | ||
}); | ||
} | ||
|
||
toString(pretty = false){ | ||
return JSON.stringify(this.chapters.map(chapter => ({ | ||
start: secondsToTimestamp(chapter.startTime, {milliseconds: true}), | ||
title: chapter.title || '', | ||
image: chapter.img || '', | ||
href: chapter.href || '' | ||
})), null, pretty ? 2 : 0); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.