Skip to content

Commit

Permalink
feat: import artwork from imgur album
Browse files Browse the repository at this point in the history
  • Loading branch information
harisato committed Aug 8, 2024
1 parent 59ac9aa commit 1a55e9b
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/modules/artwork/artwork.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,21 @@ export class ArtworkService implements OnModuleInit {
const urls = [];
vaidArtworks.forEach(async (artwork: string) => {
if (artwork.indexOf('drive.google.com/drive/folders') > 0) {
// get all file in folder
// get all file in google drive folder
const files = await this.crawlGoogleDriveFolder(artwork);
files.forEach((file) => {
const fileUrl = `https://drive.google.com/file/d/${file.id}`;
urls.push(fileUrl);
crawlPromises.push(this.crawlImage(fileUrl));
});
} else if (artwork.indexOf('imgur.com/a/') > 0) {
// get all file in imgur album
const files = await this.crawlImgurAlbum(artwork);
files.forEach((file) => {
const fileUrl = `https://i.imgur.com/${file.id}.jpg`;
urls.push(fileUrl);
crawlPromises.push(this.crawlImage(fileUrl));
});
} else {
urls.push(artwork);
crawlPromises.push(this.crawlImage(artwork));
Expand Down Expand Up @@ -210,6 +218,27 @@ export class ArtworkService implements OnModuleInit {
}
}

private async crawlImgurAlbum(url: string) {
const api = this.configService.get<string>('imgur.api');
const clientId = this.configService.get<string>('imgur.clientId');
const albumHash = url.split('/')?.[4];
try {
const response = await axios.get(`${api}/3/album/${albumHash}/images`, {
headers: {
Authorization: `Client-ID ${clientId}`,
},
});

return response.data?.data.map((data) => ({ id: data.id }));
} catch (error) {
return {
errors: {
message: JSON.stringify(error),
},
};
}
}

private async crawlGoogleDriveImage(url: string) {
const fileId = url.split('/')?.[5];
try {
Expand Down

0 comments on commit 1a55e9b

Please sign in to comment.