Initial Commit

Initial commit of project.
This commit is contained in:
Alex Stewart
2019-03-09 01:21:44 -08:00
parent e54e9653ed
commit 13420951b1
124 changed files with 5883 additions and 0 deletions
+19
View File
@@ -0,0 +1,19 @@
package network;
import java.sql.ResultSet;
// NOTE: This outlines Java DataBase Connection Driver
// requirements that cater to our specific needs.
public abstract class JDBCDriver
{
// Connects to the database, returns a bool if it succeeded.
public abstract boolean Connect();
// Disconnects the driver. Returns if the driver is disconnected now, regardless of connection status.
public abstract boolean Disconnect();
// Executes a SQL command with the database. Returns if it was executed successfully, or in the
// case of the returning statement, returns the ResultSet.
public abstract boolean ExecuteStatement(String statement);
public abstract ResultSet ExecuteReturningStatement(String statement);
}
+30
View File
@@ -0,0 +1,30 @@
package network;
import java.sql.ResultSet;
public class JDBCDriverMySQL extends JDBCDriver
{
@Override
public boolean Connect() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean Disconnect() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean ExecuteStatement(String statement) {
// TODO Auto-generated method stub
return false;
}
@Override
public ResultSet ExecuteReturningStatement(String statement) {
// TODO Auto-generated method stub
return null;
}
}
+30
View File
@@ -0,0 +1,30 @@
package network;
import java.sql.ResultSet;
public class JDBCDriverPostgreSQL extends JDBCDriver
{
@Override
public boolean Connect() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean Disconnect() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean ExecuteStatement(String statement) {
// TODO Auto-generated method stub
return false;
}
@Override
public ResultSet ExecuteReturningStatement(String statement) {
// TODO Auto-generated method stub
return null;
}
}
+117
View File
@@ -0,0 +1,117 @@
package network;
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import utils.GlobalLog;
import utils.LogFilter;
// Only to be called by the generic driver.
public class JDBCDriverSQLite extends JDBCDriver
{
Connection connection = null;
String databaseFolder = "db/";
String databaseName = "catfood";
@Override
public boolean Connect()
{
try
{
{ // Scope to discard file...
File f = new File(databaseFolder);
if(!f.exists() || !f.isDirectory())
{
f.mkdir();
}
}
// db parameters
String url = "jdbc:sqlite:db/" + databaseName + ".db";
// create a connection to the database
connection = DriverManager.getConnection(url);
GlobalLog.Log(LogFilter.Database, "Connection to SQLite has been established.");
}
catch (SQLException e)
{
GlobalLog.Error(e.getMessage());
}
return connection != null;
}
@Override
public boolean Disconnect()
{
try
{
if (connection != null)
connection.close();
return true;
}
catch (SQLException ex)
{
GlobalLog.Error(ex.getMessage());
return false;
}
}
@Override
public ResultSet ExecuteReturningStatement(String sql)
{
if(connection == null)
return null;
if(sql == null || sql.length() == 0)
return null;
try
{
Statement statement = connection.createStatement();
ResultSet set = statement.executeQuery(sql);
return set;
}
catch (SQLException e)
{
try {
GlobalLog.Fatal(e.getMessage());
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return null;
}
}
public boolean ExecuteStatement(String sql)
{
if(connection == null)
return false;
if(sql == null || sql.length() == 0)
return false;
try
{
Statement statement = connection.createStatement();
boolean executed = statement.execute(sql);
return executed;
}
catch (SQLException e)
{
try {
GlobalLog.Fatal(e.getMessage());
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return false;
}
}
}
+29
View File
@@ -0,0 +1,29 @@
package network;
import utils.HTTPUtils;
public class NetworkColiru
{
public String compileCPlus(String query)
{
// Escape characters that need escaping.
// Primarily types of whitespace.
query = query.replace("\\n", "\\\\n");
query = query.replace("\\t", "\\\\t");
query = query.replace("\n", "\\n");
query = query.replace("\"", "\\\"");
query = query.replace("\t", "\\t");
// Send the compilation request!
String result = HTTPUtils.SendPOSTRequest("http://coliru.stacked-crooked.com/compile"
, "{ \"cmd\": \"g++ main.cpp && ./a.out\", \"src\": \"" + query + "\" }");
// If we got a valid response...
if(!result.isEmpty())
{
result = "Here's what happened when I went to compiled that! \n```" + result + "```";
}
return result;
}
}
+69
View File
@@ -0,0 +1,69 @@
package network;
import com.google.gson.Gson;
import core.*;
import offline.*;
import utils.*;
public class NetworkDerpi
{
private final String mainURL = "https://derpibooru.org/search.json?q=";
private static final Gson jsonParser_ = new Gson();
private class DerpiResponseObject
{
public String image;
public String tags;
}
private class InitialRequest
{
public int id;
}
public GenericImage getDerpi(String query)
{
GenericImage image = new GenericImage(" ", " ", " ");
query = query.trim();
query = query.replace(" ", ",");
String res = HTTPUtils.SendGETRequest(mainURL + query + "&random_image=1&key=" + Ref.derpiKey);
if(res != null)
{
// Use class evaluation on an array of the response object to be able to hold multiple.
InitialRequest obj = jsonParser_.fromJson(res, InitialRequest.class);
if(res != null)
{
String res2 = HTTPUtils.SendGETRequest("https://derpibooru.org/" + obj.id + ".json");
image.editPostURL("<https://derpibooru.org/" + obj.id +">");
DerpiResponseObject imageObj = jsonParser_.fromJson(res2, DerpiResponseObject.class);
image.editImageURL("https:" + imageObj.image.substring(0, imageObj.image.indexOf('_')) + imageObj.image.substring(imageObj.image.lastIndexOf('.')));
if(imageObj.tags.contains("artist:"))
{
String [] sepTags = imageObj.tags.split(",");
String artists = "";
for(int i = 0; i < sepTags.length; i++)
{
if(sepTags[i].contains("artist:"))
{
if(!artists.equals(""))
{
artists += " and ";
}
artists += sepTags[i].substring(sepTags[i].indexOf(":")+1);
}
}
image.editArtist(artists);
}
else
{
image.editArtist("Artist Unknown!");
}
}
}
return image;
}
}
+103
View File
@@ -0,0 +1,103 @@
package network;
import com.google.gson.Gson;
import core.*;
import utils.*;
/**
* This is the e621 request class, designed for form and parse requests that
* use the e621 API, and is entirely static.
*
* If you ever find this class randomly not working,
* it may be a good idea to make sure the user agent string is set in
* HTTPUtils to something other than a browser emulating string, or the default
* java one.
*
* @author Wisp
* Edited by Rin
*/
public class NetworkE621
{
///////////////////////////////////////
// Internal JSON class and variables //
//////////////////////////////////.////
private static final Gson jsonParser_ = new Gson();
private static final String API_ROOT = "https://e621.net/post/index.json?";
private static int maxSearchResults_ = 10;
private static String[] blacklist = {"theallseeingeye","scat","diaper","cub"};
private class E621ResponseObject
{
// public varaibles matching the case and the type we want for JSON.
// There are many more fields, but if we don't provide some it just
// doesn't bother parsing them.
public String file_url;
public String id;
public String tags;
public String [] artist;
}
////////////////////
// Static methods //
////////////////////
// Requests a specific image, then returns a few.
public GenericImage getE621(String input)
{
GenericImage image = new GenericImage("","","");
boolean blacklisted;
// Clean up request and replace problematic characters for the query string.
input = input.trim();
input = input.replace("+", "%2B");
input = input.replace(" ", "%20");
// Configure and send request. Note: Random ordering added as first
// tag by default. User-provided tags, therefore, will override it.
// If order:score is provided, that will be honored over order:random.
String res = HTTPUtils.SendPOSTRequest(API_ROOT
, "tags=order:random%20" + input + "&limit=" + maxSearchResults_);
if(res != null)
{
// Use class evaluation on an array of the response imageObject to be able to hold multiple.
E621ResponseObject[] imageObj = jsonParser_.fromJson(res, E621ResponseObject[].class);
// For now, we really just wanna display images and their source.
// Append them all separately to a response string w/ some flavor text.
if(imageObj.length < 1)
{
}
else
{
for(int i = 0; i < imageObj.length; ++i)
{
blacklisted = false;
for(int j = 0; j < blacklist.length; j++)
{
if(imageObj[i].tags.contains(blacklist[j]))
{
blacklisted = true;
}
}
if(blacklisted)
{
continue;
}
// We will always have a file URL. That's a given.
image.editImageURL(imageObj[i].file_url);
image.editPostURL("https://e621.net/post/show/" + imageObj[i].id);
if(imageObj[i].artist.length > 0)
image.editArtist(imageObj[i].artist[0]);
else
image.editArtist("Artist Not Found!");
}
}
}
return image;
}
}
+80
View File
@@ -0,0 +1,80 @@
package network;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import com.google.gson.Gson;
import offline.Ref;
public class NetworkJDoodle
{
private static final Gson jsonParser_ = new Gson();
private class JDoodleObject
{
public String output;
public String cpuTime;
}
public String compileJava(String query)
{
String result = "";
String lang = "java";
String version = "0";
query = query.replace("\\n", "\\\\n");
query = query.replace("\\t", "\\\\t");
query = query.replace("\n", "\\n");
query = query.replace("\"", "\\\"");
query = query.replace("\t", "\\t");
String input = "{\"clientId\": \"" + Ref.jdoodleID + "\",\"clientSecret\":\"" + Ref.jdoodleSecret + "\",\"script\":\"" + query +
"\",\"language\":\"" + lang + "\",\"versionIndex\":\"" + version + "\"} ";
try {
URL url = new URL("https://api.jdoodle.com/v1/execute");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
//result += input + "\n";
OutputStream outputStream = connection.getOutputStream();
outputStream.write(input.getBytes());
outputStream.flush();
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
result = "Please check your inputs : HTTP error code : "+ connection.getResponseCode();
return result;
}
BufferedReader bufferedReader;
bufferedReader = new BufferedReader(new InputStreamReader((connection.getInputStream())));
String output;
String fullOut = "";
result += "This is what happened! \n";
while ((output = bufferedReader.readLine()) != null)
{
fullOut += output + "\n";
}
JDoodleObject doodle = jsonParser_.fromJson(fullOut, JDoodleObject.class);
result += doodle.output + "\n";
result += "The CPU time was " + doodle.cpuTime + "\n";
connection.disconnect();
return result;
} catch (MalformedURLException e)
{
return "You probably shouldn't be seeing this";
} catch (IOException e)
{
return "You probably shouldn't be seeing this";
}
}
}
+36
View File
@@ -0,0 +1,36 @@
package network;
import java.io.*;
import java.net.*;
import offline.Ref;
public class NetworkWolfram
{
private static Integer num = 1;
public String getWolfram(String input) throws IOException
{
String name = null;
synchronized(num)
{
name = "wolfram_" + num;
++num;
}
URL url = new URL("http://api.wolframalpha.com/v1/simple?appid=" + Ref.wolfRamID + "&i="
+ URLEncoder.encode(input, "UTF-8"));
InputStream in = new BufferedInputStream(url.openStream());
OutputStream out = new BufferedOutputStream(new FileOutputStream(name + ".png"));
for ( int i; (i = in.read()) != -1; )
{
out.write(i);
}
in.close();
out.close();
return name + ".png";
}
}