local nicovideo = {}
nicovideo.triggers = {
"nicovideo.jp/watch/(sm%d+)",
"nicovideo.jp/watch/(%d+)",
"nico.ms/(sm%d+)"
}
local makeOurDate = function(dateString)
local pattern = "(%d+)%-(%d+)%-(%d+)T"
local year, month, day = dateString:match(pattern)
if month == "00" then
return year
elseif day == "00" then
return month..'.'..year
else
return day..'.'..month..'.'..year
end
end
function nicovideo:get_video(id)
local url = 'https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20xml%20where%20url%3D%22http%3A%2F%2Fext.nicovideo.jp%2Fapi%2Fgetthumbinfo%2F'..id..'%22&format=json'
local res, code = https.request(url)
if code ~= 200 then return nil end
local data = json.decode(res).query.results.nicovideo_thumb_response.thumb
if not data then return nil end
local title = data.title
local date = makeOurDate(data.first_retrieve)
if data.user_nickname then
user = data.user_nickname
else
user = data.ch_name
end
local views = comma_value(data.view_counter)
local dura = data.length
local favs = comma_value(data.mylist_counter)
local comm = comma_value(data.comment_num)
local pic = data.thumbnail_url
local text = ''..title..'\nHochgeladen am '..date..' von '..user..', '..views..'x angesehen, Länge: '..dura..', '..favs..'x favoritisiert, '..comm..' Kommentare'
return text, pic
end
function nicovideo:action(msg, config, matches)
local id = matches[1]
local text, pic_url = nicovideo:get_video(id)
if not text then
utilities.send_reply(self, msg, config.errors.results)
return
end
utilities.send_typing(self, msg.chat.id, 'upload_photo')
local file = download_to_file(pic_url, 'nicovideo_thumb.png')
utilities.send_photo(self, msg.chat.id, file, nil, msg.message_id)
utilities.send_reply(self, msg, text, 'HTML')
end
return nicovideo