package edu.kit.ksri.paperfinder.scholar.http;

import edu.kit.ksri.paperfinder.Config;
import edu.kit.ksri.paperfinder.scholar.util.FileUtils;

/**
 * Created by janscheurenbrand on 28.11.14.
 */
public class ContentRetriever {

    /**
     * @param searchTerm The term you want to search for
     * @param i Index of the result page (= offset, if multiplied with n), starting at 0!
     * @param n Number of results per page. MUST be between 10 and 20
     * @param includePatents Whether to include patents in the searach results
     * @param includeCitations Whether to include patents in the searach results
     * @return
     */
    public String getSingleResultPage(String searchTerm, int i, int n, boolean includePatents, boolean includeCitations) {

        // To reduce real requests to Google while testing
        if (Config.TEST_MODE) {
            return FileUtils.readFile(getClass().getResource("/results.html").getFile());
        }

        Request request = new Request(Verb.GET, Config.BASE_DOMAIN.concat("/scholar"));
        request.addURIParam("q",searchTerm);
        request.addURIParam("num", String.valueOf(n));
        if (i > 0) {
            request.addURIParam("start", String.valueOf(n*i));
        }
        request.addURIParam("as_sdt", (includePatents ? "0,5" : "1,5"));
        request.addURIParam("as_vis", (includeCitations ? "0" : "1"));

        Response response = request.execute();
        return response.getResponseBody();
    }

}

@@ -138,16 +138,90 @@
 Utils;%0A%0A
+import java.io.UnsupportedEncodingException;%0Aimport java.net.URLEncoder;%0A%0A
 /**%0A * C
@@ -1105,24 +1105,42 @@
 scholar%22));%0A
+        try %7B%0A    
         requ
@@ -1163,20 +1163,143 @@
 %22q%22,
-searchTerm);
+ URLEncoder.encode(searchTerm, %22UTF-8%22));%0A        %7D catch (UnsupportedEncodingException e) %7B%0A            e.printStackTrace();%0A        %7D
 %0A   

Encode Search Query