-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHtmlParser.java
More file actions
94 lines (87 loc) · 2.39 KB
/
HtmlParser.java
File metadata and controls
94 lines (87 loc) · 2.39 KB
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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;
import java.util.ArrayList;
import java.util.Scanner;
public class HtmlParser implements Module{
private String command = "toggletitles";
private Message m;
private boolean admin;
private boolean on = true;
public void parse(Message m, boolean admin){
this.m = m;
this.admin = admin;
}
public String[] outputs(){
String target = "PRIVMSG " + m.getParam();
if(!m.getParam().startsWith("#")) target = "NOTICE " + m.getSender();
String[] messageSplit = m.getTrailing().split("\\s+");
if(m.isBotCommand() && admin){
if(m.getBotCommand().equals(command)){
String[] outputs = new String[1];
if(on) outputs[0] = target + " :Title reporting is now off";
else outputs[0] = target + " :Title reporting is now on";
on = !on;
return outputs;
}
}
if(!on)return null;
for(int i = 0; i < messageSplit.length; i++){
if(messageSplit[i].startsWith("http://") || messageSplit[i].startsWith("https://")){
try {
URL url = new URL(messageSplit[i]);
Scanner scan = new Scanner(url.openStream());
String title = "";
boolean titlefound = false;
while(scan.hasNext()){
String next = scan.next();
if(next.contains("<title>")){
titlefound = true;
next = next.split("<title")[1];
}
else if(next.contains("<title")){
titlefound = true;
next = next.split("<title")[1];
/*
while(scan.hasNext()){
next = scan.next();
if(next.contains(">")){
next = next.split(">")[1];
break;
}
}
*/
}
if(titlefound){
if(next.contains("</title>")){
next = next.split("</title>")[0];
title += next + " ";
break;
}
else title += next + " ";
if(title.split("\\s+").length > 20){
title += "...";
break;
}
}
}
if(title.length() > 100){
title = title.substring(0, 95) + "...";
}
String[] outputs = new String[1];
title = title.substring(1);
outputs[0] = (target + " :[URL] " + title + "(" + url.getHost() + ")");
return outputs;
}
catch(Exception e){
String[] outputs = new String[1];
e.printStackTrace();
outputs[0] = (target + " :Could not read url, " + e);
return outputs;
}
}
}
return null;
}
}