lab5: add development files, not ready for production
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
package lab5lib;
|
||||
|
||||
//import java.net.URL;
|
||||
//import java.io.BufferedInputStream;
|
||||
import java.util.Scanner;
|
||||
import java.lang.Exception;
|
||||
//import javax.json.JsonObject;
|
||||
|
||||
public class Fetcher {
|
||||
//public void main() {}
|
||||
|
||||
/*
|
||||
private static String fetchString(String remote_url) {
|
||||
try {
|
||||
Scanner reader = new Scanner(new URL(remote_url).openStream(), "UTF-8");
|
||||
String jsonString = "";
|
||||
|
||||
if (reader.hasNextLine()) {
|
||||
jsonString = reader.nextLine();
|
||||
}
|
||||
|
||||
return jsonString;
|
||||
} catch (Exception e) {
|
||||
System.out.println("[ERROR] Failed to fetch resource from " + remote_url + " due to the following exception: " + e);
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Allows to fetch unique strings directly from the search engine
|
||||
*
|
||||
* @since 0.2
|
||||
*
|
||||
* @throws Exception in case if the server fails to provide the content
|
||||
*/
|
||||
public static String fetchTextFromPython() throws Exception {
|
||||
try {
|
||||
Process contentFetcher = Runtime.getRuntime().exec("python3 lab3lib/fetchContent.py");
|
||||
Scanner reader = new Scanner(contentFetcher.getInputStream());
|
||||
return new String(reader.nextLine());
|
||||
} catch (Exception e) {
|
||||
throw new Exception("Failed to fetch content from the server");
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
public StringBuilder fetchText(String request) {
|
||||
String responce = fetchString("http://10.1.1.2:8080/search?language=en-US&format=json&q=" + request);
|
||||
JSONObject results = new JSONObject(responce);
|
||||
return results.get("results").get(0).get("content");
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
//JSONObject results = 'http://10.1.1.2:8080/search?q=test&language=en-US&format=json'
|
||||
@@ -0,0 +1,10 @@
|
||||
import requests
|
||||
from random import randint
|
||||
import json
|
||||
|
||||
r = requests.get("http://10.1.1.2:8080/search?q=test&format=json&language=en-US")
|
||||
|
||||
results = json.loads(r.text)["results"]
|
||||
final_content = results[randint(0, len(results))]['content']
|
||||
|
||||
print(final_content)
|
||||
Reference in New Issue
Block a user