package redditjson;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Reader;
import java.net.URL;
import java.nio.charset.Charset;
import org.json.JSONException;
import org.json.JSONObject;
public class RedditPager {
public static void main( String args[] ) {
int iteration = 0;
String after = "";
try {
PrintWriter writer = new PrintWriter( "reddit.json" );
String redditSearch = "harry+potter";
while ( iteration < 100 ) {
JSONObject nextRedditPage = null;
try {
String query = "http://www.reddit.com/search.json?q=" + redditSearch + "&limit=100&sort=relevance&t=all" + after;
System.out.println( "Trying: " + query );
nextRedditPage = readJsonFromUrl( query );
Thread.sleep(5000);
String afterString = nextRedditPage.getJSONObject( "data" ).getString( "after" );
after = "&after=" + afterString;
writer.println( nextRedditPage.toString() );
writer.flush();
iteration += 1;
} catch (InterruptedException e) {
} catch (IOException e) {
} catch (JSONException e) {
if ( nextRedditPage != null )
writer.println( nextRedditPage.toString() );
writer.flush();
System.out.println( "Done." );
break;
}
}
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
InputStream is = new URL(url).openStream();
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String jsonText = readAll(rd);
JSONObject json = new JSONObject(jsonText);
return json;
} finally {
is.close();
}
}
private static String readAll(Reader rd) throws IOException {
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1) {
sb.append((char) cp);
}
return sb.toString();
}
}
package redditjson;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import org.json.*;
public class RedditJSONParser {
public static void main( String args[] ) {
try {
BufferedReader f = new BufferedReader( new FileReader( "fifty.json" ) );
String line = null;
while ( (line = f.readLine()) != null ) {
JSONObject listing = new JSONObject( line );
JSONObject listingData = listing.getJSONObject( "data" );
JSONArray topics = listingData.getJSONArray( "children" );
for ( int j = 0; j < topics.length(); j++ ) {
JSONObject nextTopic = topics.getJSONObject( j );
JSONObject nextTopicData = nextTopic.getJSONObject( "data" );
String nextID = nextTopicData.getString( "id" );
int nextScore = nextTopicData.getInt( "score" );
int nextUps = nextTopicData.getInt( "ups" );
int nextDowns = nextTopicData.getInt( "downs" );
String nextSubreddit = nextTopicData.getString( "subreddit" );
String nextTitle = nextTopicData.getString( "title" );
String nextSelfText = nextTopicData.getString( "selftext" );
int numComments = nextTopicData.getInt( "num_comments" );
if ( !nextSelfText.equals( "" ) ) {
try {
JSONObject sentiment = new JSONObject( getSentimentString( nextSelfText ) );
String sentimentLabel = sentiment.getString( "label" );
JSONObject p = sentiment.getJSONObject( "probability" );
double neg = p.getDouble( "neg" );
double neutral = p.getDouble( "neutral" );
double pos = p.getDouble( "pos" );
System.out.println(
nextID + "|||" +
nextSubreddit + "|||" +
nextSelfText.replaceAll( "\n", " " ) + "|||" +
nextTitle + "|||" +
nextScore + "|||" +
nextUps + "|||" +
nextDowns + "|||" +
numComments + "|||" + sentimentLabel + "|||" + neg + "|||" + neutral + "|||" + pos);
}
catch ( IOException e ) {
continue;
}
Thread.sleep( 3000 );
}
}
}
f.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static String getSentimentString(String data) throws IOException, JSONException {
String urlParameters = "text=" + data;
byte[] postData = urlParameters.getBytes( Charset.forName( "UTF-8" ));
int postDataLength = postData.length;
String request = "http://text-processing.com/api/sentiment/";
URL url = new URL( request );
HttpURLConnection cox = (HttpURLConnection) url.openConnection();
cox.setDoOutput( true );
cox.setDoInput ( true );
cox.setInstanceFollowRedirects( false );
cox.setRequestMethod( "POST" );
cox.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded");
cox.setRequestProperty( "charset", "utf-8");
cox.setRequestProperty( "Content-Length", Integer.toString( postDataLength ));
cox.setUseCaches( false );
try( DataOutputStream wr = new DataOutputStream( cox.getOutputStream())) {
wr.write( postData );
}
BufferedReader in = new BufferedReader(new InputStreamReader(cox.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
return response.toString();
}
}






Code can be downloaded by clicking here: Controversy Galaxy Visualization. Processing, mysql, and Java were used for this project. Reddit and Sentiment analysis APIs were used.
Click a subreddit to see posts. Click a post to see text. Mousewheel to scroll. Pressing any key goes up one level.