2017-06-04 20:15:04 +02:00
/ * *
* smmdbapi query
2017-06-05 17:16:39 +02:00
* api query , return all courses as ArrayList
2017-06-04 20:15:04 +02:00
* /
package application ;
import java.io.BufferedReader ;
import java.io.IOException ;
import java.io.InputStreamReader ;
import java.net.URL ;
import java.util.ArrayList ;
2017-08-30 11:29:58 +02:00
import org.apache.logging.log4j.LogManager ;
import org.apache.logging.log4j.Logger ;
2017-06-04 20:15:04 +02:00
import com.eclipsesource.json.Json ;
import com.eclipsesource.json.JsonArray ;
import com.eclipsesource.json.JsonValue ;
import datatypes.SmmdbApiDataType ;
public class SmmdbApiQuery {
2017-09-12 15:04:21 +02:00
//FIXME limit=70 as workaround for to long response, courseDataGz is to long
private String URL = " http://smmdb.ddns.net/api/getcourses?format=json&limit=70 " ;
2017-08-30 11:29:58 +02:00
private static final Logger LOGGER = LogManager . getLogger ( SmmdbApiQuery . class . getName ( ) ) ;
2017-06-04 20:15:04 +02:00
public SmmdbApiQuery ( ) {
//Auto-generated constructor stub
}
2017-08-26 00:13:09 +02:00
/ * *
* start smmdb api query
* @return a ArryList with all courses found at smmdb
* /
2017-06-04 20:15:04 +02:00
public ArrayList < SmmdbApiDataType > startQuery ( ) {
ArrayList < SmmdbApiDataType > course = new ArrayList < > ( ) ;
String output = " " ;
try {
2017-06-05 17:16:39 +02:00
URL apiUrl = new URL ( URL ) ;
2017-06-04 20:15:04 +02:00
BufferedReader ina = new BufferedReader ( new InputStreamReader ( apiUrl . openStream ( ) ) ) ;
output = ina . readLine ( ) ;
ina . close ( ) ;
2017-09-12 15:04:21 +02:00
System . out . println ( output ) ;
2017-08-30 11:29:58 +02:00
LOGGER . info ( " response from " + URL + " was valid " ) ;
2017-06-04 20:15:04 +02:00
} catch ( IOException e ) {
2017-08-30 11:29:58 +02:00
LOGGER . error ( " error while making api request or reading response " ) ;
LOGGER . error ( " response from " + URL + " was: " + output , e ) ;
2017-06-04 20:15:04 +02:00
}
2017-09-12 15:04:21 +02:00
String apiOutput = " { \" courses \" : " + output + " } " ;
2017-08-25 14:57:15 +02:00
JsonArray items = Json . parse ( apiOutput ) . asObject ( ) . get ( " courses " ) . asArray ( ) ;
2017-08-27 18:21:56 +02:00
2017-08-25 14:57:15 +02:00
for ( JsonValue item : items ) {
2017-08-26 00:13:09 +02:00
int courseTheme , gameStyle , difficulty , lastmodified , uploaded , autoScroll , stars , time ;
String owner , id , nintendoid , title ;
2017-08-27 18:21:56 +02:00
2017-06-05 17:16:39 +02:00
try {
2017-08-26 00:13:09 +02:00
courseTheme = item . asObject ( ) . getInt ( " courseTheme " , 9 ) ;
2017-06-05 17:16:39 +02:00
} catch ( Exception e ) {
2017-08-26 00:13:09 +02:00
courseTheme = 9 ;
2017-06-05 17:16:39 +02:00
}
2017-08-27 18:21:56 +02:00
2017-06-05 17:16:39 +02:00
try {
2017-08-26 00:13:09 +02:00
gameStyle = item . asObject ( ) . getInt ( " gameStyle " , 9 ) ;
2017-06-05 17:16:39 +02:00
} catch ( Exception e ) {
2017-08-26 00:13:09 +02:00
gameStyle = 9 ;
2017-06-05 17:16:39 +02:00
}
2017-08-27 18:21:56 +02:00
2017-06-05 17:16:39 +02:00
try {
2017-08-26 00:13:09 +02:00
difficulty = item . asObject ( ) . getInt ( " difficulty " , 9 ) ;
2017-06-05 17:16:39 +02:00
} catch ( Exception e ) {
difficulty = 9 ;
}
2017-08-27 18:21:56 +02:00
2017-06-05 17:16:39 +02:00
try {
2017-08-26 00:13:09 +02:00
lastmodified = item . asObject ( ) . getInt ( " lastmodified " , 9 ) ;
2017-06-05 17:16:39 +02:00
} catch ( Exception e ) {
lastmodified = 9 ;
}
2017-08-27 18:21:56 +02:00
2017-06-05 17:16:39 +02:00
try {
2017-08-26 00:13:09 +02:00
uploaded = item . asObject ( ) . getInt ( " uploaded " , 9 ) ;
2017-06-05 17:16:39 +02:00
} catch ( Exception e ) {
uploaded = 9 ;
}
2017-08-27 18:21:56 +02:00
2017-06-05 17:16:39 +02:00
try {
2017-08-26 00:13:09 +02:00
autoScroll = item . asObject ( ) . getInt ( " autoScroll " , 9 ) ;
2017-06-05 17:16:39 +02:00
} catch ( Exception e ) {
2017-08-26 00:13:09 +02:00
autoScroll = 9 ;
2017-06-05 17:16:39 +02:00
}
2017-08-27 18:21:56 +02:00
2017-06-05 17:16:39 +02:00
try {
2017-08-26 00:13:09 +02:00
stars = item . asObject ( ) . getInt ( " stars " , 9 ) ;
2017-06-05 17:16:39 +02:00
} catch ( Exception e ) {
stars = 9 ;
}
2017-08-27 18:21:56 +02:00
2017-06-18 00:33:07 +02:00
try {
2017-08-26 00:13:09 +02:00
time = item . asObject ( ) . getInt ( " time " , 9 ) ;
2017-06-18 00:33:07 +02:00
} catch ( Exception e ) {
2017-08-26 00:13:09 +02:00
time = 9 ;
2017-06-18 00:33:07 +02:00
}
2017-08-27 18:21:56 +02:00
2017-06-05 17:16:39 +02:00
try {
2017-08-26 00:13:09 +02:00
owner = item . asObject ( ) . getString ( " owner " , " " ) ;
2017-06-05 17:16:39 +02:00
} catch ( Exception e ) {
2017-08-26 00:13:09 +02:00
owner = " notset " ;
2017-06-05 17:16:39 +02:00
}
2017-08-27 18:21:56 +02:00
2017-06-05 17:16:39 +02:00
try {
2017-08-26 00:13:09 +02:00
id = item . asObject ( ) . getString ( " id " , " " ) ;
2017-06-05 17:16:39 +02:00
} catch ( Exception e ) {
2017-08-26 00:13:09 +02:00
id = " notset " ;
2017-06-05 17:16:39 +02:00
}
2017-08-27 18:21:56 +02:00
2017-06-05 17:16:39 +02:00
try {
2017-08-25 14:57:15 +02:00
nintendoid = item . asObject ( ) . getString ( " nintendoid " , " " ) ;
2017-06-05 17:16:39 +02:00
} catch ( Exception e ) {
nintendoid = " notset " ;
}
2017-08-30 11:29:58 +02:00
2017-06-05 17:16:39 +02:00
try {
2017-08-25 14:57:15 +02:00
title = item . asObject ( ) . getString ( " title " , " " ) ; ;
2017-06-05 17:16:39 +02:00
} catch ( Exception e ) {
title = " notset " ;
}
2017-08-26 00:13:09 +02:00
course . add ( new SmmdbApiDataType ( courseTheme , gameStyle , difficulty , lastmodified , uploaded , autoScroll ,
stars , time , owner , id , nintendoid , title ) ) ;
2017-08-25 14:57:15 +02:00
}
2017-06-04 20:15:04 +02:00
return course ;
}
}