Add challenge 1
This commit is contained in:
29
.gitignore
vendored
Normal file
29
.gitignore
vendored
Normal file
@@ -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
|
||||||
22
LICENSE.md
Normal file
22
LICENSE.md
Normal file
@@ -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.
|
||||||
8
SCChallengeEmail/.idea/.gitignore
generated
vendored
Normal file
8
SCChallengeEmail/.idea/.gitignore
generated
vendored
Normal file
@@ -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
|
||||||
10
SCChallengeEmail/.idea/libraries/google_code_gson.xml
generated
Normal file
10
SCChallengeEmail/.idea/libraries/google_code_gson.xml
generated
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
<component name="libraryTable">
|
||||||
|
<library name="google.code.gson" type="repository">
|
||||||
|
<properties maven-id="com.google.code.gson:gson:2.9.1" />
|
||||||
|
<CLASSES>
|
||||||
|
<root url="jar://$PROJECT_DIR$/lib/gson-2.9.1.jar!/" />
|
||||||
|
</CLASSES>
|
||||||
|
<JAVADOC />
|
||||||
|
<SOURCES />
|
||||||
|
</library>
|
||||||
|
</component>
|
||||||
6
SCChallengeEmail/.idea/misc.xml
generated
Normal file
6
SCChallengeEmail/.idea/misc.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_19" default="true" project-jdk-name="19" project-jdk-type="JavaSDK">
|
||||||
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
8
SCChallengeEmail/.idea/modules.xml
generated
Normal file
8
SCChallengeEmail/.idea/modules.xml
generated
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/SCChallengeEmail.iml" filepath="$PROJECT_DIR$/SCChallengeEmail.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
6
SCChallengeEmail/.idea/vcs.xml
generated
Normal file
6
SCChallengeEmail/.idea/vcs.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
12
SCChallengeEmail/SCChallengeEmail.iml
Normal file
12
SCChallengeEmail/SCChallengeEmail.iml
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="JAVA_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||||
|
<exclude-output />
|
||||||
|
<content url="file://$MODULE_DIR$">
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||||
|
</content>
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
<orderEntry type="library" name="google.code.gson" level="project" />
|
||||||
|
</component>
|
||||||
|
</module>
|
||||||
BIN
SCChallengeEmail/lib/gson-2.9.1.jar
Normal file
BIN
SCChallengeEmail/lib/gson-2.9.1.jar
Normal file
Binary file not shown.
76
SCChallengeEmail/src/GetPersonDetails.java
Normal file
76
SCChallengeEmail/src/GetPersonDetails.java
Normal file
@@ -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<String, String> userData = new Gson().fromJson(userDataString, new TypeToken<HashMap<String, String>>() {}.getType());
|
||||||
|
HashMap<String, String> addressData = new Gson().fromJson(addressDataString, new TypeToken<HashMap<String, String>>() {}.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");
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user