Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Text;
- using System.Xml;
- using System.Data.SQLite;
- namespace GDAT
- {
- public class GDATConverter
- {
- public static SQLiteDatabase database = new SQLiteDatabase();
- public GDATConverter(string inputFile = "")
- {
- if (!string.IsNullOrEmpty(inputFile))
- database = new SQLiteDatabase(inputFile);
- }
- private static System.Net.WebClient webClient = new System.Net.WebClient() { Proxy = null };
- public static string UsernameToUserId(string username, bool ThrowException = true)
- {
- string result = null;
- bool erroneous = false;
- result = database.ExecuteScalar("SELECT userid FROM usernames WHERE username = '" + username + "' LIMIT 1");
- if (string.IsNullOrEmpty(result))
- {
- result = webClient.DownloadString(string.Format("http://atil.la/public_projects/gDat/?input={0}&type=false", username));
- if (checkErroneous(result, ThrowException)) { erroneous = true; result = null; }
- else
- {
- if (!erroneous)
- {
- Dictionary<string, string> dict = new Dictionary<string, string>();
- dict.Add("username", username);
- dict.Add("userid", result);
- database.Insert("usernames", dict);
- }
- }
- }
- return result;
- }
- public static string UserIdToUsername(string userid, bool ThrowException = true)
- {
- string result = null;
- bool erroneous = false;
- result = database.ExecuteScalar("SELECT username FROM usernames WHERE userid = '" + userid + "' LIMIT 1");
- if (string.IsNullOrEmpty(result))
- {
- result = webClient.DownloadString(string.Format("http://atil.la/public_projects/gDat/?input={0}&type=true", userid));
- if (checkErroneous(result, ThrowException)) { erroneous = true; result = null; }
- else
- {
- if (!erroneous)
- {
- Dictionary<string, string> dict = new Dictionary<string, string>();
- dict.Add("username", result);
- dict.Add("userid", userid);
- database.Insert("usernames", dict);
- }
- }
- }
- return result;
- }
- private static bool checkErroneous(string input, bool ThrowException=true)
- {
- if (input.Contains("Object reference not set to an instance of an object") || input == "^E=1")
- {
- return true;
- if (ThrowException) throw new Exception(string.Format("User does not exist."));
- }
- else if (input.Contains(" is invalid. Keys must be between 1 and 50 word characters (no spaces).") || input == "^E=2")
- {
- return true;
- if (ThrowException) throw new Exception(string.Format("UserId is too long."));
- }
- return false;
- }
- public class SQLiteDatabase
- {
- String dbConnection;
- public SQLiteDatabase()
- {
- dbConnection = "Data Source=gDat.db";
- }
- public SQLiteDatabase(String inputFile)
- {
- dbConnection = String.Format("Data Source={0}", inputFile);
- }
- public SQLiteDatabase(Dictionary<String, String> connectionOpts)
- {
- String str = "";
- foreach (KeyValuePair<String, String> row in connectionOpts)
- {
- str += String.Format("{0}={1}; ", row.Key, row.Value);
- }
- str = str.Trim().Substring(0, str.Length - 1);
- dbConnection = str;
- }
- public DataTable GetDataTable(string sql)
- {
- DataTable dt = new DataTable();
- try
- {
- SQLiteConnection cnn = new SQLiteConnection(dbConnection);
- cnn.Open();
- SQLiteCommand mycommand = new SQLiteCommand(cnn);
- mycommand.CommandText = sql;
- SQLiteDataReader reader = mycommand.ExecuteReader();
- dt.Load(reader);
- reader.Close();
- cnn.Close();
- }
- catch (Exception e)
- {
- throw new Exception(e.Message);
- }
- return dt;
- }
- public int ExecuteNonQuery(string sql)
- {
- SQLiteConnection cnn = new SQLiteConnection(dbConnection);
- cnn.Open();
- SQLiteCommand mycommand = new SQLiteCommand(cnn);
- mycommand.CommandText = sql;
- int rowsUpdated = mycommand.ExecuteNonQuery();
- cnn.Close();
- return rowsUpdated;
- }
- public string ExecuteScalar(string sql)
- {
- SQLiteConnection cnn = new SQLiteConnection(dbConnection);
- cnn.Open();
- SQLiteCommand mycommand = new SQLiteCommand(cnn);
- mycommand.CommandText = sql;
- object value = mycommand.ExecuteScalar();
- cnn.Close();
- if (value != null)
- {
- return value.ToString();
- }
- return "";
- }
- public bool Insert(String tableName, Dictionary<String, String> data, bool ThrowException = false)
- {
- String columns = "";
- String values = "";
- Boolean returnCode = true;
- foreach (KeyValuePair<String, String> val in data)
- {
- columns += String.Format(" {0},", val.Key.ToString());
- values += String.Format(" '{0}',", val.Value);
- }
- columns = columns.Substring(0, columns.Length - 1);
- values = values.Substring(0, values.Length - 1);
- try
- {
- this.ExecuteNonQuery(String.Format("insert into {0}({1}) values({2});", tableName, columns, values));
- }
- catch (Exception exception)
- {
- if (ThrowException)
- throw exception;
- returnCode = false;
- }
- return returnCode;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement