63 lines
2.4 KiB
TypeScript
63 lines
2.4 KiB
TypeScript
import { injectable } from 'inversify';
|
|
import { Service } from '../../core/container';
|
|
import { Database, getConnection } from '../../core/database';
|
|
import { inject } from '../../core/inject';
|
|
import { Copy } from '../../entities/library/copy';
|
|
import { Language } from '../../entities/library/language';
|
|
import { Source } from '../../entities/library/source';
|
|
import { Work } from '../../entities/library/work';
|
|
import { dateObjectToString } from '../date/date-util';
|
|
import type { SourceGetterInterface } from '../source/source-getter-interface';
|
|
import { isNhentaiRealLanguage, languageToLangCode, NhentaiRealLanguage } from './nhentai-util';
|
|
|
|
async function getLanguage(nhentaiLanguageIdentifier: NhentaiRealLanguage): Promise<LanguageEntityInterface> {
|
|
const { manager } = await getConnection(Database.LIBRARY);
|
|
return manager.getRepository(Language).findOneOrFail(languageToLangCode[nhentaiLanguageIdentifier]);
|
|
}
|
|
|
|
@injectable()
|
|
export class NhentaiSourceGetter implements SourceGetterInterface {
|
|
private nhentaiApi: NhentaiApiInterface;
|
|
|
|
public constructor(@inject(Service.NHENTAI_API) nhentaiApi: NhentaiApiInterface) {
|
|
this.nhentaiApi = nhentaiApi;
|
|
}
|
|
|
|
public async find(identifier: string): Promise<WorkEntityInterface> {
|
|
const gallery = await this.nhentaiApi.getGallery(identifier);
|
|
|
|
const work = new Work();
|
|
const copy = new Copy();
|
|
const source = new Source();
|
|
|
|
const { manager } = await getConnection(Database.LIBRARY);
|
|
|
|
source.uri = gallery.url;
|
|
|
|
copy.sources = Promise.resolve([source]);
|
|
|
|
work.nameCanonical = gallery.title.main;
|
|
if (gallery.uploadTime) {
|
|
work.releaseDate = dateObjectToString(new Date(gallery.uploadTime));
|
|
}
|
|
if (gallery.languages.length) {
|
|
const filteredLanguages: NhentaiRealLanguage[] = gallery.languages.filter(
|
|
(language): language is NhentaiRealLanguage => {
|
|
if (language === 'translated') {
|
|
// new transformation of type 'translation' here (source getter needs more abstraction before this can be done elegantly)
|
|
return false;
|
|
} else if (isNhentaiRealLanguage(language)) {
|
|
return true;
|
|
}
|
|
return false;
|
|
},
|
|
);
|
|
work.languages = Promise.all(filteredLanguages.map((language) => getLanguage(language)));
|
|
}
|
|
work.copies = Promise.resolve([copy]);
|
|
await manager.save([work, copy, source]);
|
|
|
|
return work;
|
|
}
|
|
}
|