-
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.
Browse files
Browse the repository at this point in the history
* [#47] Add support for parsing audio files in news posts * Rename image utils to file utils
- Loading branch information
1 parent
4185a05
commit c7dee3c
Showing
14 changed files
with
194 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"osrs-web-scraper": minor | ||
--- | ||
|
||
Add support for parsing audio files in news posts |
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,39 @@ | ||
import fs from "fs"; | ||
import { HTMLElement } from "node-html-parser"; | ||
|
||
import { | ||
downloadFile, | ||
formatFileName, | ||
getFileExtension, | ||
} from "../../../../../utils/file"; | ||
import { | ||
ListenTemplate, | ||
MediaWikiComment, | ||
} from "../../../../../utils/mediawiki"; | ||
import { ContentNodeParser } from "../types"; | ||
|
||
export const audioParser: ContentNodeParser = (node, { title }) => { | ||
if (node instanceof HTMLElement && node.firstChild instanceof HTMLElement) { | ||
const source = node.firstChild as HTMLElement; | ||
const audioLink = source.attributes.src; | ||
|
||
const formattedTitle = formatFileName(title as string); | ||
const audioDirectory = `./out/news/${formattedTitle}`; | ||
if (!fs.existsSync(audioDirectory)) { | ||
fs.mkdirSync(audioDirectory, { recursive: true }); | ||
} | ||
|
||
const audioExtension = getFileExtension(audioLink); | ||
const outputFileName = `${formattedTitle} narration.${audioExtension}`; | ||
|
||
downloadFile(audioLink, `${audioDirectory}/${outputFileName}`); | ||
|
||
return new ListenTemplate(outputFileName, { | ||
align: "center", | ||
title: "Audio reading", | ||
}).build(); | ||
} | ||
return new MediaWikiComment("Invalid audio node"); | ||
}; | ||
|
||
export default audioParser; |
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
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
65 changes: 65 additions & 0 deletions
65
src/utils/mediawiki/contents/templates/__tests__/__snapshots__/listen.test.ts.snap
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,65 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`ListenTemplate it should render with only a file 1`] = ` | ||
MediaWikiTemplate { | ||
"name": "Listen", | ||
"params": Array [ | ||
Object { | ||
"key": "filename", | ||
"value": "test.mp3", | ||
}, | ||
], | ||
} | ||
`; | ||
|
||
exports[`ListenTemplate it should render with options: {"align":"left","title":"test title"} 1`] = ` | ||
MediaWikiTemplate { | ||
"name": "Listen", | ||
"params": Array [ | ||
Object { | ||
"key": "filename", | ||
"value": "test.mp3", | ||
}, | ||
Object { | ||
"key": "align", | ||
"value": "left", | ||
}, | ||
Object { | ||
"key": "title", | ||
"value": "test title", | ||
}, | ||
], | ||
} | ||
`; | ||
|
||
exports[`ListenTemplate it should render with options: {"align":"left"} 1`] = ` | ||
MediaWikiTemplate { | ||
"name": "Listen", | ||
"params": Array [ | ||
Object { | ||
"key": "filename", | ||
"value": "test.mp3", | ||
}, | ||
Object { | ||
"key": "align", | ||
"value": "left", | ||
}, | ||
], | ||
} | ||
`; | ||
|
||
exports[`ListenTemplate it should render with options: {"title":"test title"} 1`] = ` | ||
MediaWikiTemplate { | ||
"name": "Listen", | ||
"params": Array [ | ||
Object { | ||
"key": "filename", | ||
"value": "test.mp3", | ||
}, | ||
Object { | ||
"key": "title", | ||
"value": "test title", | ||
}, | ||
], | ||
} | ||
`; |
15 changes: 15 additions & 0 deletions
15
src/utils/mediawiki/contents/templates/__tests__/listen.test.ts
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,15 @@ | ||
import ListenTemplate, { ListeTemplateOptions } from "../listen"; | ||
|
||
describe("ListenTemplate", () => { | ||
test("it should render with only a file", () => { | ||
expect(new ListenTemplate("test.mp3").build()).toMatchSnapshot(); | ||
}); | ||
|
||
test.each<ListeTemplateOptions>([ | ||
{ align: "left" }, | ||
{ title: "test title" }, | ||
{ align: "left", title: "test title" }, | ||
])("it should render with options: %j", (options) => { | ||
expect(new ListenTemplate("test.mp3", options).build()).toMatchSnapshot(); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,8 +1,10 @@ | ||
export { default as CollapedSectionTemplate } from "./collapsedSection"; | ||
export { default as ListenTemplate } from "./listen"; | ||
export { default as NewsPollTemplate } from "./newsPoll"; | ||
export { default as PollTemplate } from "./poll"; | ||
export { default as PollNoticeTemplate } from "./pollNotice"; | ||
export { default as PollWrapperTemplate } from "./pollWrapper"; | ||
export { default as UpdateTemplate } from "./update"; | ||
|
||
export * from "./listen"; | ||
export * from "./poll"; |
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,36 @@ | ||
import { Template } from "./types"; | ||
import MediaWikiTemplate from "../template"; | ||
|
||
export type ListenAlignment = "left" | "right" | "center"; | ||
|
||
export type ListeTemplateOptions = { | ||
align?: ListenAlignment; | ||
title?: string; | ||
}; | ||
|
||
class ListenTemplate extends Template { | ||
align?: ListenAlignment; | ||
fileName: string; | ||
title?: string; | ||
|
||
constructor(fileName: string, options?: ListeTemplateOptions) { | ||
super("Listen"); | ||
this.fileName = fileName; | ||
this.align = options?.align; | ||
this.title = options?.title; | ||
} | ||
|
||
build() { | ||
const listenTemplate = new MediaWikiTemplate(this.name); | ||
listenTemplate.add("filename", this.fileName); | ||
if (this.align) { | ||
listenTemplate.add("align", this.align); | ||
} | ||
if (this.title) { | ||
listenTemplate.add("title", this.title); | ||
} | ||
return listenTemplate; | ||
} | ||
} | ||
|
||
export default ListenTemplate; |