Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.IO;
- using System.Runtime.Serialization.Formatters.Binary;
- using System.Net;
- using System.Text.RegularExpressions;
- using System.Xml;
- using Thrift;
- using Thrift.Protocol;
- using Thrift.Collections;
- using Thrift.Transport;
- using Evernote.EDAM.Type;
- using Evernote.EDAM.UserStore;
- using Evernote.EDAM.NoteStore;
- using Evernote.EDAM.Error;
- using System.Runtime.InteropServices;
- namespace SoccerwayWebScraper
- {
- public partial class Form1 : Form
- {
- string getLeaguesUrlStart = "http://www.soccerway.com/a/block_teams_index_club_teams?block_id=page_teams_1_block_teams_index_club_teams_2&callback_params=%7B%22level%22%3A%222%22%7D&action=expandItem¶ms=%7B%22area_id%22%3A%22";
- string getLeaguesUrlEnd = "%22%2C%22level%22%3A2%2C%22item_key%22%3A%22area_id%22%7D";
- string getTeamsUrlStart = "http://www.soccerway.com/a/block_teams_index_club_teams?block_id=page_teams_1_block_teams_index_club_teams_2&callback_params=%7B%22level%22%3A%222%22%7D&action=expandItem¶ms=%7B%22competition_id%22%3A%22";
- string getTeamsUrlEnd = "%22%2C%22level%22%3A3%2C%22item_key%22%3A%22competition_id%22%7D";
- string getStatsUrlStart = "http://www.soccerway.com/a/block_team_squad?block_id=page_team_1_block_team_squad_7&callback_params=%7B%22team_id%22%3A%22";
- string getStatsUrlEnd = "%22%7D&action=changeView¶ms=%7B%22view%22%3A1%7D";
- string authToken = "S=s1:U=94e77:E=16d5aa23a5c:C=16602f10d60:P=1cd:A=en-devtoken:V=2:H=cb38f20368a29a1b2b3f34cc38f95fce";
- public Form1()
- {
- InitializeComponent();
- }
- List<Country> ReadSerializedCountryList()
- {
- //Stream streamRead = new MemoryStream(Properties.Resources.Countries);
- Stream streamRead = File.OpenRead("Countries.serialized");
- BinaryFormatter binaryRead = new BinaryFormatter();
- List<Country> result = (List<Country>)binaryRead.Deserialize(streamRead);
- streamRead.Close();
- return result;
- }
- List<League> ReadSerializedLeagueList(string path)
- {
- //Stream streamRead = new MemoryStream(Properties.Resources.Leagues);
- Stream streamRead = File.OpenRead(path);
- BinaryFormatter binaryRead = new BinaryFormatter();
- List<League> result = (List<League>)binaryRead.Deserialize(streamRead);
- streamRead.Close();
- return result;
- }
- List<Country> ReadCountries(string filename)
- {
- string file = File.ReadAllText(filename);
- List<string> rawCountries = file.Split(new string[1] {"</li>"}, StringSplitOptions.RemoveEmptyEntries).ToList();
- rawCountries.Remove(rawCountries.Last());
- List<Country> countries = new List<Country>();
- foreach (string country in rawCountries)
- {
- int index1 = country.IndexOf("data-area_id=\"") + "data-area_id=\"".Length;
- int index2 = country.IndexOf("\">\r\n <div class=\"row\">");
- string temp = country.Substring(index1, index2 - index1);
- int id = Int32.Parse(temp);
- index1 = country.IndexOf("\" >\r\n") + "\" >\r\n".Length;
- index2 = index1 + country.Remove(0, index1).IndexOf("\r\n");
- string name = country.Substring(index1, index2 - index1);
- name = name.Trim();
- countries.Add(new Country() { Id = id, Name = name });
- }
- return countries;
- }
- void SerializeCountryList()
- {
- Stream streamWrite = File.Create("Countries2.serialized");
- BinaryFormatter binaryWrite = new BinaryFormatter();
- binaryWrite.Serialize(streamWrite, allCountries);
- streamWrite.Close();
- }
- void SerializeLeagues(List<League> l, string path)
- {
- Stream streamWrite = File.Create(path);
- BinaryFormatter binaryWrite = new BinaryFormatter();
- binaryWrite.Serialize(streamWrite, l);
- streamWrite.Close();
- }
- void BuildLeagueListFromFiles()
- {
- allLeagues = new List<League>();
- foreach (Country c in allCountries)
- {
- string text = File.ReadAllText(c.Name);
- if (!text.Contains("<ul class=\\\"competitions\\\""))
- continue;
- text = text.Substring(text.IndexOf("<ul"), text.IndexOf("<\\/ul") - text.IndexOf("<ul"));
- List<string> rawLeagues = text.Split(new string[1] { "<li" }, StringSplitOptions.RemoveEmptyEntries).ToList();
- rawLeagues.Remove(rawLeagues.First());
- foreach (string s in rawLeagues)
- {
- int index1 = s.IndexOf("data-competition_id=\\\"") + "data-competition_id=\\\"".Length;
- int index2 = s.IndexOf("\\\"><div class=\\\"row\\\"");
- string temp = s.Substring(index1, index2 - index1);
- index1 = s.IndexOf("class=\\\"competition\\\" >") + "class=\\\"competition\\\" >".Length;
- index2 = s.IndexOf("<\\/a><span class=\\\"expand_icon\\\">");
- string name = s.Substring(index1, index2 - index1);
- allLeagues.Add(new League() { CountryId = c.Id, Id = temp, Name = name });
- }
- }
- }
- Random r = new Random();
- void SaveAllCountryLeaguesToFiles()
- {
- foreach (Country c in allCountries)
- {
- string url = getLeaguesUrlStart + c.Id.ToString() + getLeaguesUrlEnd;
- WebClient wc = new WebClient();
- wc.Headers.Add("User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.77 Safari/535.7");
- wc.DownloadFile(url, c.Name);
- System.Threading.Thread.Sleep(r.Next(1000, 4000));
- }
- }
- List<Country> allCountries;
- List<League> allLeagues;
- List<League> monitoredLeagues;
- private void Form1_Load(object sender, EventArgs e)
- {
- SetPassword();
- SetLocation();
- allCountries = ReadSerializedCountryList();
- allLeagues = ReadSerializedLeagueList("Leagues.serialized");
- monitoredLeagues = ReadSerializedLeagueList("Monitored.serialized");
- PopulateComboBox();
- PopulateListBox();
- }
- void UpdateAllLeaguesToEvernote()
- {
- // im not sure whether we need this, but im too lazy to test
- List<League> unupdatedLeagues = (List<League>)Ext.Clone(monitoredLeagues);
- everythingFinishedUpdating = false;
- DoDelayedLeagueUpdate(unupdatedLeagues);
- }
- bool leagueFinishedUpdating = false;
- bool everythingFinishedUpdating = false;
- // same as teams
- void DoDelayedLeagueUpdate(List<League> unupdatedLeagues)
- {
- if (SomethingDown())
- {
- everythingFinishedUpdating = true;
- return;
- }
- if (unupdatedLeagues.Count() == 0)
- {
- //finished everything
- everythingFinishedUpdating = true;
- return;
- }
- leagueFinishedUpdating = false;
- League leagueToBeUpdated = unupdatedLeagues.Last();
- UpdateLeagueToEvernote(leagueToBeUpdated);
- unupdatedLeagues.Remove(leagueToBeUpdated);
- Timer t = new Timer() { Interval = r.Next(10000, 20000) };
- t.Tick += delegate { if (leagueFinishedUpdating) { t.Stop(); DoDelayedLeagueUpdate(unupdatedLeagues); } };
- t.Start();
- }
- void UpdateLeagueToEvernote(League l)
- {
- List<Team> rawTeams = GetAllTeams(l);
- if (SomethingDown())
- {
- leagueFinishedUpdating = true;
- return;
- }
- List<TeamWithStats> teamsWithStats = new List<TeamWithStats>();
- DoDelayedTeamUpdate(l, rawTeams, teamsWithStats);
- }
- // This function is called for EVERY team with a timer, this is done to not send 9999 requests at once, but in a human like 1-4 second intervals
- // it's kind of a delayed recursion
- void DoDelayedTeamUpdate(League l, List<Team> teamsLeftToUpdate, List<TeamWithStats> teamsUpdated)
- {
- if (SomethingDown())
- {
- leagueFinishedUpdating = true;
- return;
- }
- if (teamsLeftToUpdate.Count() == 0)
- {
- updateMsg = "Now sending team stats to evernote...";
- UpdateNotebook(l, teamsUpdated);
- leagueFinishedUpdating = true;
- return;
- }
- Team teamToUpdate = teamsLeftToUpdate.Last();
- updateMsg = "Getting stats table for " + l.QualifiedName + " team " + teamToUpdate.Name;
- TeamWithStats updatedTeam = new TeamWithStats(teamToUpdate, GetTeamStatsTable(teamToUpdate));
- if (SomethingDown())
- {
- leagueFinishedUpdating = true;
- return;
- }
- teamsLeftToUpdate.Remove(teamToUpdate);
- teamsUpdated.Add(updatedTeam);
- Timer t = new Timer() { Interval = r.Next(1000, 3000) };
- t.Tick += delegate { t.Stop(); DoDelayedTeamUpdate(l, teamsLeftToUpdate, teamsUpdated); };
- t.Start();
- }
- bool SomethingDown()
- {
- if (offline || evernoteUnreachable || soccerwayUnreachable)
- return true;
- return false;
- }
- string FormartAsEvernote(string html, bool replacements)
- {
- if (replacements)
- {
- // remove all invalid attributes and tags
- List<string> res = new List<string>() { "(class|id|data-season_id|data-people_id)=\"[^\"]{0,}\"", "</?a[^>]{0,}>" };
- foreach (string re in res)
- {
- html = Regex.Replace(html, re, "");
- }
- // close any img tags
- html = html.Replace("</img>", "");
- // src because it also can contain a close tag / like src="http://", so we skip it here
- string unclosedImg = "<img[^>]{0,}src=\"[^>]{0,}\"[^/>]{0,}>";
- MatchCollection mc = Regex.Matches(html, unclosedImg);
- foreach (Match match in mc)
- {
- string temp = match.Value.Replace(">", "/>");
- html = html.Replace(match.Value, temp);
- }
- }
- return @"<?xml version=""1.0"" encoding=""UTF-8""?>
- <!DOCTYPE en-note SYSTEM ""http://xml.evernote.com/pub/enml2.dtd"">
- <en-note>" + html + "</en-note>";
- }
- void PopulateComboBox()
- {
- foreach (Country c in allCountries)
- {
- comboBox1.Items.Add(c);
- }
- comboBox1.SelectedIndex = 0;
- }
- void PopulateListBox()
- {
- listBox1.Items.Clear();
- foreach (League l in monitoredLeagues)
- {
- listBox1.Items.Add(l);
- }
- }
- List<Team> GetAllTeams(League l)
- {
- List<Team> teams = new List<Team>();
- string url = getTeamsUrlStart + l.Id + getTeamsUrlEnd;
- WebClient wc = new WebClient();
- wc.Headers.Add("User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.77 Safari/535.7");
- string raw = "";
- int i = 0;
- while (true)
- {
- if (!Online())
- {
- offline = true;
- return teams;
- }
- try
- {
- raw = wc.DownloadString(url);
- break;
- }
- catch
- {
- }
- if (i++ > 20)
- {
- soccerwayUnreachable = true;
- return teams;
- }
- }
- raw = raw.Remove(raw.IndexOf("<\\/ul"));
- List<string> rawTeams = raw.Split(new string[] { "<li" }, StringSplitOptions.RemoveEmptyEntries).ToList();
- rawTeams.Remove(rawTeams.First());
- foreach (string s in rawTeams)
- {
- int index1 = s.IndexOf("<a href=\\\"\\") + "<a href=\\\"\\".Length;
- int index2 = s.IndexOf("\\\" class=");
- string urlOfTeam = s.Substring(index1, index2 - index1);
- urlOfTeam = urlOfTeam.Replace("\\", "");
- index1 = s.IndexOf("class=\\\"team\\\" >") + "class=\\\"team\\\" >".Length;
- index2 = s.IndexOf("<\\/a>");
- string name = s.Substring(index1, index2 - index1);
- teams.Add(new Team() { LeagueId = l.Id, Name = Regex.Unescape(name), Url = urlOfTeam });
- }
- // " class=\\\" \\\" ><div class=\\\"row\\\"><a href=\\\"\\/teams\\/albania\\/ks-apolonia-fier\\/\\\" class=\\\"team\\\" >Apolonia Fier<\\/a><\\/div><\\/li>"
- return teams;
- }
- string GetTeamIdFromUrl(string url)
- {
- WebClient wc = new WebClient();
- wc.Headers.Add("User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.77 Safari/535.7");
- string html = "";
- int i = 0;
- while (true)
- {
- if (!Online())
- {
- offline = true;
- return "";
- }
- try
- {
- html = wc.DownloadString("http://www.soccerway.com" + url);
- break;
- }
- catch
- {
- }
- if (i++ > 20)
- {
- soccerwayUnreachable = true;
- return "";
- }
- }
- int index1 = html.IndexOf("\"team_id\":") + "\"team_id\":".Length;
- int index2 = html.IndexOf(",\"competition_id\"");
- html = html.Substring(index1, index2 - index1);
- return html;
- }
- string GetTeamStatsTable(Team t)
- {
- string url = getStatsUrlStart + GetTeamIdFromUrl(t.Url) + getStatsUrlEnd;
- if (SomethingDown())
- {
- return "";
- }
- WebClient wc = new WebClient();
- wc.Headers.Add("User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.77 Safari/535.7");
- string html = "";
- int i = 0;
- while (true)
- {
- if (!Online())
- {
- offline = true;
- return "";
- }
- try
- {
- html = wc.DownloadString(url);
- break;
- }
- catch
- {
- }
- if (i++ > 20)
- {
- soccerwayUnreachable = true;
- return "";
- }
- }
- int index1 = html.IndexOf("<table");
- int index2 = html.IndexOf("<\\/table>") + "<\\/table>".Length;
- if (index1 == -1)
- return TEAM_HAS_NO_STATS;
- html = html.Substring(index1, index2 - index1);
- return Regex.Unescape(html);
- }
- const string TEAM_HAS_NO_STATS = "<p>This team has no stats table.</p>";
- private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
- {
- List<League> leagues = new List<League>();
- foreach (League l in allLeagues)
- {
- Country c = (Country)comboBox1.SelectedItem;
- if (l.CountryId == c.Id)
- leagues.Add(l);
- }
- comboBox2.Items.Clear();
- foreach (League l in leagues)
- {
- comboBox2.Items.Add(l);
- }
- comboBox2.SelectedIndex = 0;
- }
- private void button2_Click(object sender, EventArgs e)
- {
- League toBeAddedLeague = (League)comboBox2.SelectedItem;
- if (listBox1.Items.Contains(toBeAddedLeague))
- {
- MessageBox.Show("Already added!");
- return;
- }
- monitoredLeagues.Add((League)comboBox2.SelectedItem);
- PopulateListBox();
- }
- private void Form1_FormClosing(object sender, FormClosingEventArgs e)
- {
- if (started && !everythingFinishedUpdating)
- {
- MessageBox.Show("Can not exit while updating! Please wait.");
- e.Cancel = true;
- return;
- }
- SaveLocation();
- SerializeLeagues(monitoredLeagues, "Monitored.serialized");
- }
- private void button1_Click(object sender, EventArgs e)
- {
- if (listBox1.SelectedItems.Count == 0)
- {
- MessageBox.Show("Nothing selected!");
- return;
- }
- foreach(object obj in listBox1.SelectedItems)
- {
- monitoredLeagues.Remove((League)obj);
- }
- PopulateListBox();
- }
- // Creates a notebook if it doesnt exist
- // deletes all notes
- // adds a new note for every team
- // returns the error if any, if "" then it passed ok
- string UpdateNotebook(League l, List<TeamWithStats> teams)
- {
- int i = 0;
- while (true)
- {
- if (!Online())
- {
- offline = true;
- return "";
- }
- try
- {
- String username = textBox1.Text;
- String password = textBox2.Text;
- String consumerKey = EVERNOTE_CONSUMER_KEY;
- String consumerSecret = EVERNOTE_CONSUMER_SECRET;
- String evernoteHost = EVERNOTE_HOST;
- String edamBaseUrl = "http://" + evernoteHost; // do NOT use https - some kind of certificate errors
- Uri userStoreUrl = new Uri(edamBaseUrl + "/edam/user");
- TTransport userStoreTransport = new THttpClient(userStoreUrl);
- TProtocol userStoreProtocol = new TBinaryProtocol(userStoreTransport);
- UserStore.Client userStore = new UserStore.Client(userStoreProtocol);
- bool versionOK =
- userStore.checkVersion(CLIENT_NAME,
- Evernote.EDAM.UserStore.Constants.EDAM_VERSION_MAJOR,
- Evernote.EDAM.UserStore.Constants.EDAM_VERSION_MINOR);
- if (!versionOK)
- {
- throw new Exception(ERROR_VERSION);
- }
- AuthenticationResult authResult = null;
- String noteStoreUrlat = "";
- try
- {
- //authResult = userStore.authenticate(username, password,
- // consumerKey, consumerSecret);
- noteStoreUrlat = userStore.getNoteStoreUrl(authToken);
- }
- catch
- {
- throw new Exception(ERROR_RUNTIME_LOGIN);
- }
- //User user = authResult.User;
- //String authToken = authResult.AuthenticationToken;
- //String authToken = "";
- //Uri noteStoreUrl = new Uri(edamBaseUrl + "/edam/note/" + user.ShardId);
- Uri noteStoreUrl = new Uri(noteStoreUrlat);
- TTransport noteStoreTransport = new THttpClient(noteStoreUrl);
- TProtocol noteStoreProtocol = new TBinaryProtocol(noteStoreTransport);
- NoteStore.Client noteStore = new NoteStore.Client(noteStoreProtocol);
- List<Notebook> notebooks = noteStore.listNotebooks(authToken);
- string openNotebookGuid = "";
- foreach (Notebook n in notebooks)
- {
- if (n.Name == l.QualifiedName)
- openNotebookGuid = n.Guid;
- }
- if (openNotebookGuid == "")
- {
- // we need to create one
- openNotebookGuid = noteStore.createNotebook(authToken, new Notebook() { Name = l.QualifiedName }).Guid;
- }
- // We get all the notes in the league notebook
- NoteList nl = noteStore.findNotes(authToken, new NoteFilter() { NotebookGuid = openNotebookGuid }, 0, 999); // 999 is the maximum number of teams possible in a league...
- // and delete them
- foreach (Note n in nl.Notes)
- {
- noteStore.deleteNote(authToken, n.Guid);
- }
- // and now add the new ones
- foreach (TeamWithStats t in teams)
- {
- updateMsg = "Updating team " + l.QualifiedName + " " + t.Name + " to evernote";
- Note newTeamStatsNote = new Note();
- newTeamStatsNote.NotebookGuid = openNotebookGuid;
- newTeamStatsNote.Title = t.Name.Trim();
- newTeamStatsNote.Content = t.Stats == TEAM_HAS_NO_STATS ? FormartAsEvernote(t.Stats, false) : FormartAsEvernote(t.Stats, true);
- noteStore.createNote(authToken, newTeamStatsNote);
- }
- return ""; //"" is everything is ok == no error
- }
- catch(Exception e)
- {
- if (e.Message == ERROR_RUNTIME_LOGIN || e.Message == ERROR_VERSION)
- throw;
- }
- if (i++ > 20)
- {
- evernoteUnreachable = true;
- return "";
- }
- }
- }
- bool started = false;
- private void button3_Click_1(object sender, EventArgs e)
- {
- if (listBox1.Items.Count == 0)
- {
- MessageBox.Show("Please select some leagues to monitor. Then click this!");
- return;
- }
- //if (textBox1.Text.Trim() == "" || textBox2.Text.Trim() == "")
- //{
- // MessageBox.Show("Please enter username and password.");
- // return;
- //}
- if (!CanLogin(textBox1.Text, textBox2.Text))
- {
- MessageBox.Show("Cant login. You have mistyped your username or password.");
- return;
- }
- groupBox1.Enabled = false;
- groupBox2.Enabled = false;
- groupBox3.Enabled = false;
- button4.Enabled = true;
- button5.Enabled = true;
- if (checkBox1.Checked)
- {
- SavePassword();
- }
- else
- {
- ClearPassword();
- }
- started = true;
- BeginWebScraping();
- textBox3.Text = "Please wait...";
- }
- void SetPassword()
- {
- if (Properties.Settings.Default.PasswordSet)
- {
- textBox1.Text = Properties.Settings.Default.Username;
- textBox2.Text = Properties.Settings.Default.Password;
- }
- }
- void SetLocation()
- {
- if (Properties.Settings.Default.LocationSet)
- {
- this.Location = new Point(Properties.Settings.Default.LocationX, Properties.Settings.Default.LocationY);
- }
- }
- void SaveLocation()
- {
- Properties.Settings.Default.LocationSet = true;
- Properties.Settings.Default.LocationX = this.Location.X;
- Properties.Settings.Default.LocationY = this.Location.Y;
- Properties.Settings.Default.Save();
- }
- void SavePassword()
- {
- Properties.Settings.Default.PasswordSet = true;
- Properties.Settings.Default.Username = textBox1.Text;
- Properties.Settings.Default.Password = textBox2.Text;
- Properties.Settings.Default.Save();
- }
- void ClearPassword()
- {
- Properties.Settings.Default.PasswordSet = false;
- Properties.Settings.Default.Save();
- }
- DateTime nextUpdate;
- // this is NOT the forms timer - intentional!
- Timer everythingUpdateTimer;
- // THIS IS CALLED BY "UPDATERIGHTNOW" BUTTON BE VERY CAREFUL ABOUT MODIFYING ***ANYTHING***
- void BeginWebScraping()
- {
- // here we try again, if any of these are true, they will be made so by methods that access the net further down
- offline = false;
- evernoteUnreachable = false;
- soccerwayUnreachable = false;
- UpdateAllLeaguesToEvernote();
- everythingUpdateTimer = new Timer() { Interval = 500 };
- everythingUpdateTimer.Tick += delegate
- {
- // not yet
- if (DateTime.Compare(DateTime.Now, nextUpdate) < 0)
- {
- if (SomethingDown())
- {
- // The last update FAILED, we will try again after an hour
- if (DateTime.Compare(DateTime.Now.Add(new TimeSpan(0, 1, 0)), nextUpdate) < 0)
- {
- TimeSpan tsFailed = new TimeSpan(0, 1, 0);
- nextUpdate = DateTime.Now.Add(tsFailed);
- }
- }
- return;
- }
- else // let's try again because it's time
- {
- offline = false;
- evernoteUnreachable = false;
- soccerwayUnreachable = false;
- }
- if(everythingFinishedUpdating) // theoretically this could happen if there are way too many leagues and an update time of 1 hour
- UpdateAllLeaguesToEvernote();
- TimeSpan ts = new TimeSpan((int)numericUpDown1.Value, 0, 0);
- nextUpdate = DateTime.Now.Add(ts);
- };
- everythingUpdateTimer.Start();
- // DO NOT INCREASE THE 480 HOUR LIMIT! IT WILL OVERFLOW INT32.MAXVALUE
- TimeSpan initialts = new TimeSpan((int)numericUpDown1.Value, 0, 0); // same as ts
- nextUpdate = DateTime.Now.Add(initialts);
- }
- const string EVERNOTE_CONSUMER_KEY = "soccerwayscraper";
- const string EVERNOTE_CONSUMER_SECRET = "5ff315a2191d98d1";
- //const string EVERNOTE_HOST = "evernote.com"; // to switch between the sanbox.evernote.com
- const string EVERNOTE_HOST = "www.evernote.com"; // to switch between the sanbox.evernote.com
- const string CLIENT_NAME = "Simple note storer v1.0"; // coulnt think of anything better
- const string ERROR_VERSION = "Evernote has changed it's API protocol, this application needs to be updated.";
- const string ERROR_RUNTIME_LOGIN = "Failed to login. Perhaps the password is changed while this program was running or the user deleted."; // not the same as simple login
- bool CanLogin(string user, string pass)
- {
- int i = 0;
- while (true) // try has a return
- {
- if (!Online())
- {
- textBox3.Text = "No internet.";
- return false;
- }
- else
- {
- textBox3.Text = "Please add leagues, enter username and password and click \"Begin web scraping\".";
- }
- try
- {
- String username = user;
- String password = pass;
- String consumerKey = EVERNOTE_CONSUMER_KEY;
- String consumerSecret = EVERNOTE_CONSUMER_SECRET;
- String evernoteHost = EVERNOTE_HOST;
- String edamBaseUrl = "http://" + evernoteHost;
- Uri userStoreUrl = new Uri(edamBaseUrl + "/edam/user");
- TTransport userStoreTransport = new THttpClient(userStoreUrl);
- TProtocol userStoreProtocol = new TBinaryProtocol(userStoreTransport);
- UserStore.Client userStore = new UserStore.Client(userStoreProtocol);
- bool versionOK =
- userStore.checkVersion(CLIENT_NAME,
- Evernote.EDAM.UserStore.Constants.EDAM_VERSION_MAJOR,
- Evernote.EDAM.UserStore.Constants.EDAM_VERSION_MINOR);
- if (!versionOK)
- {
- throw new Exception(ERROR_VERSION);
- }
- AuthenticationResult authResult = null;
- try
- {
- //authResult = userStore.authenticate(username, password,consumerKey, consumerSecret);
- }
- catch
- {
- return false;
- }
- return true;
- }
- catch(Exception e)
- {
- if (e.Message == ERROR_VERSION)
- throw;
- }
- if (i++ > 20)
- throw new Exception("Failed to connect to evernote. This application can not be started.");
- }
- }
- // There was a prettier method I used a lot, but it gave a lot of false positives, this works better
- bool Online()
- {
- try
- {
- string myAddress = "www.google.com";
- IPAddress[] addresslist = Dns.GetHostAddresses(myAddress);
- if (addresslist[0].ToString().Length > 6)
- {
- return true;
- }
- else
- return false;
- }
- catch
- {
- return false;
- }
- }
- private void button4_Click(object sender, EventArgs e)
- {
- if (!everythingFinishedUpdating)
- {
- MessageBox.Show("Can not exit while updating! Please wait.");
- return;
- }
- Application.Restart();
- }
- string updateMsg;
- bool offline = false;
- bool evernoteUnreachable = false;
- bool soccerwayUnreachable = false;
- private void timer1_Tick(object sender, EventArgs e)
- {
- if (!started)
- return;
- if (everythingFinishedUpdating)
- {
- TimeSpan timeToNextUpdate = nextUpdate.Subtract(DateTime.Now);
- textBox3.Text = "";
- if (offline)
- textBox3.Text += "Offline.\r\n";
- if (evernoteUnreachable)
- textBox3.Text += "Evernote unreachable.\r\n";
- if (soccerwayUnreachable)
- textBox3.Text += "Soccerway unreachable.\r\n";
- textBox3.Text += "The next update will happen after " + timeToNextUpdate.Days + " days and " + timeToNextUpdate.Hours + ":" + timeToNextUpdate.Minutes + ":" + timeToNextUpdate.Seconds;
- }
- else
- {
- textBox3.Text = "Working right now. Please be patient! " + updateMsg;
- }
- this.Refresh();
- }
- private void button5_Click(object sender, EventArgs e)
- {
- if (!everythingFinishedUpdating)
- {
- MessageBox.Show("Update is already under way!");
- return;
- }
- everythingUpdateTimer.Stop();
- BeginWebScraping();
- }
- }
- [Serializable]
- struct Country
- {
- public int Id;
- public string Name;
- public override string ToString()
- {
- return Name;
- }
- }
- [Serializable]
- struct League : IEquatable<League>
- {
- public string Id; // "Other" leagues are like IDs "0-9", which is NaN
- public string Name;
- public int CountryId;
- public string CountryName;
- public string QualifiedName
- {
- get { return this.ToString(); }
- }
- public override string ToString()
- {
- return CountryName + ": " + Name;
- }
- public bool Equals(League l)
- {
- if (l.Id == this.Id)
- return true;
- return false;
- }
- }
- class Team
- {
- public string Url;
- public string Name;
- public string LeagueId; // Intentional string
- public override string ToString()
- {
- return Name;
- }
- }
- class TeamWithStats : Team
- {
- public string Stats; //html
- public TeamWithStats(Team t, string stats)
- {
- Stats = stats;
- base.Name = t.Name;
- base.Url = t.Url;
- base.LeagueId = t.LeagueId;
- }
- }
- // Binārā serializācija objektu instanču klonēšanai
- static class Ext // Extensions
- {
- // Vienkārš objektu klonētajs, kurš izmanto serializāciju
- public static object Clone(object obj)
- {
- object objResult = null;
- using (MemoryStream ms = new MemoryStream())
- {
- BinaryFormatter bf = new BinaryFormatter();
- bf.Serialize(ms, obj);
- ms.Position = 0;
- objResult = bf.Deserialize(ms);
- }
- return objResult;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement