Skip to content

Commit

Permalink
re promisify providers
Browse files Browse the repository at this point in the history
  • Loading branch information
cbartondock committed May 30, 2024
1 parent 0b1cdd9 commit c4fa263
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 67 deletions.
74 changes: 38 additions & 36 deletions src/lib/image-providers/steamcdn.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,57 +14,59 @@ export class SteamCDNProvider extends GenericProvider {
this.xrw = new xRequestWrapper(proxy, true, 3, 3000);
this.client = new SGDB({key: apiKey});
}

retrieveUrls() {
let self = this;
this.xrw.promise = new Promise<void>(async (resolve) => {
try {
let chosenId: number;
if(sgdbIdRegex.test(self.proxy.title)) {
chosenId = parseInt(self.proxy.title.match(sgdbIdRegex)[1]);
} else {
chosenId = ((await self.client.searchGame(self.proxy.title))[0]||{}).id;
}
let imageGameId: string;
this.xrw.promise = new Promise<void>((resolve) => {
let idPromise: Promise<number> = null;
if(sgdbIdRegex.test(self.proxy.title)) {
idPromise = Promise.resolve(parseInt(self.proxy.title.match(sgdbIdRegex)[1]))
} else {
idPromise = self.client.searchGame(self.proxy.title).then((res: any) => (res[0]||{}).id);
}
idPromise.then((chosenId: number|undefined)=>{
if(!chosenId) {
if(self.proxy.imageType === 'long') { // Don't throw this error 5 times.
self.xrw.logError(`SGDB found no matching games for title "${self.proxy.title}"`)
}
self.proxy.completed();
resolve();
} else {
// convert sgdbId to steamId
const platformData = ((await self.client.getGameById(chosenId, {platformdata: ['steam']}))||{}).external_platform_data;
if(platformData && platformData.steam && platformData.steam.length) {
const {id, metadata} = platformData.steam[0];
// return CDN urls
for(let artworkType of artworkTypes) {
if(self.proxy.imageType === artworkType && id) {
if(self.proxy.imageType === 'icon' && metadata.clienticon) {
self.proxy.image({
imageProvider: imageProviderNames.steamCDN,
imageUrl: `https://cdn.cloudflare.steamstatic.com/steamcommunity/public/images/apps/${id}/${metadata.clienticon}.ico`,
loadStatus: 'notStarted'
})
} else {
self.proxy.image({
imageProvider: imageProviderNames.steamCDN,
imageUrl: `https://cdn.cloudflare.steamstatic.com/steam/apps/${id}/${steamArtworkDict[artworkType]}`,
loadStatus: 'notStarted'
})
}
}
// convert sgdbId to steamId
return self.client.getGameById(chosenId, {platformdata: ['steam']})
}
}).then((data: any)=> {
const platformData = (data||{}).external_platform_data;
if(platformData && platformData.steam && platformData.steam.length) {
const {id, metadata} = platformData.steam[0];
// return CDN urls
for(let artworkType of artworkTypes) {
if(self.proxy.imageType === artworkType && id) {
if(self.proxy.imageType === 'icon' && metadata.clienticon) {
self.proxy.image({
imageProvider: imageProviderNames.steamCDN,
imageUrl: `https://cdn.cloudflare.steamstatic.com/steamcommunity/public/images/apps/${id}/${metadata.clienticon}.ico`,
loadStatus: 'notStarted'
})
} else {
self.proxy.image({
imageProvider: imageProviderNames.steamCDN,
imageUrl: `https://cdn.cloudflare.steamstatic.com/steam/apps/${id}/${steamArtworkDict[artworkType]}`,
loadStatus: 'notStarted'
})
}
}
self.proxy.completed();
resolve();
}
}
catch(error) {
}
self.proxy.completed();
resolve();
})
.catch((error: string) => {
self.xrw.logError(error);
self.proxy.completed();
resolve();
};
})
});
});
}

stopUrlDownload() {
Expand Down
61 changes: 32 additions & 29 deletions src/lib/image-providers/steamgriddb.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ export class SteamGridDbProvider extends GenericProvider {
retrieveUrls() {
let self = this;
let imageGameId: string;
this.xrw.promise = new Promise<void>(async (resolve) => {
try {
let chosenId: number;
if(sgdbIdRegex.test(self.proxy.title)) {
chosenId = parseInt(self.proxy.title.match(sgdbIdRegex)[1]);
} else {
chosenId = ((await self.client.searchGame(self.proxy.title))[0]||{}).id;
}
this.xrw.promise = new Promise<void>((resolve) => {
let idPromise: Promise<number> = null;
if(sgdbIdRegex.test(self.proxy.title)) {
idPromise = Promise.resolve(parseInt(self.proxy.title.match(sgdbIdRegex)[1]))
} else {
idPromise = self.client.searchGame(self.proxy.title).then((res: any) => (res[0]||{}).id);
}
idPromise.then((chosenId: number|undefined)=>{
if(!chosenId) {
if(self.proxy.imageType === 'long') { // Don't throw this error 5 times.
self.xrw.logError(`SGDB found no matching games for title "${self.proxy.title}"`)
Expand All @@ -70,52 +70,55 @@ export class SteamGridDbProvider extends GenericProvider {
humor: self.proxy.imageProviderAPIs.humor ? "any" : "false"
};
const choose = (x: any, fallback: string[]) => x && x.length ? x : fallback;
let res: any;
if(self.proxy.imageType === 'long') {
res = await self.client.getGrids(Object.assign(params, {
query = self.client.getGrids(Object.assign(params, {
dimensions: choose(self.proxy.imageProviderAPIs.sizes, ["460x215","920x430"]),
styles: self.proxy.imageProviderAPIs.styles
}))
} else if (self.proxy.imageType === 'tall') {
res = await self.client.getGrids(Object.assign(params, {
query = self.client.getGrids(Object.assign(params, {
dimensions: ["600x900"],
styles: self.proxy.imageProviderAPIs.styles
}));
} else if (self.proxy.imageType === 'hero') {
res = await self.client.getHeroes(Object.assign(params, {
query = self.client.getHeroes(Object.assign(params, {
dimensions: choose(self.proxy.imageProviderAPIs.sizesHero, null),
styles: self.proxy.imageProviderAPIs.stylesHero
}));
} else if (self.proxy.imageType === 'logo') {
res = await self.client.getLogos(Object.assign(params, {
query = self.client.getLogos(Object.assign(params, {
styles: self.proxy.imageProviderAPIs.stylesLogo
}));
} else if (self.proxy.imageType === 'icon') {
res = await self.client.getIcons(Object.assign(params, {
query = self.client.getIcons(Object.assign(params, {
dimensions: choose(self.proxy.imageProviderAPIs.sizesIcon, null),
styles: self.proxy.imageProviderAPIs.stylesIcon
}));
}
if(res !== null && res.length > 0) {
for (let i=0; i < res.length; i++) {
self.proxy.image({
imageProvider: imageProviderNames.sgdb,
imageUrl: res[i].url,
imageGameId: imageGameId,
imageArtworkId: String(res[i].id),
imageUploader: res[i].author.name,
loadStatus: 'notStarted'
});
}
return query
}
})
.then((res: any)=>{
if(res !== null && res.length>0) {
for (let i=0; i < res.length; i++) {
self.proxy.image({
imageProvider: imageProviderNames.sgdb,
imageUrl: res[i].url,
imageGameId: imageGameId,
imageArtworkId: String(res[i].id),
imageUploader: res[i].author.name,
loadStatus: 'notStarted'
});
}
self.proxy.completed();
resolve();
}
} catch(error) {
self.proxy.completed();
resolve();
})
.catch((error: string) => {
self.xrw.logError(error);
self.proxy.completed();
resolve();
}
});
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/renderer/services/preview.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -691,12 +691,12 @@ export class PreviewService {
for (let imageKey of imageKeys) {
const imageByPool = this.onlineImages[artworkType][imageKey]
if(imageByPool.retrieving) { continue; }
imageByPool.retrieving = true;
const imageByProvider = imageByPool.online;
const parserEnabledProviders = imageByPool.parserEnabledProviders;
const imageProvidersForKey: OnlineProviderType[] = _.intersection(parserEnabledProviders, this.appSettings.enabledProviders);
this.previewVariables.numberOfQueriedImages += imageProvidersForKey.map((provider) => imageByProvider[provider].searchQueries.length).reduce((x,y)=>x+y, 0);
let retrievingByProvider = Object.fromEntries(onlineProviders.map(x=>[x, imageProvidersForKey.includes(x)]))
let retrievingByProvider = Object.fromEntries(onlineProviders.map(x=>[x, imageProvidersForKey.includes(x) && !!imageByProvider[x].searchQueries.length]));
imageByPool.retrieving = Object.values(retrievingByProvider).reduce((x,y)=> x||y, false)
for(let provider of imageProvidersForKey) {
const image = imageByProvider[provider];
if (image !== undefined && image.searchQueries.length) {
Expand Down

0 comments on commit c4fa263

Please sign in to comment.