Skip to content

Instantly share code, notes, and snippets.

@Electroid
Last active August 29, 2015 14:12
Show Gist options
  • Save Electroid/9e766d937f570af955a9 to your computer and use it in GitHub Desktop.
Save Electroid/9e766d937f570af955a9 to your computer and use it in GitHub Desktop.
OCN C# Extracter
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Net;
using HtmlAgilityPack;
namespace ConsoleApplication1 {
public static class StringTool {
// If this is false the program will not extract the data
public static bool authorize = true;
public static string shorten(string source, int length) {
if (source == null) {
Console.WriteLine("Error. String must contain content.");
authorize = false;
}
else {
length = source.length();
}
if (source.length() > length) {
source = source.substring(0, length);
}
else {
Console.writeLine("Error. Length integer must be larger than string's length.");
authorize = false;
}
return source;
}
}
class Program {
// Collection of the data to use, ignore other data in the API
public static string serverStatus; // Last server the player was seen on
public static int kills, deaths;
public static int kd, kk;
public static decimal raindrops; // In the form of thousands
public static decimal daysPlayed;
public static void webConnection() {
string baseUrl = "http://oc.tc/player/";
Console.WriteLine("What is the username of the player?");
string playerName = Console.ReadLine() + ".html"; // creates a playerName.html file to be loaded
string urlToGo = baseUrl + playerName;
WebClient scraper = new WebClient();
scraper.DownloadFile(urlToGo, playerName); // gets the html file to be parsed
HtmlDocument doc = new HtmlDocument();
doc.Load(playerName); // Loads the HTML document
}
public static int convertToInt(string str) {
shortStr = str.shorten(str, 6);
int integer = Convert.ToInt32(shortStr);
return integer;
}
public static void getServerStatus() {
HtmlNode statusNode = doc.DocumentNode.SelectSingleNode("/html/body/div/section[1]/div[1]/h1/small");
status = statusNode.InnerText; // Grabs inner text of the small node, status
}
public static void getKills() {
HtmlNode killsNode = doc.DocumentNode.SelectSingleNode("/html/body/div/section[1]/div[2]/div[1]/div/div[1]/div/div[2]/h2");
string killsString = killsNode.InnerText; // Grabs Kills node and converts it to a readable string
kills = convertToInt(killsString);
}
public static void getDeaths() {
HtmlNode deathsNode = doc.DocumentNode.SelectSingleNode("/html/body/div/section[1]/div[2]/div[1]/div/div[2]/h2");
string deathsString = deathsNode.InnerText; // Gets inner data
kills = convertToInt(deathsString);
}
public static void getKd() {
HtmlNode kdNode = doc.DocumentNode.SelectSingleNode("/html/body/div/section[1]/div[2]/div[3]/h2[1]"); //Grabs the kd node
string kdString = kdNode.InnerText; //Gets inner data
kills = convertToInt(kdString);
}
public static void Main(string[] args) {
webConnection();
if (authorize == true) {
getServerStatus();
getKills();
getDeaths();
getKd();
}
else {
Console.WriteLine("Cannot extract player data. Your soruce is corrupted.");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment