Comparing version 3.0.0 to 3.1.0
@@ -0,1 +1,15 @@ | ||
# Version 3.0.0 -> 3.1.0 | ||
This adds searching for movies and items from omdb. | ||
```js | ||
imdb.search({title: 'foo'}, {apiKey: 'bar'}).then(console.log); | ||
``` | ||
This also adds supports for timeouts | ||
```js | ||
imdb.get('The Toxic Avenger', {apiKey: 'foo', timeout: 30}).then(console.log); | ||
``` | ||
# Version 2.2.2 -> 3.0.0 | ||
@@ -2,0 +16,0 @@ |
112
lib/imdb.js
@@ -19,2 +19,17 @@ "use strict"; | ||
var omdbapi = "https://www.omdbapi.com/"; | ||
function reqtoqueryobj(req, apikey, page) { | ||
var ret = { | ||
"s": req.title, | ||
"r": "json", | ||
"apikey": apikey, | ||
"page": page | ||
}; | ||
if (req.reqtype !== undefined) { | ||
ret["type"] = req.reqtype; | ||
} | ||
if (req.year !== undefined) { | ||
req["y"] = req.year; | ||
} | ||
return ret; | ||
} | ||
var trans_table = new util_1.Inverter({ | ||
@@ -43,6 +58,6 @@ "genres": "Genre", | ||
} | ||
else if (obj.hasOwnProperty(attr) && trans_table.get(attr) !== undefined) { | ||
else if (trans_table.get(attr) !== undefined) { | ||
this[trans_table.get(attr)] = obj[attr]; | ||
} | ||
else if (obj.hasOwnProperty(attr)) { | ||
else { | ||
this[attr.toLowerCase()] = obj[attr]; | ||
@@ -70,6 +85,6 @@ } | ||
} | ||
else if (obj.hasOwnProperty(attr) && trans_table.get(attr) !== undefined) { | ||
else if (trans_table.get(attr) !== undefined) { | ||
this[trans_table.get(attr)] = obj[attr]; | ||
} | ||
else if (obj.hasOwnProperty(attr)) { | ||
else { | ||
this[attr.toLowerCase()] = obj[attr]; | ||
@@ -86,3 +101,3 @@ } | ||
__extends(TVShow, _super); | ||
function TVShow(object, apiKey) { | ||
function TVShow(object, opts) { | ||
var _this = _super.call(this, object) || this; | ||
@@ -94,3 +109,3 @@ _this._episodes = []; | ||
_this.totalseasons = parseInt(_this["totalseasons"]); | ||
_this._apikey = apiKey; | ||
_this.opts = opts; | ||
return _this; | ||
@@ -105,3 +120,7 @@ } | ||
for (var i = 1; i <= tvShow.totalseasons; i++) { | ||
funcs.push(rp({ "qs": { "apikey": tvShow._apikey, "i": tvShow.imdbid, "r": "json", "Season": i }, "json": true, "url": omdbapi })); | ||
var reqopts = { "qs": { "apikey": tvShow.opts.apiKey, "i": tvShow.imdbid, "r": "json", "Season": i }, "json": true, "url": omdbapi }; | ||
if ("timeout" in this.opts) { | ||
reqopts["timeout"] = this.opts.timeout; | ||
} | ||
funcs.push(rp(reqopts)); | ||
} | ||
@@ -114,3 +133,3 @@ var prom = Promise.all(funcs) | ||
if (interfaces_1.isError(datum)) { | ||
var err = new ImdbError(datum.Error, undefined); | ||
var err = new ImdbError(datum.Error); | ||
if (cb) { | ||
@@ -146,6 +165,43 @@ return cb(err, undefined); | ||
exports.TVShow = TVShow; | ||
var SearchResult = (function () { | ||
function SearchResult(obj) { | ||
for (var attr in obj) { | ||
if (attr === "Year") { | ||
this.year = parseInt(obj[attr]); | ||
} | ||
else { | ||
this[attr.toLowerCase()] = obj[attr]; | ||
} | ||
} | ||
} | ||
return SearchResult; | ||
}()); | ||
exports.SearchResult = SearchResult; | ||
var SearchResults = (function () { | ||
function SearchResults(obj, page, opts, req) { | ||
this.results = []; | ||
this.page = page; | ||
this.req = req; | ||
this.opts = opts; | ||
for (var attr in obj) { | ||
if (attr === "Search") { | ||
for (var _i = 0, _a = obj.Search; _i < _a.length; _i++) { | ||
var result = _a[_i]; | ||
this.results.push(new SearchResult(result)); | ||
} | ||
} | ||
else { | ||
this[attr.toLowerCase()] = obj[attr]; | ||
} | ||
} | ||
} | ||
SearchResults.prototype.next = function () { | ||
return search(this.req, this.opts, this.page + 1); | ||
}; | ||
return SearchResults; | ||
}()); | ||
exports.SearchResults = SearchResults; | ||
var ImdbError = (function () { | ||
function ImdbError(message, movie) { | ||
function ImdbError(message) { | ||
this.message = message; | ||
this.movie = movie; | ||
this.name = "imdb api error"; | ||
@@ -158,3 +214,3 @@ } | ||
if (req.opts === undefined || !req.opts.hasOwnProperty("apiKey")) { | ||
var err = new ImdbError("Missing api key in opts", req); | ||
var err = new ImdbError("Missing api key in opts"); | ||
if (cb) { | ||
@@ -167,3 +223,2 @@ return cb(err, undefined); | ||
} | ||
var responseData = ""; | ||
var qs = { apikey: req.opts.apiKey, plot: "full", r: "json", y: req.year }; | ||
@@ -176,6 +231,10 @@ if (req.name) { | ||
} | ||
var prom = rp({ "qs": qs, url: omdbapi, json: true }).then(function (data) { | ||
var reqopts = { "qs": qs, url: omdbapi, json: true }; | ||
if ("timeout" in req.opts) { | ||
reqopts["timeout"] = req.opts.timeout; | ||
} | ||
var prom = rp(reqopts).then(function (data) { | ||
var ret; | ||
if (interfaces_1.isError(data)) { | ||
var err = new ImdbError(data.Error + ": " + (req.name ? req.name : req.id), req); | ||
var err = new ImdbError(data.Error + ": " + (req.name ? req.name : req.id)); | ||
if (cb) { | ||
@@ -193,3 +252,3 @@ return cb(err, undefined); | ||
else if (interfaces_1.isTvshow(data)) { | ||
ret = new TVShow(data, req.opts.apiKey); | ||
ret = new TVShow(data, req.opts); | ||
} | ||
@@ -200,3 +259,3 @@ else if (interfaces_1.isEpisode(data)) { | ||
else { | ||
var err = new ImdbError("type: " + data.Type + " not valid", req); | ||
var err = new ImdbError("type: " + data.Type + " not valid"); | ||
if (cb) { | ||
@@ -233,2 +292,23 @@ return cb(err, undefined); | ||
exports.getById = getById; | ||
function search(req, opts, page) { | ||
if (page === undefined) { | ||
page = 1; | ||
} | ||
var qs = reqtoqueryobj(req, opts.apiKey, page); | ||
var reqopts = { "qs": qs, url: omdbapi, json: true }; | ||
if ("timeout" in opts) { | ||
reqopts["timeout"] = opts.timeout; | ||
} | ||
var prom = rp(reqopts).then(function (data) { | ||
if (interfaces_1.isError(data)) { | ||
var err = new ImdbError(data.Error + ": " + req.title); | ||
return Promise.reject(err); | ||
} | ||
else { | ||
return Promise.resolve(new SearchResults(data, page, opts, req)); | ||
} | ||
}); | ||
return prom; | ||
} | ||
exports.search = search; | ||
//# sourceMappingURL=imdb.js.map |
@@ -5,3 +5,3 @@ { | ||
"description": "Queries unofficial imdb APIs to get movie and television information from imdb", | ||
"version": "3.0.0", | ||
"version": "3.1.0", | ||
"main": "lib/imdb.js", | ||
@@ -8,0 +8,0 @@ "homepage": "https://github.com/worr/node-imdb-api", |
@@ -26,12 +26,5 @@ # node-imdb-api | ||
```js | ||
let movie; | ||
imdb.getReq({ apiKey: 'foo', name: 'The Toxic Avenger' }, (err, things) => { | ||
movie = things; | ||
}); | ||
// Promises! | ||
imdb.get('The Toxic Avenger', {apiKey: 'foo'}).then(console.log); | ||
imdb.getById('tt0090190', {apiKey: 'foo'}).then(console.log); | ||
imdb.getReq({ name: 'The Toxic Avenger', opts: {apiKey: 'foo'} }).then(console.log); | ||
imdb.get('The Toxic Avenger', {apiKey: 'foo', timeout: 30}).then(console.log).catch(console.log); | ||
imdb.getById('tt0090190', {apiKey: 'foo', timeout: 30}).then(console.log).catch(console.log); | ||
imdb.getReq({ name: 'The Toxic Avenger', opts: {apiKey: 'foo', timeout: 30} }).then(console.log).catch(console.log); | ||
``` | ||
@@ -69,6 +62,3 @@ DATA | ||
```js | ||
let movie; | ||
imdb.getReq({ id: 'tt0090190', opts: {apiKey: 'foo'} }, (err, things) => { | ||
movie = things; | ||
}); | ||
imdb.getReq({ id: 'tt0090190', opts: {apiKey: 'foo'} }).then(console.log) | ||
``` | ||
@@ -103,5 +93,16 @@ DATA | ||
``` | ||
How do I search for things? | ||
```js | ||
imdb.search({ | ||
title: 'Toxic Avenger' | ||
}, { | ||
apiKey: 'foo' | ||
}).then(console.log).catch(console.log); | ||
``` | ||
How do I get series episodes? | ||
Well, it's a promise (or a function that takes a callback). | ||
Well, it's a promise: | ||
```js | ||
@@ -112,6 +113,2 @@ imdb.get('How I Met Your Mother', {apiKey: 'foo'}).then(things => { | ||
imdb.get('How I Met Your Mother', {apiKey: 'foo'}, (err, things) => { | ||
things.episodes((err, moreThings) => console.log(moreThings)); | ||
}); | ||
... | ||
@@ -118,0 +115,0 @@ Episode { |
185
src/imdb.ts
@@ -17,2 +17,4 @@ /// <reference path="../typings/index.d.ts"/> | ||
OmdbSeason, | ||
OmdbSearch, | ||
OmdbSearchResult, | ||
OmdbTvshow | ||
@@ -24,3 +26,3 @@ } from "./interfaces"; | ||
let Promise = es6promise.Promise; | ||
const Promise = es6promise.Promise; | ||
@@ -31,4 +33,10 @@ const omdbapi = "https://www.omdbapi.com/"; | ||
apiKey: string; | ||
timeout?: number; | ||
} | ||
type RequestType = "movie" | ||
| "series" | ||
| "episode" | ||
| "game"; | ||
export interface MovieRequest { | ||
@@ -41,2 +49,27 @@ name?: string; | ||
export interface SearchRequest { | ||
title: string; | ||
reqtype?: RequestType; | ||
year?: number; | ||
} | ||
function reqtoqueryobj(req: SearchRequest, apikey: string, page: number): object { | ||
const ret = { | ||
"s": req.title, | ||
"r": "json", | ||
"apikey": apikey, | ||
"page": page, | ||
}; | ||
if (req.reqtype !== undefined) { | ||
ret["type"] = req.reqtype; | ||
} | ||
if (req.year !== undefined) { | ||
req["y"] = req.year; | ||
} | ||
return ret; | ||
} | ||
const trans_table = new Inverter({ | ||
@@ -57,7 +90,7 @@ "genres": "Genre", | ||
constructor (obj: OmdbEpisode, season: number) { | ||
constructor(obj: OmdbEpisode, season: number) { | ||
this.season = season; | ||
for (let attr in obj) { | ||
for (const attr in obj) { | ||
if (attr === "Released") { | ||
let [year, month, day] = obj[attr].split("-"); | ||
const [year, month, day] = obj[attr].split("-"); | ||
this.released = new Date(parseInt(year), parseInt(month) - 1, parseInt(day)); | ||
@@ -70,5 +103,5 @@ } else if (attr === "Rating") { | ||
this.name = obj[attr]; | ||
} else if (obj.hasOwnProperty(attr) && trans_table.get(attr) !== undefined) { | ||
} else if (trans_table.get(attr) !== undefined) { | ||
this[trans_table.get(attr)] = obj[attr]; | ||
} else if (obj.hasOwnProperty(attr)) { | ||
} else { | ||
this[attr.toLowerCase()] = obj[attr]; | ||
@@ -106,7 +139,7 @@ } | ||
constructor (obj: OmdbMovie) { | ||
for (let attr in obj) { | ||
constructor(obj: OmdbMovie) { | ||
for (const attr in obj) { | ||
if (attr === "year" || attr.toLowerCase() === "year") { | ||
this["_year_data"] = obj[attr]; | ||
if (! obj[attr].match(/\d{4}[\-–]\d{4}/)) { | ||
if (!obj[attr].match(/\d{4}[\-–]\d{4}/)) { | ||
this[attr.toLowerCase()] = parseInt(obj[attr]); | ||
@@ -118,5 +151,5 @@ } | ||
this[attr.toLowerCase()] = parseFloat(obj[attr]); | ||
} else if (obj.hasOwnProperty(attr) && trans_table.get(attr) !== undefined) { | ||
} else if (trans_table.get(attr) !== undefined) { | ||
this[trans_table.get(attr)] = obj[attr]; | ||
} else if (obj.hasOwnProperty(attr)) { | ||
} else { | ||
this[attr.toLowerCase()] = obj[attr]; | ||
@@ -133,3 +166,3 @@ } | ||
private _episodes: Episode[] = []; | ||
private _apikey: string; | ||
private opts: MovieOpts; | ||
public start_year; | ||
@@ -139,9 +172,9 @@ public end_year; | ||
constructor (object: OmdbTvshow, apiKey: string) { | ||
constructor(object: OmdbTvshow, opts: MovieOpts) { | ||
super(object); | ||
let years = this["_year_data"].split("-"); | ||
const years = this["_year_data"].split("-"); | ||
this.start_year = parseInt(years[0]) ? parseInt(years[0]) : null; | ||
this.end_year = parseInt(years[1]) ? parseInt(years[1]) : null; | ||
this.totalseasons = parseInt(this["totalseasons"]); | ||
this._apikey = apiKey; | ||
this.opts = opts; | ||
} | ||
@@ -154,16 +187,21 @@ | ||
let tvShow = this; | ||
const tvShow = this; | ||
let funcs = []; | ||
const funcs = []; | ||
for (let i = 1; i <= tvShow.totalseasons; i++) { | ||
funcs.push(rp({"qs": {"apikey": tvShow._apikey, "i": tvShow.imdbid, "r": "json", "Season": i}, "json": true, "url": omdbapi})); | ||
const reqopts = { "qs": { "apikey": tvShow.opts.apiKey, "i": tvShow.imdbid, "r": "json", "Season": i }, "json": true, "url": omdbapi }; | ||
if ("timeout" in this.opts) { | ||
reqopts["timeout"] = this.opts.timeout; | ||
} | ||
funcs.push(rp(reqopts)); | ||
} | ||
let prom = Promise.all(funcs) | ||
const prom = Promise.all(funcs) | ||
.then(function(ep_data: OmdbSeason[] | OmdbError[]) { | ||
let eps: Episode[] = []; | ||
for (let key in ep_data) { | ||
let datum = ep_data[key]; | ||
const eps: Episode[] = []; | ||
for (const key in ep_data) { | ||
const datum = ep_data[key]; | ||
if (isError(datum)) { | ||
let err = new ImdbError(datum.Error, undefined); | ||
const err = new ImdbError(datum.Error); | ||
if (cb) { | ||
@@ -175,4 +213,4 @@ return cb(err, undefined); | ||
} else { | ||
let season = parseInt(datum.Season); | ||
for (let ep in datum.Episodes) { | ||
const season = parseInt(datum.Season); | ||
for (const ep in datum.Episodes) { | ||
eps.push(new Episode(datum.Episodes[ep], season)); | ||
@@ -201,7 +239,52 @@ } | ||
export class SearchResult { | ||
public title: string; | ||
public year: number; | ||
public imdbid: string; | ||
public type: RequestType; | ||
public poster: string; | ||
constructor(obj: OmdbSearchResult) { | ||
for (const attr in obj) { | ||
if (attr === "Year") { | ||
this.year = parseInt(obj[attr]); | ||
} else { | ||
this[attr.toLowerCase()] = obj[attr]; | ||
} | ||
} | ||
} | ||
} | ||
export class SearchResults { | ||
public results: SearchResult[] = []; | ||
public totalresults: number; | ||
private page: number; | ||
private opts: MovieOpts; | ||
private req: SearchRequest; | ||
constructor(obj: OmdbSearch, page: number, opts: MovieOpts, req: SearchRequest) { | ||
this.page = page; | ||
this.req = req; | ||
this.opts = opts; | ||
for (const attr in obj) { | ||
if (attr === "Search") { | ||
for (const result of obj.Search) { | ||
this.results.push(new SearchResult(result)); | ||
} | ||
} else { | ||
this[attr.toLowerCase()] = obj[attr]; | ||
} | ||
} | ||
} | ||
public next(): Promise<SearchResults> { | ||
return search(this.req, this.opts, this.page + 1); | ||
} | ||
} | ||
export class ImdbError { | ||
public name: string = "imdb api error"; | ||
constructor(public message: string, public movie: MovieRequest) { | ||
} | ||
constructor(public message: string) { } | ||
} | ||
@@ -211,4 +294,4 @@ | ||
if (req.opts === undefined || ! req.opts.hasOwnProperty("apiKey")) { | ||
let err = new ImdbError("Missing api key in opts", req); | ||
if (req.opts === undefined || !req.opts.hasOwnProperty("apiKey")) { | ||
const err = new ImdbError("Missing api key in opts"); | ||
if (cb) { | ||
@@ -221,4 +304,3 @@ return cb(err, undefined); | ||
let responseData = ""; | ||
let qs = {apikey: req.opts.apiKey, plot: "full", r: "json", y: req.year}; | ||
const qs = { apikey: req.opts.apiKey, plot: "full", r: "json", y: req.year }; | ||
@@ -231,6 +313,12 @@ if (req.name) { | ||
let prom = rp({"qs": qs, url: omdbapi, json: true}).then(function(data: OmdbMovie | OmdbError) { | ||
const reqopts = { "qs": qs, url: omdbapi, json: true }; | ||
if ("timeout" in req.opts) { | ||
reqopts["timeout"] = req.opts.timeout; | ||
} | ||
const prom = rp(reqopts).then(function(data: OmdbMovie | OmdbError) { | ||
let ret: Movie | Episode; | ||
if (isError(data)) { | ||
let err = new ImdbError(data.Error + ": " + (req.name ? req.name : req.id), req); | ||
const err = new ImdbError(data.Error + ": " + (req.name ? req.name : req.id)); | ||
if (cb) { | ||
@@ -245,7 +333,7 @@ return cb(err, undefined); | ||
} else if (isTvshow(data)) { | ||
ret = new TVShow(data, req.opts.apiKey); | ||
ret = new TVShow(data, req.opts); | ||
} else if (isEpisode(data)) { | ||
ret = new Episode(data, 30); | ||
} else { | ||
let err = new ImdbError("type: " + data.Type + " not valid", req); | ||
const err = new ImdbError("type: " + data.Type + " not valid"); | ||
if (cb) { | ||
@@ -276,7 +364,30 @@ return cb(err, undefined); | ||
export function get(name: string, opts: MovieOpts, cb?: (err: Error, data: Movie) => any): Promise<Movie> { | ||
return getReq({id: undefined, opts: opts, name: name }, cb); | ||
return getReq({ id: undefined, opts: opts, name: name }, cb); | ||
} | ||
export function getById(imdbid: string, opts: MovieOpts, cb?: (err: Error, data: Movie) => any): Promise<Movie> { | ||
return getReq({id: imdbid, opts: opts, name: undefined}, cb); | ||
return getReq({ id: imdbid, opts: opts, name: undefined }, cb); | ||
} | ||
export function search(req: SearchRequest, opts: MovieOpts, page?: number): Promise<SearchResults> { | ||
if (page === undefined) { | ||
page = 1; | ||
} | ||
const qs = reqtoqueryobj(req, opts.apiKey, page); | ||
const reqopts = { "qs": qs, url: omdbapi, json: true }; | ||
if ("timeout" in opts) { | ||
reqopts["timeout"] = opts.timeout; | ||
} | ||
const prom = rp(reqopts).then(function(data: OmdbSearch | OmdbError) { | ||
if (isError(data)) { | ||
const err = new ImdbError(`${data.Error}: ${req.title}`); | ||
return Promise.reject(err); | ||
} else { | ||
return Promise.resolve(new SearchResults(data, page, opts, req)); | ||
} | ||
}); | ||
return prom; | ||
} |
@@ -66,2 +66,16 @@ export interface OmdbMovie { | ||
export interface OmdbSearchResult { | ||
Title: string; | ||
Year: string; | ||
imdbID: string; | ||
Type: string; | ||
Poster: string; | ||
} | ||
export interface OmdbSearch { | ||
Search: OmdbSearchResult[]; | ||
totalResults: string; | ||
Response: string; | ||
} | ||
export interface OmdbError { | ||
@@ -72,3 +86,3 @@ Response: string; | ||
export function isError(response: OmdbSeason | OmdbTvshow | OmdbMovie | OmdbError): response is OmdbError { | ||
export function isError(response: OmdbSearch | OmdbSeason | OmdbTvshow | OmdbMovie | OmdbError): response is OmdbError { | ||
return response.Response === "False"; | ||
@@ -75,0 +89,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
200548
39
4289
179
5