Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for Calibre Recipes #7

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/Modules/ConversionModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@ import { ConverterContract } from "@/Protocols/ConverterProtocol"

import RSSConverterTool from "@/Tools/Converters/RSSConverterTool"
import MangaConverterTool from "@/Tools/Converters/MangaConverterTool"
import RecipeConverterTool from "@/Tools/Converters/RecipeConverterTool"

class ConversionModule {
async convert (content: Content<unknown>): Promise<DocumentModel[]> {
async convert(content: Content<unknown>): Promise<DocumentModel[]> {
const converter = this.getConverterBySourceConfig(content.sourceConfig)

return await converter.convert(content)
}

private getConverterBySourceConfig (sourceConfig: SourceConfig): ConverterContract<unknown> {
private getConverterBySourceConfig(sourceConfig: SourceConfig): ConverterContract<unknown> {
const converterMap: Record<SourceConfig["type"], ConverterContract<unknown>> = {
rss: RSSConverterTool,
manga: MangaConverterTool
manga: MangaConverterTool,
recipe: RecipeConverterTool
}

return converterMap[sourceConfig.type]
Expand Down
8 changes: 5 additions & 3 deletions src/Modules/ImportationModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@ import { SourceConfig } from "@/Protocols/SetupInputProtocol"

import RSSImporterTool from "@/Tools/Importers/RSSImporterTool"
import MangaImporterTool from "@/Tools/Importers/MangaImporterTool"
import RecipeImporterTool from "@/Tools/Importers/RecipeImporterTool"

class ImportationModule {
async import (sourceConfig: SourceConfig): Promise<Content<unknown>> {
async import(sourceConfig: SourceConfig): Promise<Content<unknown>> {
const importer = this.getImporterBySourceConfig(sourceConfig)

return await importer.import(sourceConfig)
}

private getImporterBySourceConfig (sourceConfig: SourceConfig): ImporterContract<unknown> {
private getImporterBySourceConfig(sourceConfig: SourceConfig): ImporterContract<unknown> {
const importerMap: Record<SourceConfig["type"], ImporterContract<unknown>> = {
rss: RSSImporterTool,
manga: MangaImporterTool
manga: MangaImporterTool,
recipe: RecipeImporterTool,
}

return importerMap[sourceConfig.type]
Expand Down
2 changes: 1 addition & 1 deletion src/Protocols/SetupInputProtocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export type SourceConfig = {
* Every RSS post will be a new document inside your kindle.
*/
splitRSSPosts?: boolean
type: "rss" | "manga"
type: "rss" | "manga" | "recipe"
}

export type KindleConfig = {
Expand Down
77 changes: 77 additions & 0 deletions src/Tools/Converters/RecipeConverterTool.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import fs from "fs"

import { DocumentModel } from "@/Models/DocumentModel"

import { ConverterContract } from "@/Protocols/ConverterProtocol"
import { Content } from "@/Protocols/ImporterProtocol"
import { GenerateEPUBOptions, EpubContent } from "@/Protocols/EbookGeneratorProtocol"
import { SourceConfig } from "@/Protocols/SetupInputProtocol"


import EbookGeneratorService from "@/Services/EbookGeneratorService"
import QueueService from "@/Services/QueueService"

import FileUtil from "@/Utils/FileUtil"
import DateUtil from "@/Utils/DateUtil"

class RecipeConverterTool implements ConverterContract<Buffer> {
private readonly queueService = new QueueService({ concurrency: 10 })
private readonly ebookGeneratorService = new EbookGeneratorService()

async convert(content: Content<Buffer>): Promise<DocumentModel[]> {
const EPUBConfigs = await this.RecipeToEPUBConfig(content.sourceConfig)
const epubFilePath = content.sourceConfig.name
const documents: DocumentModel[] = await Promise.all(
EPUBConfigs.map(async EPUBConfig => (
await this.queueService.enqueue(async () => {

const mobiFilePath = await this.RecipeToMOBI(epubFilePath)

const { filename } = FileUtil.parseFilePath(mobiFilePath)

const mobiData = fs.createReadStream(mobiFilePath)

return new DocumentModel({
title: EPUBConfig.title,
filename,
data: mobiData,
type: content.sourceConfig.type
})
})
))
)

return documents
}

private async RecipeToEPUBConfig(sourceConfig: SourceConfig): Promise<GenerateEPUBOptions[]> {
const metadataTitle = sourceConfig.name
const metadataSubTitle = DateUtil.todayFormattedDate
const content_data: EpubContent = {
title: "",
author: "",
data: ""
}

return [{
title: `${metadataTitle} ${metadataSubTitle}`,
author: null,
publisher: null,
cover: null,
content: [content_data],
metadata: {
title: metadataTitle,
subTitle: metadataSubTitle
}
}]

}

private async RecipeToMOBI(epubFilePath: string): Promise<string> {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kindle now supports ePub, and Mobi is no longer supported. As a result, the conversion to Mobi is no longer necessary.

const recipe_name = `${epubFilePath}.recipe`
const mobiFilePath = await this.ebookGeneratorService.generateMOBIFromEPUB(recipe_name)
return mobiFilePath
}
}

export default new RecipeConverterTool()
16 changes: 16 additions & 0 deletions src/Tools/Importers/RecipeImporterTool.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Content, ImporterContract } from "@/Protocols/ImporterProtocol"
import { SourceConfig } from "@/Protocols/SetupInputProtocol"


class RecipeImporterTool implements ImporterContract<Buffer> {

async import(sourceConfig: SourceConfig): Promise<Content<Buffer>> {

return {
data: null,
sourceConfig
}
}
}

export default new RecipeImporterTool()
Loading