-
-
Save anonymous/d62d564370ba7523787a to your computer and use it in GitHub Desktop.
Spark Scraper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.IO; | |
using System.Net; | |
using HtmlAgilityPack; | |
namespace MySparkScraper | |
{ | |
class ExWebClient | |
{ | |
//The cookies will be here. | |
private CookieContainer _cookies = new CookieContainer(); | |
//In case you need to clear the cookies | |
public void ClearCookies() | |
{ | |
_cookies = new CookieContainer(); | |
} | |
public HtmlDocument Get(string url) | |
{ | |
var request = (HttpWebRequest)WebRequest.Create(url); | |
request.Proxy = WebRequest.DefaultWebProxy; | |
request.Method = "GET"; | |
//This is the important part. | |
request.CookieContainer = _cookies; | |
var response = (HttpWebResponse)request.GetResponse(); | |
var stream = response.GetResponseStream(); | |
//When you get the response from the website, the cookies will be stored | |
//automatically in "_cookies". | |
using (var reader = new StreamReader(stream)) | |
{ | |
string html = reader.ReadToEnd(); | |
var doc = new HtmlDocument(); | |
doc.LoadHtml(html); | |
return doc; | |
} | |
} | |
public HtmlDocument Login(string url, string username, string password) | |
{ | |
var request = (HttpWebRequest)WebRequest.Create(url); | |
request.Proxy = WebRequest.DefaultWebProxy; | |
byte[] send = System.Text.Encoding.Default.GetBytes(string.Format("loginSrc=ext&username={0}&password={1}&sign-in=Sign+In", username, password)); | |
request.ContentType = "application/x-www-form-urlencoded"; | |
request.ContentLength = send.Length; | |
request.Method = "POST"; | |
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36"; | |
request.Headers["Cache-Control"] = "no-cache"; | |
//This is the important part. | |
request.CookieContainer = _cookies; | |
Stream sout = request.GetRequestStream(); | |
sout.Write(send, 0, send.Length); | |
sout.Flush(); | |
sout.Close(); | |
var response = (HttpWebResponse)request.GetResponse(); | |
var stream = response.GetResponseStream(); | |
//When you get the response from the website, the cookies will be stored | |
//automatically in "_cookies". | |
using (var reader = new StreamReader(stream)) | |
{ | |
string html = reader.ReadToEnd(); | |
var doc = new HtmlDocument(); | |
doc.LoadHtml(html); | |
return doc; | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using Newtonsoft.Json.Linq; | |
namespace MySparkScraper | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var client = new ExWebClient(); | |
var doc1 = client.Get("https://www.spark.co.nz/myspark/"); | |
// find the login url | |
var loginForm = doc1.DocumentNode.Descendants("form").FirstOrDefault(n => n.Attributes["name"] != null && n.Attributes["name"].Value == "myForm"); | |
var loginUrl = loginForm.Attributes["action"].Value.Replace("&", "&"); | |
client.Login(loginUrl, "<encoded email>", "<password>"); | |
var usageInfo = client.Get("https://www.spark.co.nz/portal/site/mytelecom-site/template.BINARYPORTLET/menuitem.c26e41f6aeaa53d794910bf3bc407ea0/resource.process/?javax.portlet.tpst=e0282410b21e6a6e85cdc707d5107ea0&javax.portlet.rid_e0282410b21e6a6e85cdc707d5107ea0=ajaxGetDataUsage&javax.portlet.rcl_e0282410b21e6a6e85cdc707d5107ea0=cacheLevelPage&javax.portlet.begCacheTok=com.vignette.cachetoken&javax.portlet.endCacheTok=com.vignette.cachetoken"); | |
dynamic info = JObject.Parse(usageInfo.DocumentNode.InnerText); | |
Console.WriteLine(string.Format("Used {0} of {1} {2} for period {3}", info.actionResult.dataUsed, | |
info.actionResult.dataCap, info.actionResult.dataUnit, info.actionResult.usagePeriod)); | |
Console.ReadLine(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment