-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
56 lines (49 loc) · 1.27 KB
/
index.js
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
const request = require('request')
const qs = require('querystring')
const BASE_URL = 'http://www.omdbapi.com/'
const OPTS_DEFAULT = {
r: 'json',
plot: 'short',
t: null,
}
module.exports = {
keyword: 'omdb',
helper: {
title: 'omdb',
subtitle: 'Find a movie you\'re curious about',
},
query: q => new Promise(resolve => {
const searchObj = Object.assign({}, OPTS_DEFAULT, { t: q })
const searchParams = qs.stringify(searchObj)
const url = `${BASE_URL}?${searchParams}`
request.get({ url }, (err, responseObj, body) => {
if (err) { resolve({ items: [] }); return }
const response = JSON.parse(body)
if (!isTruthyResponse(response)) {
resolve({ items: [] })
return
}
const resultItem = {
title: response.Title,
// @TODO: Truncate the Plot when it is long
subtitle: `${response.imdbRating} - ${response.Plot}`,
arg: `http://imdb.com/title/${response.imdbID}`,
}
resolve({ items: [resultItem] })
})
}),
render: {
type: 'html',
render,
},
}
function render (item) {
// @TODO: Add more info into the panel
return `
<h3>${item.Title}</h3>
<p>${item.Plot}</p>
`
}
function isTruthyResponse ({ Response }) {
return Response === 'True'
}