-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport.ts
106 lines (95 loc) · 3.46 KB
/
import.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import fetch from "node-fetch";
import {Movie} from "./model";
import {saveMovie} from "./repository";
import {PutObjectCommand, PutObjectCommandInput, S3Client} from "@aws-sdk/client-s3";
import {getIamToken, Token} from "./iam";
import {HttpRequest} from "@aws-sdk/protocol-http";
const MOVIES_COUNT = 100;
const s3Client = new S3Client({
region: "ru-central1",
endpoint: "https://storage.yandexcloud.net"
});
interface TmdbMovie {
id: number,
title: string,
type: string,
original_title: string,
original_language: string,
release_date: string,
poster_path: string,
popularity: number,
video: boolean,
vote_count: number,
vote_average: number,
genre_ids: number[],
backdrop_path: string,
adult: boolean,
overview: string,
}
interface TmdbGenre {
id: number;
name: string;
}
function transform(tmdbMovie: TmdbMovie, genresMap: Map<number, string>): Movie {
return {
id: tmdbMovie.id,
title: tmdbMovie.title,
original_title: tmdbMovie.original_title,
original_language: tmdbMovie.original_language,
poster_path: tmdbMovie.poster_path,
backdrop_path: tmdbMovie.backdrop_path,
popularity: tmdbMovie.popularity,
vote_average: tmdbMovie.vote_average / 2,
vote_count: tmdbMovie.vote_count,
overview: tmdbMovie.overview,
release_date: tmdbMovie.release_date,
genres: tmdbMovie.genre_ids ? tmdbMovie.genre_ids.map(genreId => genresMap.get(genreId)).join(", ") : undefined,
adult: String(tmdbMovie.adult),
video: String(tmdbMovie.video),
type: "FILM"
};
}
export const handler = async (event: any): Promise<any> => {
const genres: TmdbGenre[] = await (await fetch("https://storage.yandexcloud.net/movies-app/genres.json")).json() as TmdbGenre[];
const genresMap = new Map(genres.map(genre => [genre.id, genre.name]));
const tmdbMovies: TmdbMovie[] = await (await fetch("https://storage.yandexcloud.net/movies-app/tmdb.json")).json() as TmdbMovie[];
for (let i = 0; i < MOVIES_COUNT; i++) {
let movie = transform(tmdbMovies[i], genresMap);
await saveMovie(movie)
await importImage("posters", movie.poster_path?.slice(1))
await importImage("backdrops", movie.backdrop_path?.slice(1))
}
return {
"statusCode": 200,
"body": `Imported ${MOVIES_COUNT} movies from TMDB`,
"isBase64Encoded": false
}
}
async function importImage(folder: string, file: string | undefined, size: string = "w500") {
if (!file) return;
const uploadParams: PutObjectCommandInput = {
Bucket: process.env.IMAGES_BUCKET_NAME,
Key: `${folder}/${file}`,
Body: await (await fetch(`https://image.tmdb.org/t/p/${size}/${file}`)).buffer()
};
try {
await callWithToken(() => s3Client.send(new PutObjectCommand(uploadParams)))
console.log(`Imported image ${file}`)
} catch (e) {
console.error(`Failed to import image ${file}: `, e);
}
}
function callWithToken(operation: () => Promise<any>): Promise<any> {
s3Client.middlewareStack.add(
(next) => async (arguments_) => {
const request = arguments_.request as HttpRequest;
const token: Token = await getIamToken();
request.headers["X-YaCloud-SubjectToken"] = token.access_token;
return next(arguments_);
},
{
step: "finalizeRequest",
}
);
return operation.apply({});
}