Esempio n. 1
0
        public void FillDB()
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_allEventsApiUrl);

            request.Method = "GET";
            string json;

            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                Stream       dataStream = response.GetResponseStream();
                StreamReader reader     = new StreamReader(dataStream);
                json = reader.ReadToEnd();
                reader.Close();
                dataStream.Close();
            }
            int     added = 0;
            JObject obj;

            try
            {
                obj = JObject.Parse(json);
                foreach (var item in obj["events"])
                {
                    JObject itemObj   = ((JObject)item);
                    JObject eventObj  = (JObject)itemObj["event"];
                    Event   ev        = new Event();
                    string  eventData = eventObj["id"].ToObject <string>();

                    ev.Home        = eventObj["homeName"].ToObject <string>();
                    ev.Away        = eventObj["awayName"].ToObject <string>();
                    ev.Start       = DateHelpers.TimeStampToDateTime(eventObj["start"].ToObject <double>());
                    ev.PrematchEnd = DateHelpers.TimeStampToDateTime(eventObj["prematchEnd"].ToObject <double>());
                    if (ev.IsLive())
                    {
                        continue;
                    }
                    JArray paths = ((JArray)eventObj["path"]);
                    if (!mappings.SportMappings.ContainsKey(paths[0]["name"].ToObject <string>()))
                    {
                        continue;
                    }
                    Sport sport = mappings.SportMappings[paths[0]["name"].ToObject <string>()].DBCopy(context);
                    if (sport != null)
                    {
                        ev.Sport = sport;
                    }
                    else
                    {
                        break;
                    }
                    if (paths.Count == 2)
                    {
                        ev.League = paths[1]["name"].ToObject <string>();
                    }
                    else if (paths.Count == 3)
                    {
                        ev.Region = paths[1]["name"].ToObject <string>();
                        ev.League = paths[2]["name"].ToObject <string>();
                    }
                    Event dbEvent = DBFinder.FindEvent(ev).DBCopy(context);
                    int   eventID;
                    if (dbEvent != null)
                    {
                        continue;
                    }
                    else
                    {
                        context.Events.Add(ev);
                        context.SaveChanges();
                        EventSiteData data = new EventSiteData();
                        dbEvent      = ev;
                        data.EventID = ev.ID;
                        data.SiteID  = mappings.ID;
                        data.Data    = eventData;
                        context.EventSiteDatas.Add(data);
                        Int32.TryParse(eventData, out eventID);
                    }
                    if (eventID == 0)
                    {
                        continue;
                    }
                    string offersUrl = GetEventOffersApiUrl(eventID);
                    request = (HttpWebRequest)WebRequest.Create(offersUrl);
                    try
                    {
                        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                        {
                            Stream       dataStream = response.GetResponseStream();
                            StreamReader reader     = new StreamReader(dataStream);
                            json = reader.ReadToEnd();
                            reader.Close();
                            dataStream.Close();
                        }
                    }
                    catch (WebException ex)
                    {
                        continue;
                    }
                    obj = JObject.Parse(json);
                    foreach (JObject offer in obj["betoffers"])
                    {
                        string betOfferType = ((JObject)offer["betOfferType"])["name"].ToObject <string>();
                        string betTarget    = ((JObject)offer["criterion"])["label"].ToObject <string>();
                        if (!mappings.BetTypeMappings.ContainsKey(betOfferType))
                        {
                            continue;
                        }
                        if (!mappings.BetTargetMappings.ContainsKey(betTarget))
                        {
                            continue;
                        }
                        BetType   type     = mappings.BetTypeMappings[betOfferType].DBCopy(context);
                        BetTarget target   = mappings.BetTargetMappings[betTarget].DBCopy(context);
                        BetOffer  betOffer = new BetOffer();
                        betOffer.BetTypeID = type.ID;
                        if (target != null)
                        {
                            betOffer.BetTargetID = target.ID;
                        }
                        dbEvent.BetOffers.Add(betOffer);
                        context.SaveChanges();
                        foreach (JObject outcome in offer["outcomes"])
                        {
                            Outcome o = new Outcome();
                            if (!mappings.OutcomeTypeMappings.ContainsKey(outcome["label"].ToObject <string>()))
                            {
                                continue;
                            }
                            o.OutcomeType = mappings.OutcomeTypeMappings[outcome["label"].ToObject <string>()].DBCopy(context);
                            o.Odds        = outcome["oddsAmerican"].ToObject <string>();
                            o.BetOfferID  = betOffer.ID;
                            if (outcome.ContainsKey("line"))
                            {
                                o.Line = outcome["line"].ToObject <int>();
                            }
                            context.Outcomes.Add(o);
                        }
                    }
                    Console.WriteLine("Event added: " + dbEvent.Home + " - " + dbEvent.Away);
                    added++;
                }
                context.SaveChanges();
                Console.WriteLine("Total: " + added);
            }
            catch (Exception ex)
            {
                Console.WriteLine("888 invalid json, or changed json formatting");
            }
        }
