-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added search command for fetching from DuckDuckGo
- Loading branch information
1 parent
51c0cf1
commit 91faf56
Showing
7 changed files
with
135 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/main/java/su/knst/telegram/ai/utils/functions/FunctionSearchResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package su.knst.telegram.ai.utils.functions; | ||
|
||
import su.knst.telegram.ai.utils.functions.search.SearchInfo; | ||
|
||
import java.util.List; | ||
|
||
public class FunctionSearchResult extends FunctionResult { | ||
public List<SearchInfo> searchInfos; | ||
|
||
public FunctionSearchResult(List<SearchInfo> searchInfos) { | ||
super(true); | ||
|
||
this.searchInfos = searchInfos; | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/main/java/su/knst/telegram/ai/utils/functions/search/DDGSearch.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package su.knst.telegram.ai.utils.functions.search; | ||
|
||
import org.jsoup.Jsoup; | ||
import org.jsoup.nodes.Document; | ||
import org.jsoup.nodes.Element; | ||
import org.jsoup.select.Elements; | ||
|
||
import java.io.IOException; | ||
import java.net.URLEncoder; | ||
import java.nio.charset.Charset; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class DDGSearch { | ||
protected static final String url = "https://html.duckduckgo.com/html/?q="; | ||
|
||
public static List<SearchInfo> search(String request) throws IOException { | ||
Document doc =Jsoup.connect(url + URLEncoder.encode(request, Charset.defaultCharset())).get(); | ||
Elements results = doc.body().getElementsByClass("result"); | ||
|
||
return parseInfo(results); | ||
} | ||
|
||
protected static List<SearchInfo> parseInfo(Elements elements) { | ||
ArrayList<SearchInfo> searchInfos = new ArrayList<>(); | ||
|
||
for (Element element : elements) { | ||
SearchInfo searchInfo = new SearchInfo(); | ||
|
||
Elements title = element.getElementsByClass("result__title"); | ||
Elements url = element.getElementsByClass("result__url"); | ||
Elements description = element.getElementsByClass("result__snippet"); | ||
|
||
if (!title.isEmpty()) | ||
searchInfo.setTitle(title.text()); | ||
|
||
if (!url.isEmpty()) | ||
searchInfo.setUrl(url.get(0).attributes().get("href")); | ||
|
||
if (!description.isEmpty()) | ||
searchInfo.setDescription(description.text()); | ||
|
||
searchInfos.add(searchInfo); | ||
} | ||
|
||
return searchInfos; | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
src/main/java/su/knst/telegram/ai/utils/functions/search/SearchInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package su.knst.telegram.ai.utils.functions.search; | ||
|
||
public class SearchInfo { | ||
protected String title; | ||
protected String url; | ||
protected String description; | ||
|
||
public SearchInfo() { | ||
} | ||
|
||
public SearchInfo(String title, String url, String description) { | ||
this.title = title; | ||
this.url = url; | ||
this.description = description; | ||
} | ||
|
||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
public void setTitle(String title) { | ||
this.title = title; | ||
} | ||
|
||
public String getUrl() { | ||
return url; | ||
} | ||
|
||
public void setUrl(String url) { | ||
this.url = url; | ||
} | ||
|
||
public String getDescription() { | ||
return description; | ||
} | ||
|
||
public void setDescription(String description) { | ||
this.description = description; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "SearchInfo{" + | ||
"title='" + title + '\'' + | ||
", url='" + url + '\'' + | ||
", description='" + description + '\'' + | ||
'}'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters