-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTitle.java
88 lines (71 loc) · 2.12 KB
/
Title.java
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
import java.io.*;
import java.net.*;
import java.util.regex.*;
import net.GUIpsp.GuiBot.*;
public class Title extends BasePlugin {
public void main() throws Throwable {
registerCmd(
"title",
getClass().getMethod("getTitle", String.class, String.class,
String.class, String.class, String.class), this,
"USAGE: " + GuiBot.pref + "title <url> - gets <url>'s title");
}
public String version() {
return "V1";
}
public String pluginName() {
return "Title";
}
public String author() {
return "GUIpsp";
}
public void getTitle(String channel, String sender, String login,
String hostname, String message) throws Throwable {
if (!(message.trim() == "")) {
if (!message.toLowerCase().trim().startsWith("http://")) {
message = "http://" + message;
}
HttpURLConnection httpUrlConnection = (HttpURLConnection) new URL(
message).openConnection();
httpUrlConnection.setRequestMethod("HEAD");
httpUrlConnection.connect();
httpUrlConnection.setReadTimeout(3000);
if (httpUrlConnection.getHeaderField("Content-Type").toLowerCase()
.matches(".*html.*")) {
try {
URLConnection urlConnection = new URL(message)
.openConnection();
BufferedReader reader = new BufferedReader(
new InputStreamReader(
urlConnection.getInputStream()));
String html = "", tmp = "";
while ((tmp = reader.readLine()) != null) {
html += " " + tmp;
}
reader.close();
html = html.replaceAll("\\s+", " ");
Pattern p = Pattern.compile("<title>(.*?)</title>");
Matcher m = p.matcher(html);
while (m.find() == true) {
Main.bot.sendMessage(channel, sender + ": "
+ m.group(1).trim());
}
} catch (UnknownHostException e) {
Main.bot.sendMessage(channel, sender + ": No.");
}
} else {
Main.bot.sendMessage(
channel,
sender
+ ": "
+ message
+ " is not HTML, but it's content type is "
+ httpUrlConnection
.getHeaderField("Content-Type")
+ " and it's content length is "
+ httpUrlConnection
.getHeaderField("Content-Length"));
}
}
}
}