Added parser prototype

this is a prototype for the JSON parser.
Not fully fonctionnal (bunch of NullPointerException)
This commit is contained in:
Louis Vallat 2019-04-23 10:35:02 +02:00
parent f42d4c81fd
commit 520a2ee598
4 changed files with 1436 additions and 1361 deletions

3
.gitignore vendored
View File

@ -21,4 +21,5 @@
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
/twitter_techsupportgore_bot/nbproject/private/
/twitter_techsupportgore_bot/nbproject/private/
/twitter_techsupportgore_bot/build/

File diff suppressed because it is too large Load Diff

View File

@ -29,9 +29,12 @@ dist.jar=${dist.dir}/twitter_techsupportgore_bot.jar
dist.javadoc.dir=${dist.dir}/javadoc
endorsed.classpath=
excludes=
file.reference.gson-2.8.5.jar=C:\\Users\\louis\\Documents\\libraries\\Java\\gson-2.8.5.jar
file.reference.kotlin-runtime.jar=nulllib\\kotlin-runtime.jar
includes=**
jar.compress=false
javac.classpath=
javac.classpath=\
${file.reference.gson-2.8.5.jar}
# Space-separated list of extra javac options
javac.compilerargs=
javac.deprecation=false
@ -56,6 +59,7 @@ javadoc.splitindex=true
javadoc.use=true
javadoc.version=false
javadoc.windowtitle=
kotlinc.classpath=${file.reference.kotlin-runtime.jar}
main.class=twitter_techsupportgore_bot.Twitter_techsupportgore_bot
manifest.file=manifest.mf
meta.inf.dir=${src.dir}/META-INF

View File

@ -16,18 +16,84 @@
*/
package twitter_techsupportgore_bot;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
/**
* This is where everything begins.
* This is where everything begins.
*
* @author louis
*/
public class Twitter_techsupportgore_bot {
private static HttpURLConnection con;
/**
* Launch the bot.
*
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
public static void main(String[] args) throws MalformedURLException,
ProtocolException, IOException {
String url = "https://www.reddit.com/r/techsupportgore/new.json?limit=75";
try {
URL myurl = new URL(url);
con = (HttpURLConnection) myurl.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("User-Agent", "Mozilla 5.0 (Windows; U; "
+ "Windows NT 5.1; en-US; rv:1.8.0.11) ");
StringBuilder content;
try (BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()))) {
String line;
content = new StringBuilder();
while ((line = in.readLine()) != null) {
content.append(line);
content.append(System.lineSeparator());
}
}
JsonObject objet = new JsonParser().parse(content.toString()).getAsJsonObject();
JsonObject data = new JsonParser().parse(objet.get("data").toString()).getAsJsonObject();
JsonArray children = new JsonParser().parse(data.get("children").toString()).getAsJsonArray();
for (int i = 0; i < children.size(); i++) {
JsonObject child = new JsonParser().parse(children.get(i).toString()).getAsJsonObject();
JsonObject childData = new JsonParser().parse(child.get("data").toString()).getAsJsonObject();
System.out.println("Title: " + childData.get("title").toString());
System.out.println("From: " + childData.get("author").toString());
System.out.println("Is crosspostable ? : " + !childData.get("is_crosspostable").getAsBoolean());
System.out.println("Is a video ? : " + childData.get("is_video").getAsBoolean());
System.out.println("Is mature ? : " + childData.get("over_18").getAsBoolean());
System.out.println("Score : " + childData.get("score").getAsDouble());
System.out.println("Post url : " + childData.get("url").getAsString());
JsonObject preview = new JsonParser().parse(childData.get("preview").toString()).getAsJsonObject();
JsonArray previewImages = new JsonParser().parse(preview.get("images").toString()).getAsJsonArray();
JsonObject source = new JsonParser().parse(previewImages.get(0).toString()).getAsJsonObject();
JsonObject urlSrc = new JsonParser().parse(source.get("source").toString()).getAsJsonObject();
System.out.println("Source url : " + urlSrc.get("url").toString().replace("amp;", ""));
System.out.println("-----------------");
}
} finally {
con.disconnect();
}
}
}