implemented Google Search ability

pull/2/head
Vyacheslav N. Boyko 2018-02-06 12:25:30 +03:00
parent 20e89d624a
commit 4f8b402f6e
7 changed files with 327 additions and 116 deletions

View File

@ -5,7 +5,7 @@ After=network.target
[Service]
User=bvn13
WorkingDirectory=/srv/jircbot
ExecStart=/usr/bin/java -jar /srv/jircbot/jircbot-1.1.1.jar
ExecStart=/usr/bin/java -jar /srv/jircbot/jircbot-1.1.2.jar
SuccessExitStatus=143
[Install]

View File

@ -6,7 +6,7 @@
<groupId>ru.bvn13</groupId>
<artifactId>jircbot</artifactId>
<version>1.1.1</version>
<version>1.1.2</version>
<packaging>jar</packaging>

View File

@ -22,7 +22,6 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
@ -77,6 +76,9 @@ public class JircBot extends ListenerAdapter {
@Autowired
private GrammarCorrectorListener grammarCorrectorListener;
@Autowired
private GoogleSearchListener googleSearchListener;
@PostConstruct
public void postConstruct() {
this.executorService = Executors.newSingleThreadScheduledExecutor();
@ -118,10 +120,10 @@ public class JircBot extends ListenerAdapter {
.addListener(linkPreviewListener)
.addListener(helloOnJoinListener)
.addListener(grammarCorrectorListener)
.addListener(googleSearchListener)
// not tested
//.addListener(new GoogleDoodleListener(this.config))
//.addListener(new GoogleSearchListener(this.config))
//.addListener(new YandexSearchListener(this.config, this.yandexSearchService))
.setServers(servers)

View File

@ -71,5 +71,9 @@ public class ChannelSettings extends BaseModel {
@Column(nullable = false, columnDefinition = "Boolean DEFAULT False")
private Boolean grammarCorrectionEnabled = false;
@Getter
@Setter
@Column(nullable = false, columnDefinition = "Boolean DEFAULT False")
private Boolean googleSearchEnabled = false;
}

View File

@ -1,79 +1,93 @@
package ru.bvn13.jircbot.listeners;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.customsearch.Customsearch;
import com.google.api.services.customsearch.CustomsearchRequestInitializer;
import com.google.api.services.customsearch.model.Result;
import com.google.api.services.customsearch.model.Search;
import org.pircbotx.hooks.ListenerAdapter;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.pircbotx.hooks.types.GenericMessageEvent;
import ru.bvn13.jircbot.config.JircBotConfiguration;
import ru.bvn13.jircbot.model.GoogleSearchSettings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import ru.bvn13.jircbot.bot.ImprovedListenerAdapter;
import ru.bvn13.jircbot.database.services.ChannelSettingsService;
import ru.bvn13.jircbot.services.InternetAccessor;
import static ru.bvn13.jircbot.config.JircBotConfiguration.KEY_GOOGLE_SEARCH;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.List;
public class GoogleSearchListener extends ListenerAdapter {
/**
* Created by bvn13 on 06.02.2018.
*/
@Component
public class GoogleSearchListener extends ImprovedListenerAdapter {
private static final String COMMAND = "?search ";
private static final String COMMAND = "?gs";
private JircBotConfiguration config;
@Autowired
private InternetAccessor internetAccessor;
public GoogleSearchListener(JircBotConfiguration config) {
this.config = config;
}
@Autowired
private ChannelSettingsService channelSettingsService;
@Override
public void onGenericMessage(final GenericMessageEvent event) throws Exception {
if (!channelSettingsService.getChannelSettings(this.getChannelName(event)).getGoogleSearchEnabled()) {
return;
}
if (event.getUser().getUserId().equals(event.getBot().getUserBot().getUserId())) {
return;
}
if (!event.getMessage().startsWith(COMMAND.trim())) {
if (!event.getMessage().startsWith(COMMAND)) {
return;
}
Customsearch.Cse.List list = null;
String message = event.getMessage().replace(COMMAND, "").trim();
try {
GoogleSearchSettings settings = (GoogleSearchSettings) this.config.getListenersSettings().get(KEY_GOOGLE_SEARCH);
String query = event.getMessage().substring(COMMAND.length()).trim(); //The query to search
String uuid = settings.getUuid(); //Your search engine
String appKey = settings.getAppKey(); //Your application key
//Instance Customsearch
Customsearch customSearch = new Customsearch.Builder(GoogleNetHttpTransport.newTrustedTransport(), JacksonFactory.getDefaultInstance(), null)
.setApplicationName("JIrcBot")
.setGoogleClientRequestInitializer(new CustomsearchRequestInitializer(uuid))
.build();
//Set search parameter
list = customSearch.cse().list(query).setCx(uuid);
} catch (Exception ex) {
ex.printStackTrace();
event.respond("ERROR is occured while initialization, sorry.");
return;
}
//Execute search
Search result = null;
try {
result = list.execute();
} catch (Exception ex) {
ex.printStackTrace();
event.respond("ERROR is occured while searching, sorry.");
return;
}
if (result.getItems() != null) {
Result first = result.getItems().get(0);
event.respond(String.format("FOUND: %s (%s)", first.getTitle(), first.getLink()));
} else {
event.respond("NOT FOUND");
}
String result = search(message);
event.respond(result);
}
private String search(String phrase) throws UnsupportedEncodingException {
String encodedPhrase = URLEncoder.encode(phrase.replaceAll(" ", "+"), "utf-8");
String link = "https://google.ru/search?q="+encodedPhrase;
String queryPage = internetAccessor.retrieveContentByLink(link);
Document doc = Jsoup.parse(queryPage);
Elements searchResults = doc.select("#res #search #ires .g");
Element firstResult = searchResults.first();
Element descrElement = firstResult.select(".s .st").first();
String description = descrElement.text();
Element linkToRedirectPage = firstResult.select(".r a").first();
String linkTitle = linkToRedirectPage.text();
String redirectPage = internetAccessor.retrieveContentByLink("https://google.ru"+linkToRedirectPage.attr("href"));
String destinationUrl = null;
try {
Document redirectDoc = Jsoup.parse(redirectPage);
destinationUrl = redirectDoc.select("._jFe a").first().attr("href");
} catch (Exception e) {
try {
destinationUrl = internetAccessor.getLastUrl("https://google.ru"+linkToRedirectPage.attr("href"));
if (destinationUrl == null || destinationUrl.isEmpty()) {
destinationUrl = "https://google.ru"+linkToRedirectPage.attr("href");
}
} catch (Exception e1) {
destinationUrl = "https://google.ru"+linkToRedirectPage.attr("href");
}
}
return String.format("%s / %s / %s", URLDecoder.decode(destinationUrl, "utf-8"), linkTitle, description);
}
}

View File

@ -7,6 +7,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import ru.bvn13.jircbot.bot.ImprovedListenerAdapter;
import ru.bvn13.jircbot.database.services.ChannelSettingsService;
import ru.bvn13.jircbot.services.InternetAccessor;
import java.io.*;
import java.net.HttpURLConnection;
@ -28,6 +29,9 @@ import static java.lang.System.out;
@Component
public class LinkPreviewListener extends ImprovedListenerAdapter {
@Autowired
private InternetAccessor internetAccessor;
private static final Pattern REGEX = Pattern.compile("(?i)(?:(?:https?|ftp)://)(?:\\S+(?::\\S*)?@)?(?:(?!(?:10|127)(?:\\.\\d{1,3}){3})(?!(?:169\\.254|192\\.168)(?:\\.\\d{1,3}){2})(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))\\.?)(?::\\d{2,5})?(?:[/?#]\\S*)?");
@Autowired
@ -66,7 +70,7 @@ public class LinkPreviewListener extends ImprovedListenerAdapter {
private String parseLink(String link) throws Exception {
String content = retrieveContentByLink(link);
String content = internetAccessor.retrieveContentByLink(link);
String encoding = null; //getCharsetFromHeaders(content.toString());
if (encoding == null) {
@ -76,7 +80,7 @@ public class LinkPreviewListener extends ImprovedListenerAdapter {
String title = "";
if (encoding != null && !encoding.isEmpty()) {
content = retrieveContentByLinkWithEncoding(link, encoding);
content = internetAccessor.retrieveContentByLinkWithEncoding(link, encoding);
}
title = content.substring(content.indexOf("<title>") + 7, content.indexOf("</title>"));
@ -84,62 +88,6 @@ public class LinkPreviewListener extends ImprovedListenerAdapter {
return "Title: "+title.toString();
}
public String retrieveContentByLink(String link) {
return retrieveContentByLinkWithEncoding(link, "UTF-8");
}
public String retrieveContentByLinkWithEncoding(String link, String encoding) {
String url = ""+link;
StringBuffer content = new StringBuffer();
URL resourceUrl, base, next;
HttpURLConnection conn;
String location;
try {
while (true) {
resourceUrl = new URL(url);
conn = (HttpURLConnection) resourceUrl.openConnection();
conn.setConnectTimeout(15000);
conn.setReadTimeout(15000);
conn.setInstanceFollowRedirects(false); // Make the logic below easier to detect redirections
conn.setRequestProperty("User-Agent", "Mozilla/5.0...");
switch (conn.getResponseCode()) {
case HttpURLConnection.HTTP_MOVED_PERM:
case HttpURLConnection.HTTP_MOVED_TEMP:
location = conn.getHeaderField("Location");
location = URLDecoder.decode(location, "UTF-8");
base = new URL(url);
next = new URL(base, location); // Deal with relative URLs
url = next.toExternalForm();
continue;
}
break;
}
int status = conn.getResponseCode();
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), encoding));
String inputLine;
while ((inputLine = in.readLine()) != null) {
content.append(inputLine);
}
in.close();
conn.disconnect();
return content.toString();
} catch (Exception e) {
e.printStackTrace();
//throw new Exception("не могу получить совет для тебя");
}
return "";
}
public String decodeTitle_buffered(String title, String encoding) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();

View File

@ -0,0 +1,243 @@
package ru.bvn13.jircbot.services;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import javax.net.ssl.HttpsURLConnection;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLDecoder;
/**
* Created by bvn13 on 06.02.2018.
*/
@Component
public class InternetAccessor {
public String retrieveContentByLink(String link) {
return retrieveContentByLinkWithEncoding(link, "UTF-8");
}
public String retrieveContentByLinkWithEncoding(String link, String encoding) {
if (link.startsWith("https")) {
return retrieveContentByLinkWithEncoding_http(link, encoding);
} else {
return retrieveContentByLinkWithEncoding_https(link, encoding);
}
}
public String getLastUrl(String link) {
if (link.startsWith("https")) {
return getLastUrl_https(link);
} else {
return getLastUrl_http(link);
}
}
private String getLastUrl_http(String link) {
String url = ""+link;
StringBuffer content = new StringBuffer();
URL resourceUrl, base, next;
HttpURLConnection conn;
String location = link;
try {
while (true) {
if (location != null && location.startsWith("https://")) {
return getLastUrl_https(location);
}
resourceUrl = new URL(url);
conn = (HttpURLConnection) resourceUrl.openConnection();
conn.setConnectTimeout(15000);
conn.setReadTimeout(15000);
conn.setInstanceFollowRedirects(false); // Make the logic below easier to detect redirections
conn.setRequestProperty("User-Agent", "Mozilla/5.0...");
switch (conn.getResponseCode()) {
case HttpURLConnection.HTTP_MOVED_PERM:
case HttpURLConnection.HTTP_MOVED_TEMP:
location = conn.getHeaderField("Location");
location = URLDecoder.decode(location, "UTF-8");
base = new URL(url);
next = new URL(base, location); // Deal with relative URLs
url = next.toExternalForm();
continue;
}
break;
}
return location;
} catch (Exception e) {
e.printStackTrace();
//throw new Exception("не могу получить совет для тебя");
}
return "";
}
private String getLastUrl_https(String link) {
String url = ""+link;
StringBuffer content = new StringBuffer();
URL resourceUrl, base, next;
HttpsURLConnection conn;
String location = link;
try {
while (true) {
if (location != null && location.startsWith("http://")) {
return getLastUrl_http(location);
}
resourceUrl = new URL(url);
conn = (HttpsURLConnection) resourceUrl.openConnection();
conn.setConnectTimeout(15000);
conn.setReadTimeout(15000);
conn.setInstanceFollowRedirects(false); // Make the logic below easier to detect redirections
conn.setRequestProperty("User-Agent", "Mozilla/5.0...");
switch (conn.getResponseCode()) {
case HttpsURLConnection.HTTP_MOVED_PERM:
case HttpsURLConnection.HTTP_MOVED_TEMP:
location = conn.getHeaderField("Location");
location = URLDecoder.decode(location, "UTF-8");
base = new URL(url);
next = new URL(base, location); // Deal with relative URLs
url = next.toExternalForm();
continue;
}
break;
}
return location;
} catch (Exception e) {
e.printStackTrace();
//throw new Exception("не могу получить совет для тебя");
}
return "";
}
private String retrieveContentByLinkWithEncoding_http(String link, String encoding) {
String url = ""+link;
StringBuffer content = new StringBuffer();
URL resourceUrl, base, next;
HttpURLConnection conn;
String location = null;
try {
while (true) {
if (location != null && location.startsWith("https://")) {
return retrieveContentByLinkWithEncoding_https(location, encoding);
}
resourceUrl = new URL(url);
conn = (HttpURLConnection) resourceUrl.openConnection();
conn.setConnectTimeout(15000);
conn.setReadTimeout(15000);
conn.setInstanceFollowRedirects(false); // Make the logic below easier to detect redirections
conn.setRequestProperty("User-Agent", "Mozilla/5.0...");
switch (conn.getResponseCode()) {
case HttpURLConnection.HTTP_MOVED_PERM:
case HttpURLConnection.HTTP_MOVED_TEMP:
location = conn.getHeaderField("Location");
location = URLDecoder.decode(location, "UTF-8");
base = new URL(url);
next = new URL(base, location); // Deal with relative URLs
url = next.toExternalForm();
continue;
}
break;
}
int status = conn.getResponseCode();
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), encoding));
String inputLine;
while ((inputLine = in.readLine()) != null) {
content.append(inputLine);
}
in.close();
conn.disconnect();
return content.toString();
} catch (Exception e) {
e.printStackTrace();
//throw new Exception("не могу получить совет для тебя");
}
return "";
}
private String retrieveContentByLinkWithEncoding_https(String link, String encoding) {
String url = ""+link;
StringBuffer content = new StringBuffer();
URL resourceUrl, base, next;
HttpsURLConnection conn;
String location = null;
try {
while (true) {
if (location != null && location.startsWith("http://")) {
return retrieveContentByLinkWithEncoding_http(location, encoding);
}
resourceUrl = new URL(url);
conn = (HttpsURLConnection) resourceUrl.openConnection();
conn.setConnectTimeout(15000);
conn.setReadTimeout(15000);
conn.setInstanceFollowRedirects(false); // Make the logic below easier to detect redirections
conn.setRequestProperty("User-Agent", "Mozilla/5.0...");
switch (conn.getResponseCode()) {
case HttpsURLConnection.HTTP_MOVED_PERM:
case HttpsURLConnection.HTTP_MOVED_TEMP:
location = conn.getHeaderField("Location");
location = URLDecoder.decode(location, "UTF-8");
base = new URL(url);
next = new URL(base, location); // Deal with relative URLs
url = next.toExternalForm();
continue;
}
break;
}
int status = conn.getResponseCode();
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), encoding));
String inputLine;
while ((inputLine = in.readLine()) != null) {
content.append(inputLine);
}
in.close();
conn.disconnect();
return content.toString();
} catch (Exception e) {
e.printStackTrace();
//throw new Exception("не могу получить совет для тебя");
}
return "";
}
}