-
Notifications
You must be signed in to change notification settings - Fork 3
/
longurl.jsm
49 lines (39 loc) · 1.42 KB
/
longurl.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
// -*- 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/. */
/*global module: false */
module.version = 0.9;
module.cmd_longurl = function cmd_longurl(e) {
var url = e.args.replace(/^htt?p?(s)?:?\/\/?/i, "http$1://"); // typo correction
if (!url) {
e.reply(this.cmd_longurl.help);
return true;
}
if (url.slice(0, 4) != "http")
url = "http://" + url;
if (/^(http:\/\/v\.gd\/)(\w+)/.test(url)) {
// v.gd shortening service - use their lookup API as LongURL.org doesn't work
e.reply(e.bot.getHTTP(RegExp.$1 + "forward.php?format=simple&shorturl=" + RegExp.$2));
return true;
}
var data;
try {
data = e.bot.getJSON("http://api.longurl.org/v2/expand?content-type=1&title=1&rel-canonical=1&format=json&url=" + encodeURIComponent(url), "longurl", this.version);
} catch (ex) {}
if (!data) {
e.reply("No data found.");
return true;
}
if (data.messages) {
var msg0 = data.messages[0];
e.reply(msg0.type + ":", msg0.message);
return true;
}
var res = [data["rel-canonical"] || data["long-url"], data["content-type"]];
if (data.title)
res.push(data.title);
e.reply(res.join(" - "));
return true;
};
module.cmd_longurl.help = "Get the canonical long URL from LongURL.org. Usage: longurl <url>";