![]() ![]() |
Aug 10 2008, 11:47 AM
Post
#1
|
|
![]() Group: Members Posts: 10,620 Joined: 23-February 06 From: Houston, TX Member No.: 48 |
I've been working on a few projects lately and figured a couple of the things I wrote might be useful to others. So use this thread as a "fun snippets of code to share" thread.
|
|
|
|
Aug 10 2008, 12:00 PM
Post
#2
|
|
![]() Group: Members Posts: 10,620 Joined: 23-February 06 From: Houston, TX Member No.: 48 |
I'm working on a kind of home data server, for sports scores, weather updates, calendar reminders, etc etc. This "SportsData" class is completely self enclosed and screen scrapes the ESPN mobile website (much smaller, so much faster) for sports scores. To use it, create a new instance of the SportsData class and make sure you set the WatchedTeams string array to the teams you want updates on. The identifier string should be the sport + team (3 letters each). So if I wanted to watch the astros and rangers scores, my string array would be {"MLBHOU","MLBTEX"}.
CODE using System;
using System.Collections; using System.Net; using System.Threading; using System.Text; namespace DataServer { public enum GameType { MLB, NFL, NBA, NCAAF, UNKNOWN }; class Game { public string gameId; public GameType gameType; public string homeTeam; public string awayTeam; public int homeScore; public int awayScore; public string gamePeriod; public string gameStart; public string lastChange; public Game() { gameId = ""; gameType = GameType.UNKNOWN; homeTeam = ""; awayTeam = ""; homeScore = -1; awayScore = -1; gamePeriod = ""; gameStart = ""; lastChange = ""; } } class SportsData { public delegate void NewGameDataDelegate(Game g); public event NewGameDataDelegate NewGameData; public bool isRunning; public string DataUrl; public int UpdateInterval; ArrayList gameData; ArrayList watchedTeams; protected void OnNewGameData(Game g) { if (NewGameData != null) NewGameData(g); } public SportsData() { isRunning = false; UpdateInterval = 10; DataUrl = "http://mobileapp.espn.go.com/wireless/scoreboard"; watchedTeams = new ArrayList(); gameData = new ArrayList(); } public SportsData(int interval, string url, string[] teamstowatch, bool start) { isRunning = start; UpdateInterval = interval; DataUrl = url; watchedTeams = new ArrayList(); foreach (string s in teamstowatch) watchedTeams.Add(s); gameData = new ArrayList(); } public void Start() { isRunning = true; Thread t = new Thread(new ThreadStart(RunThread)); t.Start(); } public void Stop() { isRunning = false; } void RunThread() { while (isRunning) { System.Threading.Thread.Sleep(UpdateInterval*1000); WebClient wc = new WebClient(); string parse = wc.DownloadString(DataUrl); int secindex = parse.IndexOf("row\">"); int removeindex = 0; string sectemp = ""; ArrayList sportstoparse = new ArrayList(); while (secindex > 0) { parse = parse.Remove(0, secindex + 5); sectemp = parse; removeindex = sectemp.IndexOf("row\">"); secindex = parse.IndexOf("row\">"); if (secindex > 0) sectemp = sectemp.Remove(removeindex + 5, sectemp.Length - removeindex - 5); sportstoparse.Add(sectemp); } ArrayList gamearray = new ArrayList(); ArrayList tmpgames = new ArrayList(); foreach (string s in sportstoparse) { foreach (Game g in ParseSport(s)) gamearray.Add(g); } CheckForNewData(gamearray); } } void CheckForNewData(ArrayList newData) { Game tmpgame = new Game(); foreach (Game g in newData) { if (!isGameWatched(g)) continue; tmpgame = GetGameById(g.gameId); if (tmpgame == null) { gameData = newData; return; } if (g.gameId == tmpgame.gameId) { if (!CompareGames(g, tmpgame)) { StringBuilder chg = new StringBuilder(); if (g.awayScore != tmpgame.awayScore) chg.AppendFormat(" {0} +{1}", g.awayTeam, g.awayScore-tmpgame.awayScore); if (g.homeScore != tmpgame.homeScore) chg.AppendFormat(" {0} +{1}", g.homeTeam, g.homeScore-tmpgame.homeScore); if (g.gamePeriod == "F" && tmpgame.gamePeriod != "F") chg.AppendFormat(" Game Over"); Game retGame = g; retGame.lastChange = chg.ToString(); gameData = newData; OnNewGameData(g); return; } } } } bool isGameWatched(Game g) { if (watchedTeams.Contains(g.gameType + g.homeTeam) || watchedTeams.Contains(g.gameType + g.awayTeam)) return true; return false; } bool CompareGames(Game game1, Game game2) { if (game1.homeScore != game2.homeScore) return false; if (game1.awayScore != game2.awayScore) return false; if (game1.gamePeriod.Length <= 0 && game2.gamePeriod.Length > 0) return false; if (game1.gamePeriod == "F" && game2.gamePeriod != "F") return false; return true; } Game GetGameById(string id) { Game retGame = new Game(); foreach (Game g in gameData) { if (g.gameId == id) return g; } return null; } static ArrayList ParseSport(string rawstring) { ArrayList retGames = new ArrayList(); Game tmpGame = new Game(); GameType type; switch (rawstring.Substring(0, 3)) { case "MLB": type = GameType.MLB; break; case "NFL": type = GameType.NFL; break; case "NBA": type = GameType.NBA; break; default: type = GameType.UNKNOWN; break; } string splitstring = rawstring.Replace("gameId", "~"); splitstring = splitstring.Remove(0, splitstring.IndexOf('~') + 1); string tmpparse = ""; foreach (string s in splitstring.Split('~')) { tmpGame = new Game(); tmpGame.gameType = type; tmpparse = s.Remove(0, s.IndexOf('>') + 1); int x = tmpparse.IndexOf('<'); tmpparse = tmpparse.Remove(x, tmpparse.Length - x); string[] scoreparse = new string[1]; if (tmpparse.Contains("at")) { tmpGame.awayTeam = tmpparse.Substring(0, 3); tmpGame.homeTeam = tmpparse.Substring(7, 3); tmpGame.gameStart = tmpparse.Substring(11, tmpparse.Length - 11); } else { scoreparse = tmpparse.Split(' '); if (scoreparse.Length >= 5) { tmpGame.awayTeam = scoreparse[0]; Int32.TryParse(scoreparse[1], out tmpGame.awayScore); tmpGame.homeTeam = scoreparse[2]; Int32.TryParse(scoreparse[3], out tmpGame.homeScore); tmpGame.gamePeriod = scoreparse[4]; if (scoreparse.Length >= 6) { tmpGame.gamePeriod += " " + scoreparse[5]; } } } tmpGame.gameId = tmpGame.gameType + tmpGame.awayTeam + tmpGame.homeTeam; retGames.Add(tmpGame); } return retGames; } } } |
|
|
|
Aug 10 2008, 06:28 PM
Post
#3
|
|
|
Group: Members Posts: 15 Joined: 5-January 07 Member No.: 549 |
I prefer coding for an hourly rate, but whatever floats your boat...
|
|
|
|
Aug 10 2008, 06:47 PM
Post
#4
|
|
![]() Group: Members Posts: 10,620 Joined: 23-February 06 From: Houston, TX Member No.: 48 |
I do both
|
|
|
|
![]() ![]() |
| Lo-Fi Version | Time is now: 24th October 2025 - 02:20 PM |