2017-06-04 20:15:04 +02:00
/ * *
2017-10-15 13:37:45 +02:00
* cemu_UI
*
* Copyright 2017 < @Seil0 >
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program ; if not , write to the Free Software
* Foundation , Inc . , 51 Franklin Street , Fifth Floor , Boston ,
* MA 02110 - 1301 , USA .
2017-06-04 20:15:04 +02:00
* /
2017-10-15 13:37:45 +02:00
2017-11-13 16:44:39 +01:00
package com.cemu_UI.controller ;
2017-06-04 20:15:04 +02:00
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-11-13 16:44:39 +01:00
import com.cemu_UI.datatypes.SmmdbApiDataType ;
2017-06-04 20:15:04 +02:00
import com.eclipsesource.json.Json ;
import com.eclipsesource.json.JsonArray ;
import com.eclipsesource.json.JsonValue ;
2017-11-14 12:02:39 +01:00
public class SmmdbAPIController {
2017-06-04 20:15:04 +02:00
2017-11-10 15:58:20 +01:00
private String URL = " https://smmdb.ddns.net/api/getcourses?format=json " ;
2017-11-14 12:02:39 +01:00
private static final Logger LOGGER = LogManager . getLogger ( SmmdbAPIController . class . getName ( ) ) ;
2017-06-04 20:15:04 +02:00
2017-11-14 12:02:39 +01:00
public SmmdbAPIController ( ) {
2017-06-04 20:15:04 +02:00
//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-08-30 11:29:58 +02:00
LOGGER . info ( " response from " + URL + " was valid " ) ;
2017-10-14 15:42:13 +02:00
LOGGER . info ( output ) ;
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-12-09 13:10:19 +01:00
return course ;
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 ;
}
}