Esempio n. 2
0
 public void FillDB()
 {
     Data   = null;
     socket = new WebSocket(wsURL);
     socket.Connect();
     socket.OnMessage += Socket_OnMessage;
     socket.Send(sessionReq);
     while (!dataArrived)
     {
         Task.Delay(1000).ConfigureAwait(false);
     }
     foreach (var sport in Data["sport"].First)
     {
         if (!mappings.SportMappings.ContainsKey(sport["alias"].ToObject <string>()))
         {
             continue;
         }
         foreach (var region in sport["region"])
         {
             JObject regionObj  = (JObject)region.First;
             string  regionName = regionObj["alias"].ToObject <string>();
             foreach (var competition in regionObj["competition"])
             {
                 JObject league     = (JObject)competition.First;
                 string  leagueName = league["name"].ToObject <string>();
                 foreach (var game in league["game"])
                 {
                     try
                     {
                         Event   ev      = new Event();
                         JObject gameObj = (JObject)game.First;
                         ev.Home   = gameObj["team1_name"].ToObject <string>();
                         ev.Away   = gameObj["team2_name"].ToObject <string>();
                         ev.Region = regionName;
                         ev.League = leagueName;
                         ev.Start  = DateHelpers.TimeStampToDateTime(gameObj["start_ts"].ToObject <double>());
                         var a = gameObj["start_ts"];
                         ev.PrematchEnd = ev.Start;
                         if (ev.IsLive())
                         {
                             continue;
                         }
                         ev.SportID = mappings.SportMappings[sport["alias"].ToObject <string>()].ID;
                         Event dbEvent = DBFinder.FindEvent(ev).DBCopy(context);
                         if (dbEvent != null)
                         {
                             Console.WriteLine("Found: " + dbEvent.Home + " - " + dbEvent.Away);
                             ev = dbEvent;
                         }
                         else
                         {
                             context.Events.Add(ev);
                             if (context.SaveChanges() > 0)
                             {
                                 Console.WriteLine("Added: " + ev.Home + " - " + ev.Away);
                             }
                         }
                         foreach (var market in gameObj["market"])
                         {
                             JObject marketObj = (JObject)market.First;
                             if (!mappings.BetTypeMappings.ContainsKey(marketObj["type"].ToObject <string>()))
                             {
                                 continue;
                             }
                             BetOffer offer = new BetOffer();
                             offer.BetType = mappings.BetTypeMappings[marketObj["type"].ToObject <string>()].DBCopy(context);
                             if (!mappings.BetTargetMappings.ContainsKey(marketObj["name"].ToObject <string>()))
                             {
                                 continue;
                             }
                             offer.BetTarget = mappings.BetTargetMappings[marketObj["name"].ToObject <string>()].DBCopy(context);
                             offer.EventID   = ev.ID;
                             context.BetOffers.Add(offer);
                             context.SaveChanges();
                         }
                     }
                     catch
                     {
                         continue;
                     }
                 }
             }
         }
     }
 }