-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
133 additions
and
1 deletion.
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
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,41 @@ | ||
import { Job, Queue, Worker } from 'bullmq'; | ||
import { runIntegrations } from '../utils/integration'; | ||
|
||
export const integrationWorker = new Worker( | ||
'integrationQueue', | ||
async (job: Job) => { | ||
try { | ||
await runIntegrations(); | ||
await job.updateProgress(100); | ||
} catch (err) { | ||
await job.log(`${err}`); | ||
throw err; | ||
} | ||
}, | ||
{ | ||
connection: { | ||
host: process.env.REDIS_HOST, | ||
port: parseInt(process.env.REDIS_PORT || '6379', 10), | ||
}, | ||
concurrency: 30, | ||
limiter: { | ||
max: 30, | ||
duration: 1000 * 2, | ||
}, | ||
}, | ||
); | ||
|
||
export const integrationQueue = new Queue('integrationQueue', { | ||
connection: { | ||
host: process.env.REDIS_HOST, | ||
port: parseInt(process.env.REDIS_PORT || '6379', 10), | ||
}, | ||
defaultJobOptions: { | ||
removeOnComplete: true, | ||
attempts: 20, | ||
backoff: { | ||
type: 'fixed', | ||
delay: 1000 * 60 * 2, | ||
}, | ||
}, | ||
}); |
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,81 @@ | ||
import { sanitizer } from '../../utils'; | ||
import { prisma } from '../db/client'; | ||
|
||
interface Library { | ||
id: string; | ||
} | ||
|
||
interface Series { | ||
content: SeriesContent[]; | ||
} | ||
|
||
interface SeriesContent { | ||
id: string; | ||
name: string; | ||
} | ||
|
||
export const scanLibrary = async () => { | ||
const settings = await prisma.settings.findFirstOrThrow(); | ||
|
||
if (settings.komgaEnabled && settings.komgaHost && settings.komgaUser && settings.komgaPassword) { | ||
const baseKomgaUrl = settings.komgaHost.toLowerCase().startsWith('http') | ||
? settings.komgaHost | ||
: `http://${settings.komgaHost}`; | ||
const headers = { | ||
Authorization: `Basic ${Buffer.from(`${settings.komgaUser}:${settings.komgaPassword}`).toString('base64')}`, | ||
}; | ||
const komgaLibrariesUrl = new URL('/api/v1/libraries', baseKomgaUrl).href; | ||
|
||
const libraries: Library[] = await ( | ||
await fetch(komgaLibrariesUrl, { | ||
headers, | ||
}) | ||
).json(); | ||
|
||
await Promise.all( | ||
libraries.map(async (library) => { | ||
const komgaLibraryUrl = new URL(`/api/v1/libraries/${library.id}/scan`, baseKomgaUrl).href; | ||
await fetch(komgaLibraryUrl, { | ||
method: 'POST', | ||
headers, | ||
}); | ||
}), | ||
); | ||
} | ||
}; | ||
|
||
export const refreshMetadata = async (mangaName: string) => { | ||
const settings = await prisma.settings.findFirstOrThrow(); | ||
|
||
if (settings.komgaEnabled && settings.komgaHost && settings.komgaUser && settings.komgaPassword) { | ||
const baseKomgaUrl = settings.komgaHost.toLowerCase().startsWith('http') | ||
? settings.komgaHost | ||
: `http://${settings.komgaHost}`; | ||
const headers = { | ||
Authorization: `Basic ${Buffer.from(`${settings.komgaUser}:${settings.komgaPassword}`).toString('base64')}`, | ||
}; | ||
const komgaSeriesUrl = new URL('/api/v1/series?size=1000', baseKomgaUrl).href; | ||
|
||
const series: Series = await ( | ||
await fetch(komgaSeriesUrl, { | ||
headers, | ||
}) | ||
).json(); | ||
|
||
const content = series.content.find((c) => c.name === sanitizer(mangaName)); | ||
|
||
if (!content) { | ||
return; | ||
} | ||
|
||
const komgaSeriesRefreshUrl = new URL(`/api/v1/series/${content.id}/metadata/refresh`, baseKomgaUrl).href; | ||
await fetch(komgaSeriesRefreshUrl, { | ||
method: 'POST', | ||
headers, | ||
}); | ||
} | ||
}; | ||
|
||
export const runIntegrations = async () => { | ||
await scanLibrary(); | ||
}; |