This repository has been archived by the owner on May 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
anime.coffee
58 lines (53 loc) · 1.84 KB
/
anime.coffee
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
# Description:
# Anime RESTful APIから放送中のアニメの情報を取得
# (http://api.moemoe.tokyo/anime/v1)
#
# Commands:
# hubot anime - 今期放送中のアニメの一覧を表示
# hubot anime <title> - 今期のアニメをタイトルで検索
moment = require('moment')
table = require('easy-table')
cour = ->
year = moment().year()
month = moment().month()
if month < 3
return "/#{year}/1"
if month < 6
return "/#{year}/2"
if month < 9
return "/#{year}/3"
return "/#{year}/4"
module.exports = (robot) ->
ERR_MSG = 'Anime RESTful APIの呼出に失敗しました。'
NIL_MSG = '結果はありません。'
robot.respond /anime$/i, (msg) ->
url = "http://api.moemoe.tokyo/anime/v1/master#{cour()}"
msg.http(url).get() (err, res, body) ->
if err? or res.statusCode isnt 200
return msg.reply("#{ERR_MSG}\n```\n#{err}\n```")
animes = JSON.parse(body)
t = new table
for anime in animes
t.cell('Twitter', '@' + anime.twitter_account)
t.cell('Title', anime.title)
t.newRow()
if t.rows.length > 0
return msg.reply('```\n' + t.print().trim() + '\n```')
msg.reply(NIL_MSG)
robot.respond /anime\s+(.+)$/i, (msg) ->
url = "http://api.moemoe.tokyo/anime/v1/master#{cour()}"
keyword = msg.match[1]
msg.http(url).get() (err, res, body) ->
if err? or res.statusCode isnt 200
return msg.reply("#{ERR_MSG}\n```\n#{err}\n```")
animes = JSON.parse(body)
pattern = new RegExp(keyword, 'i')
t = new table
for anime in animes
if anime.title.search(pattern) >= 0
t.cell('Twitter', '@' + anime.twitter_account)
t.cell('Title', anime.title)
t.newRow()
if t.rows.length > 0
return msg.reply('```\n' + t.print().trim() + '\n```')
msg.reply(NIL_MSG)