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

1
.gitignore vendored
View File

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

View File

@ -1,4 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!-- <!--
*** GENERATED FROM project.xml - DO NOT EDIT *** *** GENERATED FROM project.xml - DO NOT EDIT ***
*** EDIT ../build.xml INSTEAD *** *** EDIT ../build.xml INSTEAD ***
@ -20,6 +21,8 @@ is divided into following sections:
--> -->
<project xmlns:j2seproject1="http://www.netbeans.org/ns/j2se-project/1" xmlns:j2seproject3="http://www.netbeans.org/ns/j2se-project/3" xmlns:jaxrpc="http://www.netbeans.org/ns/j2se-project/jax-rpc" basedir=".." default="default" name="twitter_techsupportgore_bot-impl"> <project xmlns:j2seproject1="http://www.netbeans.org/ns/j2se-project/1" xmlns:j2seproject3="http://www.netbeans.org/ns/j2se-project/3" xmlns:jaxrpc="http://www.netbeans.org/ns/j2se-project/jax-rpc" basedir=".." default="default" name="twitter_techsupportgore_bot-impl">
<property name="kotlin.lib" value="C:\Users\louis\AppData\Roaming\NetBeans\8.2\kotlinc\lib"/>
<typedef classpath="${kotlin.lib}/kotlin-ant.jar" resource="org/jetbrains/kotlin/ant/antlib.xml"/>
<fail message="Please build using Ant 1.8.0 or higher."> <fail message="Please build using Ant 1.8.0 or higher.">
<condition> <condition>
<not> <not>
@ -273,6 +276,7 @@ is divided into following sections:
<include name="*"/> <include name="*"/>
</dirset> </dirset>
</src> </src>
<withKotlin/>
<classpath> <classpath>
<path path="@{classpath}"/> <path path="@{classpath}"/>
</classpath> </classpath>

View File

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

View File

@ -16,18 +16,84 @@
*/ */
package twitter_techsupportgore_bot; 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 * @author louis
*/ */
public class Twitter_techsupportgore_bot { public class Twitter_techsupportgore_bot {
private static HttpURLConnection con;
/** /**
* Launch the bot. * Launch the bot.
*
* @param args the command line arguments * @param args the command line arguments
*/ */
public static void main(String[] args) { public static void main(String[] args) throws MalformedURLException,
// TODO code application logic here 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();
}
}
}