-
Notifications
You must be signed in to change notification settings - Fork 3
/
yt.jsm
99 lines (79 loc) · 2.47 KB
/
yt.jsm
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// -*- Mode: JavaScript; tab-width: 4 -*- vim:tabstop=4 syntax=javascript:
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*jshint es5: true, esnext: true, expr: true */
/*global Stream: false, aucgbot: false, module: false, system: false */
module.version = 2.6;
module.cmd_ytv = module.cmd_ytid =
function cmd_ytv(e) {
var args = e.args;
if (!/^(?:(?:https?:\/\/)?(?:www\.)?(?:youtube\.com\/(?:watch\?v=|(?:v|embed)\/)|youtu\.be\/))?([\w\-]+)(?:[?&#].*)?$/i.test(args)) {
e.reply("Get info about a YouTube video. Usage: ytv <link|id>");
return true;
}
var id = RegExp.$1, data;
try {
data = aucgbot.getJSON("http://gdata.youtube.com/feeds/api/videos/" + id + "?v=2&alt=jsonc", "yt", this.version).data;
} catch (ex) {}
if (!data) {
e.reply("YouTube returned no data.");
return true;
}
var res = this.ytRes(data);
if (id === args)
res.push("https://youtu.be/" + id);
e.reply(res.join(" - "));
return true;
};
module.cmd_yt = module.cmd_youtube =
function cmd_yt(e) {
var q = e.args;
if (!q) {
e.reply("Get the first result of a YouTube search.");
return true;
}
var data;
try {
data = aucgbot.getJSON("http://gdata.youtube.com/feeds/api/videos?v=2&alt=jsonc&max-results=1&q=" + encodeURIComponent(q), "yt", this.version).data;
} catch (ex) {}
if (!data) {
e.reply("YouTube returned no data.");
return true;
}
if (!data.totalItems) {
e.reply("No results.");
return true;
}
var item = data.items[0], res = this.ytRes(item);
res.push("https://youtu.be/" + item.id);
e.reply(res.join(" - "));
return true;
};
module.ytRes = function ytRes(data) {
var res = [data.title, data.uploader];
{
let dura = data.duration,
f = function f(n) n < 10 ? "0" + n : n,
m = f(Math.floor((dura % 3600) / 60)),
s = f(dura % 60),
h = Math.floor(dura / 3600);
res.push((h ? h + ":" : "") + m + ":" + s);
}
var desc = data.description;
if (desc) {
let i = desc.indexOf("\n");
if (i !== -1)
desc = desc.slice(0, i);
res.push(desc);
}
var rating = data.rating;
if (rating)
res.push("{0}/5 (+{1} -{2})".format(+rating.toFixed(2), data.likeCount, data.ratingCount - data.likeCount));
res.push(data.viewCount + " views");
var commentCount = data.commentCount;
if (commentCount)
res.push(commentCount === 1 ? "1 comment" : commentCount + " comments");
res.push(data.category);
return res;
};