Exemplo n.º 1
0
        public async Task <bool> GetFights(string url_)
        {
            Uri uri = new Uri(url_);

            Clear();
            try
            {
                if (!uri.AbsolutePath.StartsWith("/reports/fights-and-participants/"))
                {
                    if (uri.Segments.Length != 3)
                    {
                        return(false);
                    }
                    id = uri.Segments[2];
                }
                else if (uri.AbsolutePath.StartsWith("/reports/fights-and-participants/"))
                {
                    id = uri.Segments[3];
                }

                string sfight = null;

                var m = Regex.Match(uri.AbsoluteUri, "#fight=(\\d+|last)");
                if (m.Success)
                {
                    sfight = m.Groups[1].Value;
                }

                if (id != null)
                {
                    var url = FightsAndParticipantsUrl();
                    if (cache.Has(url))
                    {
                        fights = cache.GetItem(url);
                        log.Log("fights from cache");
                    }
                    else
                    {
                        log.Log("fights download");
                        var html = await Download(url);

                        var serializer = new DataContractJsonSerializer(typeof(FightsAndFiends));
                        var ms         = new MemoryStream(Encoding.UTF8.GetBytes(html));
                        fights = serializer.ReadObject(ms) as FightsAndFiends;
                        cache.SetItem(url, fights);
                        log.Log("download ok");
                    }
                    fight_id = 0;
                    if (sfight != null && fights != null && fights.fights != null && fights.fights.Count > 0)
                    {
                        if (sfight == "last")
                        {
                            fight_id = fights.fights.Last().id;
                        }
                        else if (int.TryParse(sfight, out fight_id))
                        {
                            if (!fights.fights.Exists(f => f.id == fight_id))
                            {
                                fight_id = 0;
                            }
                        }
                    }
                }

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
Exemplo n.º 2
0
 private void Clear()
 {
     id       = null;
     fight_id = 0;
     fights   = null;
 }