|
@@ -0,0 +1,99 @@
|
|
1
|
+/*
|
|
2
|
+ * To change this template, choose Tools | Templates
|
|
3
|
+ * and open the template in the editor.
|
|
4
|
+ */
|
|
5
|
+
|
|
6
|
+package com.md87.charliebravo.commands;
|
|
7
|
+
|
|
8
|
+import com.dmdirc.util.Downloader;
|
|
9
|
+import com.md87.charliebravo.Command;
|
|
10
|
+import com.md87.charliebravo.InputHandler;
|
|
11
|
+import com.md87.charliebravo.Response;
|
|
12
|
+import java.net.URLEncoder;
|
|
13
|
+import java.nio.charset.Charset;
|
|
14
|
+import java.util.ArrayList;
|
|
15
|
+import java.util.List;
|
|
16
|
+import java.util.regex.Matcher;
|
|
17
|
+import java.util.regex.Pattern;
|
|
18
|
+
|
|
19
|
+/**
|
|
20
|
+ *
|
|
21
|
+ * @author chris
|
|
22
|
+ */
|
|
23
|
+public class LawCommand implements Command {
|
|
24
|
+
|
|
25
|
+ protected static final String URL = "http://www.statutelaw.gov.uk/SearchResults.aspx?TYPE=QS&Title=%s&Year=%s&Number=%s&LegType=All+Legislation";
|
|
26
|
+
|
|
27
|
+ protected static final Pattern YEAR_EXTRACTOR = Pattern.compile("((1[2-9]|20)[0-9]{2})");
|
|
28
|
+ protected static final Pattern NUMBER_EXTRACTOR = Pattern.compile("c\\.?\\s*([0-9]+)");
|
|
29
|
+
|
|
30
|
+ protected static final Pattern TITLE_MATCHER = Pattern.compile(".*?<a href=\"legResults.*?activeTextDocId=([0-9]+).*?\">(.*?)</a>.*?");
|
|
31
|
+ protected static final Pattern YEAR_MATCHER = Pattern.compile(".*?<td class=\"year\">(.*?)</td>.*?");
|
|
32
|
+ protected static final Pattern TYPE_MATCHER = Pattern.compile(".*?<td class=\"type\">(.*?)</td>.*?");
|
|
33
|
+
|
|
34
|
+ public void execute(InputHandler handler, Response response, String line) throws Exception {
|
|
35
|
+ if (line.isEmpty()) {
|
|
36
|
+ response.sendMessage("You need to specify some search terms", true);
|
|
37
|
+ } else {
|
|
38
|
+ String title = line, year = "", number = "";
|
|
39
|
+ Matcher matcher;
|
|
40
|
+
|
|
41
|
+ if ((matcher = YEAR_EXTRACTOR.matcher(title)).find()) {
|
|
42
|
+ year = matcher.group(1);
|
|
43
|
+ title = title.replace(matcher.group(), "");
|
|
44
|
+ }
|
|
45
|
+
|
|
46
|
+ if ((matcher = NUMBER_EXTRACTOR.matcher(title)).find()) {
|
|
47
|
+ number = matcher.group(1);
|
|
48
|
+ title = title.replace(matcher.group(), "");
|
|
49
|
+ }
|
|
50
|
+
|
|
51
|
+ title = URLEncoder.encode(title.replaceAll("\\s+", " ").trim(), Charset.defaultCharset().name());
|
|
52
|
+
|
|
53
|
+ final List<String> result = Downloader.getPage(String.format(URL, title, year, number));
|
|
54
|
+ final List<Result> results = new ArrayList<Result>();
|
|
55
|
+
|
|
56
|
+ String resDocId = null, resTitle = null, resYear = null, resType = null;
|
|
57
|
+ for (String resline : result) {
|
|
58
|
+ if ((matcher = TITLE_MATCHER.matcher(resline)).matches()) {
|
|
59
|
+ resDocId = matcher.group(1);
|
|
60
|
+ resTitle = matcher.group(2);
|
|
61
|
+ } else if ((matcher = YEAR_MATCHER.matcher(resline)).matches()) {
|
|
62
|
+ resYear = matcher.group(1);
|
|
63
|
+ } else if ((matcher = TYPE_MATCHER.matcher(resline)).matches()) {
|
|
64
|
+ resType = matcher.group(1);
|
|
65
|
+ results.add(new Result(resDocId, resTitle, resYear, resType));
|
|
66
|
+ }
|
|
67
|
+ }
|
|
68
|
+
|
|
69
|
+ if (results.isEmpty()) {
|
|
70
|
+ response.sendMessage("There were no results for that query", true);
|
|
71
|
+ } else {
|
|
72
|
+ response.sendMessage("the first result is: " + results.get(0));
|
|
73
|
+ }
|
|
74
|
+ }
|
|
75
|
+
|
|
76
|
+ }
|
|
77
|
+
|
|
78
|
+ protected static class Result {
|
|
79
|
+
|
|
80
|
+ protected static final String LINK = "http://www.statutelaw.gov.uk/legResults.aspx?activeTextDocId=%s&PageNumber=1";
|
|
81
|
+
|
|
82
|
+ private final String docId, title, year, type;
|
|
83
|
+
|
|
84
|
+ public Result(String docId, String title, String year, String type) {
|
|
85
|
+ this.docId = docId;
|
|
86
|
+ this.title = title;
|
|
87
|
+ this.year = year;
|
|
88
|
+ this.type = type;
|
|
89
|
+ }
|
|
90
|
+
|
|
91
|
+ @Override
|
|
92
|
+ public String toString() {
|
|
93
|
+ return title + " (" + type + "; " + year + "). See "
|
|
94
|
+ + String.format(LINK, docId);
|
|
95
|
+ }
|
|
96
|
+
|
|
97
|
+ }
|
|
98
|
+
|
|
99
|
+}
|