IPB

Welcome Guest ( Log In | Register )


> sports data feed
impala454
post Sep 18 2006, 05:43 PM
Post #1





Group: Members
Posts: 10,620
Joined: 23-February 06
From: Houston, TX
Member No.: 48



anybody know of a sports scores data feed that's free?
Go to the top of the page
 
+Quote Post
 
Start new topic
Replies
impala454
post Sep 18 2006, 09:51 PM
Post #2





Group: Members
Posts: 10,620
Joined: 23-February 06
From: Houston, TX
Member No.: 48



like i said... it's quick, shit code. not pretty but it works. done in C#.NET

CODE
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.IO;
using System.Data.SqlClient;
using System.Data;

namespace EspnScoreboardParser
{
   class Program
   {
       public struct Game
       {
           public string HomeTeam;
           public string AwayTeam;
           public int HomeTeamScore;
           public int AwayTeamScore;
           public int HomeTeamRank;
           public int AwayTeamRank;
           public DateTime DateOfGame;
       }

       static void Main(string[] args)
       {
           if (args.Length < 2)
           {
               Console.WriteLine("Usage: EspnScoreboardParser.exe InputFile.txt OutputFile.txt");
               return;
           }

           StringBuilder input = new StringBuilder();
           StreamReader sr = new StreamReader(args[0]);

           input.Append(sr.ReadToEnd());

           ArrayList datechangeindexes = new ArrayList();
           ArrayList games = new ArrayList();
           ArrayList teams = new ArrayList();
           ArrayList homeranks = new ArrayList();
           ArrayList awayranks = new ArrayList();
           ArrayList hometeams = new ArrayList();
           ArrayList awayteams = new ArrayList();
           ArrayList awayscores = new ArrayList();
           ArrayList homescores = new ArrayList();
           ArrayList gamedates = new ArrayList();
           int tmpindex = 0;
           int previndex = 0;
           int count = 0;
           string tmpstr = input.ToString();
           DateTime startdatetime = new DateTime();

           while (tmpindex >= 0)
           {
               tmpindex = tmpstr.IndexOf("dateChange", previndex);
               previndex = tmpindex + 1;
               count++;
               if (count <= 1)
               {
                   int enddatestr = tmpstr.IndexOf("-</div", tmpindex);
                   enddatestr = enddatestr - tmpindex - 13;
                   string tmpdateparse = tmpstr.Substring(tmpindex + 13, enddatestr);
                   DateTime.TryParse(tmpdateparse,out startdatetime);
               }
               else
               {
                   if(tmpindex > 0)
                       datechangeindexes.Add(tmpindex);
               }
               //Console.WriteLine(tmpindex);
           }

           count = 0;
           tmpindex = 0;
           previndex = 0;
           int tmpstartindex = 0;
           DateTime currentgamedate = startdatetime;
           int gametogggle = 0;

           while(tmpindex >= 0)
           {
               tmpindex = tmpstr.IndexOf("teamId=", previndex);
               previndex = tmpindex+1;

               try
               {
                   tmpstartindex = tmpstr.IndexOf("\">", tmpindex) + 2;
               }
               catch(Exception ex1)
               {
                   //Console.WriteLine(ex1.Message);
                   break;
               }
               
               string stringtoparse = tmpstr.Substring(tmpstartindex, tmpstr.IndexOf("</a>", tmpindex) - tmpstartindex);

               for(int dateindex=datechangeindexes.Count-1; dateindex>=0; dateindex--)
               {
                   if (tmpindex >= (int)datechangeindexes[dateindex])
                   {
                       currentgamedate = startdatetime.AddDays(dateindex+1);
                       break;
                   }
                   else
                       currentgamedate = startdatetime;
               }

               gametogggle++;
               int rank = -1;

               if (stringtoparse.Contains("("))
               {
                   string tmprank = stringtoparse.Substring(stringtoparse.IndexOf("(") + 1, stringtoparse.IndexOf(")") - stringtoparse.IndexOf("(")-1);
                   if(Int32.TryParse(tmprank, out rank))
                       stringtoparse = stringtoparse.Substring(stringtoparse.IndexOf(")") + 1).Trim();
               }

               if (gametogggle % 2 == 1)
               {
                   //Console.WriteLine(currentgamedate.ToString("ddd") + ": A: " + stringtoparse);
                   awayteams.Add(stringtoparse);
                   awayranks.Add(rank);
                   gamedates.Add(currentgamedate);
               }
               else
               {
                   //Console.WriteLine(currentgamedate.ToString("ddd") + ": H: " + stringtoparse + "\n=================");
                   hometeams.Add(stringtoparse);
                   homeranks.Add(rank);
               }
             
           }

           //Console.WriteLine("------------------------------------------------------------");

           count = 0;
           tmpindex = 0;
           previndex = 0;
           tmpstartindex = 0;
           currentgamedate = startdatetime;
           gametogggle = 0;

           while (tmpindex >= 0)
           {
               tmpindex = tmpstr.IndexOf("atot", previndex);
               previndex = tmpindex + 1;

               try
               {
                   tmpstartindex = tmpstr.IndexOf("\">", tmpindex) + 2;
               }
               catch(Exception ex2)
               {
                   //Console.WriteLine(ex2.Message);
                   break;
               }

               string stringtoparse = tmpstr.Substring(tmpstartindex, tmpstr.IndexOf("</td>", tmpindex) - tmpstartindex);

               for (int dateindex = datechangeindexes.Count - 1; dateindex >= 0; dateindex--)
               {
                   if (tmpindex >= (int)datechangeindexes[dateindex])
                   {
                       currentgamedate = startdatetime.AddDays(dateindex + 1);
                       break;
                   }
                   else
                       currentgamedate = startdatetime;
               }

               awayscores.Add(Convert.ToInt32(stringtoparse));
               //Console.WriteLine(currentgamedate.ToString("ddd") + ": A: " + stringtoparse);

           }

           count = 0;
           tmpindex = 0;
           previndex = 0;
           tmpstartindex = 0;
           currentgamedate = startdatetime;
           gametogggle = 0;

           while (tmpindex >= 0)
           {
               tmpindex = tmpstr.IndexOf("htot", previndex);
               previndex = tmpindex + 1;

               try
               {
                   tmpstartindex = tmpstr.IndexOf("\">", tmpindex) + 2;
               }
               catch(Exception ex3)
               {
                   //Console.WriteLine(ex3.Message);
                   break;
               }

               string stringtoparse = tmpstr.Substring(tmpstartindex, tmpstr.IndexOf("</td>", tmpindex) - tmpstartindex);

               for (int dateindex = datechangeindexes.Count - 1; dateindex >= 0; dateindex--)
               {
                   if (tmpindex >= (int)datechangeindexes[dateindex])
                   {
                       currentgamedate = startdatetime.AddDays(dateindex + 1);
                       break;
                   }
                   else
                       currentgamedate = startdatetime;
               }

               homescores.Add(Convert.ToInt32(stringtoparse));
               //Console.WriteLine(currentgamedate.ToString("ddd") + ": H: " + stringtoparse);
           }

           Game tmpgame = new Game();

           for (int x = 0; x < homescores.Count; x++)
           {
               tmpgame = new Game();
               tmpgame.AwayTeam = (string)awayteams[x];
               tmpgame.HomeTeam = (string)hometeams[x];
               tmpgame.AwayTeamScore = (int)awayscores[x];
               tmpgame.HomeTeamScore = (int)homescores[x];
               tmpgame.AwayTeamRank = (int)awayranks[x];
               tmpgame.HomeTeamRank = (int)homeranks[x];
               tmpgame.DateOfGame = (DateTime)gamedates[x];
               games.Add(tmpgame);
           }

           foreach(Game g in games)
           {
               Console.WriteLine("{6}:  ({5}){2}={3} @ ({4}){0}={1}", g.HomeTeam, g.HomeTeamScore, g.AwayTeam,
                   g.AwayTeamScore, g.HomeTeamRank, g.AwayTeamRank, g.DateOfGame.ToString("ddd, MMM dd, yyyy"));
           }
           Console.WriteLine("{0} games parsed.\nChuck rules! ESPN drools!", games.Count);

           StreamWriter sw = new StreamWriter(args[1]);

           sw.WriteLine("Date,HomeRank,HomeTeam,HomeScore,AwayRank,AwayTeam,AwayScore");
           foreach (Game g in games)
           {
               sw.WriteLine("{0},{1},{2},{3},{4},{5},{6}", g.DateOfGame.ToString("yyyy-MM-dd"), g.HomeTeamRank,
                   g.HomeTeam, g.HomeTeamScore, g.AwayTeamRank, g.AwayTeam, g.AwayTeamScore);
           }
           sw.Close();

           SqlConnection db = new SqlConnection("younoaccessmysqlserver!!");
           db.Open();

           foreach (Game g in games)
           {
               InsertSql(db, g);
           }
           db.Close();
       }

       public static void InsertSql(SqlConnection db, Game g)
       {
           StringBuilder insert = new StringBuilder();
           insert.AppendFormat("insert into atablenameusedtobehere values ('{0}', '{1}')", g.HomeTeam, g.AwayTeam);
           SqlCommand comm = new SqlCommand(insert.ToString(), db);
           comm.ExecuteNonQuery();
       }
   }
}
Go to the top of the page
 
+Quote Post

Posts in this topic


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



Lo-Fi Version Time is now: 20th August 2026 - 03:39 PM
Skin made by: skeedio.com