This repository has been archived by the owner on Feb 20, 2019. It is now read-only.
/
index.js
executable file
·75 lines (55 loc) · 1.73 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env node
var definition = require('./parser')(process.argv.slice(2))
var addons = require('stremio-addons')
var config = require('./stremio-conf')
var stremio = new addons.Client()
var playStream = require('./playStream')
var open = require('open')
// Enable add-ons
var add = function(x) { stremio.add(x) }
config.addons.core.forEach(add)
config.addons.extra.forEach(add)
// Check args
if (! definition.name) dieWithErr('Usage: stremio-cli <name>')
// Do the work
function searchForMeta(def, cb)
{
stremio.meta.search({ query: def.name, limit: 30 }, function(err, res) {
if (err) return cb(err)
if (! (res && Array.isArray(res.results))) return cb('no results from meta.search')
var all = res.results
if (definition.year) all = all.filter(function(x) { return x.year === definition.year })
return cb(null, all)
})
}
// Utils
function dieWithErr(err)
{
console.error(err)
process.exit(1)
}
// Main
searchForMeta(definition, function(err, meta) {
if (err) dieWithErr(err)
// TODO: allow to choose if results are ambiguous
// perhaps check if name is exact match
var first = meta[0]
if (!first) dieWithErr('no meta found')
var isExactMatch = first.name.toLowerCase() == definition.name
if (definition.inApp) return open('http://app.strem.io/#/detail/'+first.type+'/'+first.imdb_id)
// XXX Use stremio-models function streamQuery (from metadata)
var q = {
type: first.type,
id: first.id
}
if (first.imdb_id) q.imdb_id = first.imdb_id
if (first.type === 'series') {
q.season = definition.season || 1
q.episode = definition.episode || 1
}
stremio.stream.find({ query: q }, function(err, streams) {
if (err) dieWithErr(err)
if (! streams[0]) dieWithErr('no stream found')
playStream(streams[0])
})
})