|
| 1 | +import org.json.JSONArray; |
| 2 | +import org.json.JSONObject; |
| 3 | +import org.json.XML; |
| 4 | + |
| 5 | +import java.io.IOException; |
| 6 | +import java.net.URL; |
| 7 | +import java.net.URLEncoder; |
| 8 | +import java.util.Scanner; |
| 9 | + |
| 10 | +class DataHandler { |
| 11 | + |
| 12 | + /** |
| 13 | + * @param query keyword for the wikidata query |
| 14 | + * @param lang language for wikidata dump |
| 15 | + * @return entityID |
| 16 | + */ |
| 17 | + private static String queryToEntity(String query, String lang) { |
| 18 | + try { |
| 19 | + //Get the data from the wikidata api using a search word and a language |
| 20 | + Scanner scanner = new Scanner(new URL("https://www.wikidata.org/w/api.php?action=wbsearchentities&format=json&uselang=" + lang + "&search=" + query + "&language=" + lang).openStream()); |
| 21 | + StringBuilder stringBuilder = new StringBuilder(); |
| 22 | + while (scanner.hasNextLine()) { |
| 23 | + stringBuilder.append(scanner.nextLine()); |
| 24 | + } |
| 25 | + scanner.close(); |
| 26 | + |
| 27 | + //Convert the data to a JSONObject and return the entities id |
| 28 | + JSONObject jsonObject = new JSONObject(stringBuilder.toString()).getJSONArray("search").getJSONObject(0); |
| 29 | + Log.success("Received the entities id"); |
| 30 | + return jsonObject.getString("id"); |
| 31 | + |
| 32 | + } catch (IOException e) { |
| 33 | + e.printStackTrace(); |
| 34 | + } |
| 35 | + return null; |
| 36 | + } |
| 37 | + |
| 38 | + static JSONObject queryCall(String query, String lang){ |
| 39 | + //Get the entities id based on a query |
| 40 | + String entityId = queryToEntity(query, lang); |
| 41 | + String queryAufruf = "SELECT ?Name ?EinwohnerZahl ?Landeswappen ?Karte ?Bild ?Koordinaten ?Flagge ?description WHERE {\n" + |
| 42 | + " wd:"+entityId+" wdt:P1448 ?Name." + |
| 43 | + " OPTIONAL { wd:"+entityId+" wdt:P1082 ?EinwohnerZahl. }" + |
| 44 | + " OPTIONAL { wd:"+entityId+" wdt:P94 ?Landeswappen. }" + |
| 45 | + " OPTIONAL { wd:"+entityId+" wdt:P242 ?Karte. }" + |
| 46 | + " OPTIONAL { wd:"+entityId+" wdt:P18 ?Bild. }" + |
| 47 | + " OPTIONAL { wd:"+entityId+" wdt:P625 ?Koordinaten. }" + |
| 48 | + " OPTIONAL { wd:"+entityId+" wdt:P41 ?Flagge. }" + |
| 49 | + " OPTIONAL { wd:"+entityId+" schema:description ?description." + |
| 50 | + " FILTER(LANG(?description)=\"de\")}" + |
| 51 | + " SERVICE wikibase:label { bd:serviceParam wikibase:language \"" + lang + "\". }" + |
| 52 | + "}"; |
| 53 | + try { |
| 54 | + //Encode the query to be used in an url |
| 55 | + String queryEncoded = URLEncoder.encode(queryAufruf, "UTF-8"); |
| 56 | + |
| 57 | + //Declare the scanner to later get data from wikidata |
| 58 | + Scanner scanner = new Scanner(new URL("https://query.wikidata.org/sparql?query=" + queryEncoded).openStream()); |
| 59 | + |
| 60 | + //Get all the data to be stored in a StringBuilder |
| 61 | + StringBuilder stringBuilder = new StringBuilder(); |
| 62 | + while (scanner.hasNextLine()) { |
| 63 | + stringBuilder.append(scanner.nextLine()); |
| 64 | + } |
| 65 | + scanner.close(); |
| 66 | + //return the data as a JSONObject |
| 67 | + String s = stringBuilder.toString(); |
| 68 | + return XML.toJSONObject(s); |
| 69 | + |
| 70 | + |
| 71 | + } catch (IOException e) { |
| 72 | + e.printStackTrace(); |
| 73 | + } |
| 74 | + return null; |
| 75 | + } |
| 76 | + |
| 77 | + static JSONObject convertJSON(JSONObject wikiData) { |
| 78 | + JSONObject result = wikiData.getJSONObject("sparql").getJSONObject("results"); |
| 79 | + JSONObject resultObject = result.optJSONObject("result"); |
| 80 | + if(resultObject == null){ |
| 81 | + resultObject = result.getJSONArray("result").getJSONObject(0); |
| 82 | + } |
| 83 | + JSONArray results = resultObject.getJSONArray("binding"); |
| 84 | + |
| 85 | + JSONObject resultsObject = new JSONObject(); |
| 86 | + |
| 87 | + for (int i = 0; i < results.length(); i++) { |
| 88 | + JSONObject jsonObject = results.getJSONObject(i); |
| 89 | + |
| 90 | + String key = jsonObject.getString("name"); |
| 91 | + String value; |
| 92 | + |
| 93 | + if(jsonObject.has("literal")) { |
| 94 | + value = String.valueOf(jsonObject.getJSONObject("literal").get("content")); |
| 95 | + } else { |
| 96 | + value = jsonObject.getString("uri"); |
| 97 | + } |
| 98 | + |
| 99 | + resultsObject.put(key, value); |
| 100 | + Log.status("added "+key); |
| 101 | + } |
| 102 | + |
| 103 | + JSONObject newWikiDataset = new JSONObject(); |
| 104 | + newWikiDataset.put("head", wikiData.getJSONObject("sparql").getJSONObject("head").getJSONArray("variable")); |
| 105 | + newWikiDataset.put("results", resultsObject); |
| 106 | + Log.success("formatting succesfull"); |
| 107 | + return newWikiDataset; |
| 108 | + } |
| 109 | +} |
0 commit comments