-
Notifications
You must be signed in to change notification settings - Fork 1
/
annict.js
75 lines (65 loc) · 2.02 KB
/
annict.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
// Description:
// Integration of anime logging service, Annict (annict.com)
//
// Configuration:
// HUBOT_ANNICT_TOKEN
//
// Commands:
// anime now - get works this season on air (upto 25 entry)
// anime q - search works by keyword
//
// Author:
// nukosuke
"use strict";
const Annict = require("annict").default;
module.exports = (robots) => {
const token = process.env.HUBOT_ANNICT_TOKEN;
var annict = new Annict;
if (typeof token === 'undefined') {
console.log("hubot-annict: Annict access token was NOT set!!");
return;
}
annict.client.setHeader("Authorization", `Bearer ${token}`);
const seasons = ['winter','spring','summer','autumn'];
robots.hear(/anime now/i, (res) => {
const now = new Date;
const season = `${now.getFullYear()}-${seasons[parseInt(now.getMonth()/3)]}`;
annict.Work.get({
fields: ['official_site_url','title'].join(','), //TODO: remove .join() after annict.js v0.0.8
filter_season: season,
sort_watchers_count: 'desc', // to get popular works first
})
.then(response => response.json())
.then(json => {
if (typeof json.works === 'undefined') {
throw new Error('invalid response json structure');
}
const message = json.works.map(work => `<${work.official_site_url}|${work.title}>`).join('\n');
res.reply(message);
})
.catch(err => {
res.reply(`error occured: ${err}`);
});
});
robots.respond(/anime q (.*)/, (res) => {
const query = res.match[1];
annict.Work.get({
filter_title: query,
})
.then(response => response.json())
.then(json => {
if (typeof json.works === 'undefined') {
throw new Error('invalid response json structure');
}
if (json.works.length === 0) {
res.reply('No results found :bow:');
} else {
const message = json.works.map(work => `<${work.official_site_url}|${work.title}>`).join('\n');
res.reply(message);
}
})
.catch(err => {
res.reply(`error: ${err}`);
});
});
};