This repository was archived by the owner on Jun 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathyoutube-info.coffee
67 lines (59 loc) · 1.77 KB
/
youtube-info.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
59
60
61
62
63
64
65
66
67
# Description
# Show video metadata when YouTube URLs are seen.
#
# Dependencies:
# None
#
# Configuration:
# None
#
# Commands:
# [YouTube video URL] - shows title and time length for the URL
#
# Notes:
# For text-based adapters like IRC.
#
# Author:
# mmb
querystring = require 'querystring'
url = require 'url'
module.exports = (robot) ->
robot.hear /(https?:\/\/www\.youtube\.com\/watch\?.+?)(?:\s|$)/i, (msg) ->
url_parsed = url.parse(msg.match[1])
query_parsed = querystring.parse(url_parsed.query)
if query_parsed.v
video_hash = query_parsed.v
showInfo msg, video_hash
robot.hear /(https?:\/\/youtu\.be\/)([a-z0-9\-_]+)/i, (msg) ->
video_hash = msg.match[2]
showInfo msg, video_hash
showInfo = (msg, video_hash) ->
msg.http("http://gdata.youtube.com/feeds/api/videos/#{video_hash}")
.query({
alt: 'json'
}).get() (err, res, body) ->
if res.statusCode is 200
data = JSON.parse(body)
entry = data.entry
r = entry.gd$rating
thumbs_up = Math.round(((r.average-r.min)/(r.max-r.min))*r.numRaters)
thumbs_down = r.numRaters - thumbs_up
msg.send "YouTube: #{entry.title.$t} (#{formatTime(entry.media$group.yt$duration.seconds)}, #{humanizeNumber(entry.yt$statistics.viewCount)} views, #{humanizeNumber(thumbs_up)} thumbs up, #{humanizeNumber(thumbs_down)} thumbs down)"
else
msg.send "YouTube: error: #{video_hash} returned #{res.statusCode}: #{body}"
formatTime = (seconds) ->
min = Math.floor(seconds / 60)
sec = seconds % 60
result = ''
if (min > 0)
result += "#{min}m"
if (sec > 0)
result += "#{sec}s"
result
humanizeNumber = (n) ->
n = n.toString()
while true
n2 = n.replace(/(\d)(\d{3})($|,)/g, '$1,$2$3')
break if n == n2
n = n2
n