Skip to content

Commit 7cda886

Browse files
committed
Fix the Image.sc search plugin
It seems that Discourse has changed their API, and in particular made it stricter with respect to needing an API key.
1 parent 33159f6 commit 7cda886

File tree

2 files changed

+89
-3
lines changed

2 files changed

+89
-3
lines changed

src/main/java/org/scijava/search/web/ImageScSearcher.java

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,16 @@
2929
package org.scijava.search.web;
3030

3131
import com.google.gson.JsonArray;
32-
import com.google.gson.JsonElement;
3332
import com.google.gson.JsonObject;
3433
import com.google.gson.JsonStreamParser;
3534

3635
import java.io.BufferedReader;
36+
import java.io.DataOutputStream;
3737
import java.io.IOException;
3838
import java.io.InputStream;
3939
import java.io.InputStreamReader;
4040
import java.io.UnsupportedEncodingException;
41+
import java.net.HttpURLConnection;
4142
import java.net.MalformedURLException;
4243
import java.net.URL;
4344
import java.net.URLEncoder;
@@ -62,7 +63,7 @@
6263
@Plugin(type = Searcher.class, enabled = false)
6364
public class ImageScSearcher implements Searcher {
6465

65-
private static String URL_PREFIX = "https://forum.image.sc/search/query.json?term=";
66+
private static String URL_PREFIX = "https://forum.image.sc/search.json?q=";
6667
private static String POST_URL_PREFIX = "https://forum.image.sc/t";
6768
private static String FORUM_AVATAR_PREFIX = "https://forum.image.sc";
6869
private static String TERM_SUFFIX = " tags:imagej";
@@ -82,7 +83,15 @@ public List<SearchResult> search(String text, boolean fuzzy) {
8283

8384
try {
8485
final URL url = new URL(URL_PREFIX + URLEncoder.encode(text + TERM_SUFFIX, "utf-8"));
85-
InputStream is = url.openStream();
86+
87+
// Pass the API key as a header parameter.
88+
HttpURLConnection con = (HttpURLConnection) url.openConnection();
89+
con.setRequestMethod("GET");
90+
con.setRequestProperty("Api-Username", "imagesc-bot");
91+
con.setRequestProperty("Api-Key", "b1a28dbb29c385e06026482661c8de55dd01972ff993bea6547783e52e8a017d");
92+
93+
// Connect and read the result.
94+
InputStream is = con.getInputStream();
8695
try (InputStreamReader sr = new InputStreamReader(is, "UTF-8");
8796
BufferedReader reader = new BufferedReader(sr))
8897
{
@@ -138,4 +147,21 @@ private String formatDate(final String datestr) {
138147
return new PrettyTime().format(Date.from(instant));
139148
}
140149

150+
// Credit: https://www.baeldung.com/java-http-request
151+
public static String getParamsString(final Map<String, String> params)
152+
throws UnsupportedEncodingException
153+
{
154+
StringBuilder result = new StringBuilder();
155+
156+
for (Map.Entry<String, String> entry : params.entrySet()) {
157+
result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
158+
result.append("=");
159+
result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
160+
result.append("&");
161+
}
162+
163+
String resultString = result.toString();
164+
return resultString.length() > 0 ? //
165+
resultString.substring(0, resultString.length() - 1) : resultString;
166+
}
141167
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*-
2+
* #%L
3+
* Search framework for SciJava applications.
4+
* %%
5+
* Copyright (C) 2017 - 2023 SciJava developers.
6+
* %%
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright notice,
11+
* this list of conditions and the following disclaimer.
12+
* 2. Redistributions in binary form must reproduce the above copyright notice,
13+
* this list of conditions and the following disclaimer in the documentation
14+
* and/or other materials provided with the distribution.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
* #L%
28+
*/
29+
package org.scijava.search.web;
30+
31+
import static org.junit.Assert.assertEquals;
32+
33+
import java.util.List;
34+
35+
import org.junit.Test;
36+
import org.scijava.Context;
37+
import org.scijava.log.LogService;
38+
import org.scijava.search.SearchResult;
39+
40+
/**
41+
* Tests {@link ImageScSearcher}.
42+
*
43+
* @author Curtis Rueden
44+
*/
45+
public class ImageScSearcherTest {
46+
47+
@Test
48+
public void testSimpleSearch() {
49+
try (final Context ctx = new Context(LogService.class)) {
50+
final String query = "before:2015-12-31 in:title bonej";
51+
final ImageScSearcher searcher = new ImageScSearcher();
52+
ctx.inject(searcher);
53+
final List<SearchResult> results = searcher.search(query, false);
54+
assertEquals(3, results.size());
55+
assertEquals("https://forum.image.sc/t/251/1", results.get(0).properties().get("URL"));
56+
assertEquals("https://forum.image.sc/t/465/1", results.get(1).properties().get("URL"));
57+
assertEquals("https://forum.image.sc/t/162/1", results.get(2).properties().get("URL"));
58+
}
59+
}
60+
}

0 commit comments

Comments
 (0)