commit 6fcb266c17fe4aa6c18294332a3a483ba090af75 Author: Matthew Grove Date: Mon Oct 31 10:17:01 2022 +0000 Add challenge 1 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..9430479 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,22 @@ + +The MIT License (MIT) + +Copyright (c) 2022 Matthew Grove + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..cfc9acc --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# uos-comp-space-cadets diff --git a/SCChallengeEmail/.idea/.gitignore b/SCChallengeEmail/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/SCChallengeEmail/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/SCChallengeEmail/.idea/libraries/google_code_gson.xml b/SCChallengeEmail/.idea/libraries/google_code_gson.xml new file mode 100644 index 0000000..dd7a0ae --- /dev/null +++ b/SCChallengeEmail/.idea/libraries/google_code_gson.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/SCChallengeEmail/.idea/misc.xml b/SCChallengeEmail/.idea/misc.xml new file mode 100644 index 0000000..03f397c --- /dev/null +++ b/SCChallengeEmail/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/SCChallengeEmail/.idea/modules.xml b/SCChallengeEmail/.idea/modules.xml new file mode 100644 index 0000000..78dd928 --- /dev/null +++ b/SCChallengeEmail/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/SCChallengeEmail/.idea/vcs.xml b/SCChallengeEmail/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/SCChallengeEmail/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/SCChallengeEmail/SCChallengeEmail.iml b/SCChallengeEmail/SCChallengeEmail.iml new file mode 100644 index 0000000..b5d0958 --- /dev/null +++ b/SCChallengeEmail/SCChallengeEmail.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/SCChallengeEmail/lib/gson-2.9.1.jar b/SCChallengeEmail/lib/gson-2.9.1.jar new file mode 100644 index 0000000..8a663ec Binary files /dev/null and b/SCChallengeEmail/lib/gson-2.9.1.jar differ diff --git a/SCChallengeEmail/src/GetPersonDetails.java b/SCChallengeEmail/src/GetPersonDetails.java new file mode 100644 index 0000000..30a7f87 --- /dev/null +++ b/SCChallengeEmail/src/GetPersonDetails.java @@ -0,0 +1,76 @@ +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.URL; +import java.util.HashMap; + +public class GetPersonDetails { + public static void getName(String userId) { + try { + String parseLine = ""; + URL url = new URL("https://www.ecs.soton.ac.uk/people/" + userId); + BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream())); + StringBuilder result = new StringBuilder(); + String userEmail = userId+"@soton.ac.uk"; + while (!parseLine.contains(userEmail)) { + parseLine = br.readLine(); + result.append(parseLine); + } + int additionalOpenedBrackets = 1; + while (!parseLine.contains("}") || additionalOpenedBrackets > 0) { + parseLine = br.readLine(); + if (parseLine.contains("{")) additionalOpenedBrackets++; + if (parseLine.contains("}")) additionalOpenedBrackets--; + result.append(parseLine); + } + br.close(); + int userDataStart = result.substring(0,result.lastIndexOf(userEmail)).lastIndexOf("{"); + int addressStart = result.indexOf("\"address\":") + 11; + int imageStart = result.indexOf("\"image\":") + 9; + String userDataString = result.substring(userDataStart,userDataStart + result.substring(userDataStart,userDataStart + result.substring(userDataStart + 1).indexOf("{")).lastIndexOf(",")) + "}"; + String addressDataString = result.substring(addressStart,addressStart + result.substring(addressStart).indexOf("}")) + "}"; + String imageDataString = result.substring(imageStart,imageStart + result.substring(imageStart).indexOf("}")) + "}"; + int imageUrlStart = imageDataString.indexOf("\"url\": \"") + 8; + String imageUrl = imageDataString.substring(imageUrlStart,imageUrlStart + imageDataString.substring(imageUrlStart).indexOf("}") - 1).replace("\"","").strip(); + HashMap userData = new Gson().fromJson(userDataString, new TypeToken>() {}.getType()); + HashMap addressData = new Gson().fromJson(addressDataString, new TypeToken>() {}.getType()); + userData.put("imageUrl","https://www.southampton.ac.uk" + imageUrl); + printSeparator(); + System.out.println("User Data:"); + for (String key: userData.keySet()) { + System.out.println(key + ": " + userData.get(key)); + } + printSeparator(2, ""); + System.out.println("Address Data:"); + for (String key: addressData.keySet()) { + System.out.println(key + ": " + addressData.get(key)); + } + printSeparator(); + } catch (IOException err) { + System.out.println(err); + } + } + + public static void printSeparator() { + System.out.println("###############################"); + } + + public static void printSeparator(Integer count) { + for (int i=1; i <= count; i++) { + printSeparator(); + } + } + + public static void printSeparator(Integer count, String separator) { + for (int i=1; i <= count; i++) { + printSeparator(); + if (i != count) System.out.println(separator); + } + } + + public static void main(String[] args) { + getName("dem"); + } +}