Skip to content

Commit

Permalink
feat(omdb): Implemented OMDB service
Browse files Browse the repository at this point in the history
  • Loading branch information
TriPSs committed Oct 15, 2020
1 parent 6f544d5 commit 1f3e909
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 11 deletions.
3 changes: 2 additions & 1 deletion libs/services/omdb/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './lib/services-omdb';
export * from './omdb.module'
export * from './omdb.service'
7 changes: 0 additions & 7 deletions libs/services/omdb/src/lib/services-omdb.spec.ts

This file was deleted.

3 changes: 0 additions & 3 deletions libs/services/omdb/src/lib/services-omdb.ts

This file was deleted.

15 changes: 15 additions & 0 deletions libs/services/omdb/src/omdb.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Module, Global } from '@nestjs/common'

import { OmdbService } from './omdb.service'

@Global()
@Module({
providers: [
OmdbService
],
exports: [
OmdbService
]
})
export class OmdbModule {
}
62 changes: 62 additions & 0 deletions libs/services/omdb/src/omdb.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { Injectable, Logger } from '@nestjs/common'
import * as Omdb from 'omdb-api-pt'
import { Images, Movie } from '@pct-org/mongo-models'

@Injectable()
export class OmdbService {

private readonly omdb

private readonly logger = new Logger('OMDB')

constructor() {
const omdbKey = process.env.OMDB_KEY

if (omdbKey) {
this.omdb = new Omdb({
apiKey: omdbKey
})
}
}

public async getMovieImages(item: Movie): Promise<Images> {
let poster = null

if (item.images.poster?.full) {
return item.images
}

try {
const images = await this.omdb.byId({
imdb: item.imdbId,
type: 'movie'
})

poster = images.Poster

} catch (err) {
if (err.statusCode && err.statusCode === 404) {
this.logger.error(`Can't find images for '${item.slug}'`, err)

} else if (err.statusCode && err.statusCode === 401) {
this.logger.error(`Rate limit hit when fetching for '${item.slug}'`, err)

} else {
this.logger.error(`Error happened getting images for '${item.slug}'`, err)
}
}

return {
backdrop: item.images.backdrop,
poster: poster
? {
full: poster.url,
high: poster.url,
medium: poster.url,
thumb: poster.url
}
: item.images.poster
}
}

}

0 comments on commit 1f3e909

Please sign in to comment.