Advertisement
atillabyte

GDAT.DLL - Source [v3.2]

Jul 1st, 2014
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.84 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Text;
  5. using System.Xml;
  6. using System.Data.SQLite;
  7.  
  8. namespace GDAT
  9. {
  10.     public class GDATConverter
  11.     {
  12.         public static SQLiteDatabase database = new SQLiteDatabase();
  13.         public GDATConverter(string inputFile = "")
  14.         {
  15.             if (!string.IsNullOrEmpty(inputFile))
  16.                 database = new SQLiteDatabase(inputFile);
  17.         }
  18.  
  19.         private static System.Net.WebClient webClient = new System.Net.WebClient() { Proxy = null };
  20.  
  21.         public static string UsernameToUserId(string username, bool ThrowException = true)
  22.         {
  23.             string result = null;
  24.             bool erroneous = false;
  25.  
  26.             result = database.ExecuteScalar("SELECT userid FROM usernames WHERE username = '" + username + "' LIMIT 1");
  27.  
  28.             if (string.IsNullOrEmpty(result))
  29.             {
  30.                 result = webClient.DownloadString(string.Format("http://atil.la/public_projects/gDat/?input={0}&type=false", username));
  31.                 if (checkErroneous(result, ThrowException)) { erroneous = true; result = null; }
  32.                 else
  33.                 {
  34.                     if (!erroneous)
  35.                     {
  36.                         Dictionary<string, string> dict = new Dictionary<string, string>();
  37.                         dict.Add("username", username);
  38.                         dict.Add("userid", result);
  39.                         database.Insert("usernames", dict);
  40.                     }
  41.                 }
  42.            }
  43.            
  44.  
  45.             return result;
  46.         }
  47.         public static string UserIdToUsername(string userid, bool ThrowException = true)
  48.         {
  49.             string result = null;
  50.             bool erroneous = false;
  51.  
  52.             result = database.ExecuteScalar("SELECT username FROM usernames WHERE userid = '" + userid + "' LIMIT 1");
  53.  
  54.             if (string.IsNullOrEmpty(result))
  55.             {
  56.                 result = webClient.DownloadString(string.Format("http://atil.la/public_projects/gDat/?input={0}&type=true", userid));
  57.                 if (checkErroneous(result, ThrowException)) { erroneous = true; result = null; }
  58.                 else
  59.                 {
  60.                     if (!erroneous)
  61.                     {
  62.                         Dictionary<string, string> dict = new Dictionary<string, string>();
  63.                         dict.Add("username", result);
  64.                         dict.Add("userid", userid);
  65.                         database.Insert("usernames", dict);
  66.                     }
  67.                 }
  68.             }
  69.             return result;
  70.         }
  71.         private static bool checkErroneous(string input, bool ThrowException=true)
  72.         {
  73.             if (input.Contains("Object reference not set to an instance of an object") || input == "^E=1")
  74.             {
  75.                 return true;
  76.                 if (ThrowException) throw new Exception(string.Format("User does not exist."));
  77.             }
  78.             else if (input.Contains(" is invalid. Keys must be between 1 and 50 word characters (no spaces).") || input == "^E=2")
  79.             {
  80.                 return true;
  81.                 if (ThrowException) throw new Exception(string.Format("UserId is too long."));
  82.             }
  83.             return false;
  84.         }
  85.  
  86.         public class SQLiteDatabase
  87.         {
  88.             String dbConnection;
  89.  
  90.             public SQLiteDatabase()
  91.             {
  92.                 dbConnection = "Data Source=gDat.db";
  93.             }
  94.             public SQLiteDatabase(String inputFile)
  95.             {
  96.                 dbConnection = String.Format("Data Source={0}", inputFile);
  97.             }
  98.             public SQLiteDatabase(Dictionary<String, String> connectionOpts)
  99.             {
  100.                 String str = "";
  101.                 foreach (KeyValuePair<String, String> row in connectionOpts)
  102.                 {
  103.                     str += String.Format("{0}={1}; ", row.Key, row.Value);
  104.                 }
  105.                 str = str.Trim().Substring(0, str.Length - 1);
  106.                 dbConnection = str;
  107.             }
  108.             public DataTable GetDataTable(string sql)
  109.             {
  110.                 DataTable dt = new DataTable();
  111.                 try
  112.                 {
  113.                     SQLiteConnection cnn = new SQLiteConnection(dbConnection);
  114.                     cnn.Open();
  115.                     SQLiteCommand mycommand = new SQLiteCommand(cnn);
  116.                     mycommand.CommandText = sql;
  117.                     SQLiteDataReader reader = mycommand.ExecuteReader();
  118.                     dt.Load(reader);
  119.                     reader.Close();
  120.                     cnn.Close();
  121.                 }
  122.                 catch (Exception e)
  123.                 {
  124.                     throw new Exception(e.Message);
  125.                 }
  126.                 return dt;
  127.             }
  128.             public int ExecuteNonQuery(string sql)
  129.             {
  130.                 SQLiteConnection cnn = new SQLiteConnection(dbConnection);
  131.                 cnn.Open();
  132.                 SQLiteCommand mycommand = new SQLiteCommand(cnn);
  133.                 mycommand.CommandText = sql;
  134.                 int rowsUpdated = mycommand.ExecuteNonQuery();
  135.                 cnn.Close();
  136.                 return rowsUpdated;
  137.             }
  138.             public string ExecuteScalar(string sql)
  139.             {
  140.                 SQLiteConnection cnn = new SQLiteConnection(dbConnection);
  141.                 cnn.Open();
  142.                 SQLiteCommand mycommand = new SQLiteCommand(cnn);
  143.                 mycommand.CommandText = sql;
  144.                 object value = mycommand.ExecuteScalar();
  145.                 cnn.Close();
  146.                 if (value != null)
  147.                 {
  148.                     return value.ToString();
  149.                 }
  150.                 return "";
  151.             }
  152.             public bool Insert(String tableName, Dictionary<String, String> data, bool ThrowException = false)
  153.             {
  154.                 String columns = "";
  155.                 String values = "";
  156.                 Boolean returnCode = true;
  157.                 foreach (KeyValuePair<String, String> val in data)
  158.                 {
  159.                     columns += String.Format(" {0},", val.Key.ToString());
  160.                     values += String.Format(" '{0}',", val.Value);
  161.                 }
  162.                 columns = columns.Substring(0, columns.Length - 1);
  163.                 values = values.Substring(0, values.Length - 1);
  164.                 try
  165.                 {
  166.                     this.ExecuteNonQuery(String.Format("insert into {0}({1}) values({2});", tableName, columns, values));
  167.                 }
  168.                 catch (Exception exception)
  169.                 {
  170.                     if (ThrowException)
  171.                         throw exception;
  172.                     returnCode = false;
  173.                 }
  174.                 return returnCode;
  175.             }
  176.         }
  177.     }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement