Commit 92cd8fea authored by  Qianyi  Shu's avatar Qianyi Shu
Browse files

add wiki bot

parent b58aac57
Pipeline #65758 canceled with stages
......@@ -22,6 +22,11 @@
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>net.sourceforge</groupId>
<artifactId>jwbf</artifactId>
<version>3.1.1</version>
</dependency>
</dependencies>
<build>
<plugins>
......
package ic.doc;
import static ic.doc.MarkDownPage.toMarkDown;
import ic.doc.web.NoneIndexPage;
import ic.doc.web.Page;
import java.io.*;
import javax.servlet.http.HttpServletResponse;
......@@ -13,7 +9,7 @@ public class PdfPage extends MarkDownPage {
}
public static File create_pdf(File markdown,String query) throws IOException{
public static File createPdf(File markdown, String query) throws IOException{
ProcessBuilder pb = new ProcessBuilder("pandoc",markdown.getName(), "-o",query + ".pdf");
File dir = new File("/tmp");
pb.directory(dir);
......@@ -31,10 +27,10 @@ public class PdfPage extends MarkDownPage {
resp.setContentType("application/pdf");
OutputStream writer = resp.getOutputStream();
File markdown = toMarkDown(answer, query);
File pdf = create_pdf(markdown, query);
File pdf = createPdf(markdown, query);
writer.write(new FileInputStream(pdf).readAllBytes());
writer.close();
boolean md = markdown.delete();
boolean pd = pdf.delete();
markdown.delete();
pdf.delete();
}
}
package ic.doc;
import net.sourceforge.jwbf.mediawiki.bots.MediaWikiBot;
public class QueryProcessor {
public MediaWikiBot wikiBot;
public QueryProcessor() {
wikiBot = new MediaWikiBot("https://en.wikipedia.org/w/");
}
public String process(String query) {
StringBuilder results = new StringBuilder();
if (query.toLowerCase().contains("shakespeare")) {
results.append("William Shakespeare (26 April 1564 - 23 April 1616) was an\n" +
"English poet, playwright, and actor, widely regarded as the greatest\n" +
......@@ -17,12 +26,6 @@ public class QueryProcessor {
"his works of hard science fiction and popular science. \n");
results.append(System.lineSeparator());
}
if (query.toLowerCase().contains("rory shu")) {
results.append("Rory Shu (9 March 1998 - ) is a Chinese JMC undergraduate\n" +
"living in London, famous for having a annoying bf (and his friend is\n"
+ "annoying too).\n");
results.append(System.lineSeparator());
}
if (query.toLowerCase().contains("nanfeng liu")) {
results.append("Nanfeng Liu (10 December 1998 - ) is a Chinese Computing undergraduate\n" +
......
......@@ -13,54 +13,54 @@ import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class WebServer {
public WebServer() throws Exception {
Server server = new Server(Integer.valueOf(System.getenv("PORT")));
ServletHandler handler = new ServletHandler();
handler.addServletWithMapping(new ServletHolder(new Website()), "/*");
server.setHandler(handler);
public WebServer() throws Exception {
Server server = new Server(Integer.valueOf(System.getenv("PORT")));
ServletHandler handler = new ServletHandler();
handler.addServletWithMapping(new ServletHolder(new Website()), "/*");
server.setHandler(handler);
server.start();
}
static class Website extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException{
String query = req.getParameter("q");
String type = req.getParameter("t");
Page p = execute(query, type);
try{
p.writeTo(resp);
}catch (InterruptedException e){
server.start();
}
}
}
static class Website extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String query = req.getParameter("q");
String type = req.getParameter("t");
Page p = execute(query, type);
try {
p.writeTo(resp);
} catch (InterruptedException e) {
}
}
}
public static Page execute(String query, String type) {
if (query == null) {
return new IndexPage();
} else {
if (query.length() <3){
query += " ";
public static Page execute(String query, String type) {
if (query == null) {
return new IndexPage();
} else {
if (query.length() < 3) {
query += " ";
/* java.lang.IllegalArgumentException: Prefix string
"" too short: length must be at least 3 */
}
String result = new QueryProcessor().process(query);
if(type == null){
return new HTMLResultPage(query, result);
}else if (type.equals("markdown")){
return new MarkDownPage(query, result);
}else{
return new PdfPage(query, result);
}
}
}
String result = new QueryProcessor().process(query);
if (type == null) {
return new HTMLResultPage(query, result);
} else if (type.equals("markdown")) {
return new MarkDownPage(query, result);
} else {
return new PdfPage(query, result);
}
}
}
public static void main(String[] args) throws Exception {
new WebServer();
}
public static void main(String[] args) throws Exception {
new WebServer();
}
}
......@@ -25,11 +25,6 @@ public class QueryProcessorTest {
assertThat(queryProcessor.process("Asimov"), containsString("science fiction"));
}
@Test
public void knowsAboutRory() throws Exception {
assertThat(queryProcessor.process("rory shu"), containsString("JMC"));
}
@Test
public void knowsAboutNanfeng() throws Exception {
assertThat(queryProcessor.process("nanfeng liu"), containsString("Computing"));
......
......@@ -10,6 +10,7 @@ import org.junit.Test;
public class doGetTest {
@Test
public void noRadioButtonReturnsHTML(){
Page p = execute("shakespeare", null);
......@@ -47,7 +48,7 @@ public class doGetTest {
assertTrue(p instanceof MarkDownPage);
}
//test that there is a markdown file created
// test that there is a markdown file created
@Test
public void markdownFileCreated() throws Exception {
String query = "shakespeare";
......@@ -59,14 +60,14 @@ public class doGetTest {
}
//test that there is a pdf file created
// test that there is a pdf file created
@Test
public void pdfFileCreated() throws Exception {
String query = "shakespeare";
String result = new QueryProcessor().process(query);
File markDown = MarkDownPage.toMarkDown(result, query);
File pdf = PdfPage.create_pdf(markDown, query);
File pdf = PdfPage.createPdf(markDown, query);
assertTrue(pdf.isFile());
}
......@@ -78,4 +79,5 @@ public class doGetTest {
p = execute("", null);
assertTrue(p.getQuery().equals(" "));
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment