Exemplo n.º 1
0
        public ActionResult FunResults()
        {
            var results = new List<FunPlace>();
            var funSearch = GetFunSearch(Request.Params);

            var today = DateTime.Now;
            //case concert :
            if (funSearch.FunType == 3)
            {
                using (var client = new WebClient())
                {
                    client.Encoding = Encoding.UTF8;
                    try
                    {
                        var location = funSearch.Latitude + "," + funSearch.Longitude;
                        var path = string.Format(Songkick, location);
                        string textString = client.DownloadString(path);
                        JObject songKickJson = JObject.Parse(textString);
                        var concerts = songKickJson["resultsPage"]["results"]["event"];
                        foreach (var c in concerts)
                        {
                            try
                            {
                                var dateStr = (string)c["start"]["date"];
                                var date = DateTime.Parse(dateStr);
                                if (today.Date != date.Date)
                                    continue;
                                var toAdd = new FunPlace()
                                {
                                    Name = (string)c["displayName"],
                                    Latitude = (double)c["location"]["lat"],
                                    Longitude = (double)c["location"]["lng"],
                                    StartTime = (string)c["start"]["time"]
                                };
                                var performances = c["performance"];
                                foreach (var p in performances)
                                {
                                    toAdd.Artists += p["displayName"] + ",";
                                }
                                toAdd.Artists = toAdd.Artists.TrimEnd(',');
                                Session["SKName"] = (string)c["displayName"];
                                Session["SKStartTime"] = (string)c["start"]["time"];
                                Session["SKArtists"] = toAdd.Artists;
                                results.Add(toAdd);
                            }
                            catch (Exception)
                            {
                                continue;
                            }
                        }
                    }
                    catch (WebException)
                    {
                    }
                }
                //find nearest 4SQ
                foreach (var item in results)
                {
                    using (var client = new WebClient())
                    {
                        client.Encoding = Encoding.UTF8;
                        try
                        {
                            var path = string.Format(FoursquareSearch, GetTokenKey());
                            var query = path + "&ll=" + item.Latitude + "," + item.Longitude + "&limit=20";
                            string textString = client.DownloadString(query);
                            JObject venuesJson = JObject.Parse(textString);
                            var venues = venuesJson["response"]["venues"];
                            var distList = new List<VenueSummary>();
                            foreach (var venue in venues)
                            {
                                var lat = (double)venue["location"]["lat"];
                                var lng = (double)venue["location"]["lng"];
                                var id = (string)venue["id"];
                                var toAdd = new VenueSummary
                                {
                                    Lat = (double)venue["location"]["lat"],
                                    Lng = (double)venue["location"]["lng"],
                                    Id = (string)venue["id"],
                                    HereNow = (int)venue["hereNow"]["count"],
                                    Dist = GetDistance(item.Latitude, item.Longitude, lat, lng)
                                };
                                distList.Add(toAdd);
                            }
                            var nearest = distList.Min(v => v.Dist);
                            var nearestVenue = (from v in distList where v.Dist == nearest select v).FirstOrDefault();
                            item.FoursquareId = nearestVenue.Id;
                            item.FriendCount = nearestVenue.HereNow;
                        }
                        catch (WebException)
                        {
                            item.FoursquareId = "";
                        }
                    }
                }
            }
            else
            {
                using (var client = new WebClient())
                {
                    client.Encoding = Encoding.UTF8;
                    try
                    {
                        var path = string.Format(FoursquareSearch, GetTokenKey());
                        var query = path + "&ll=" + funSearch.Latitude + "," + funSearch.Longitude;// +"&limit=20";
                        var toMatch = string.Empty;
                        switch(funSearch.FunType)
                        {
                            case 0:
                                toMatch = "Musée";
                                break;
                            case 1:
                                toMatch = "Restaurant";
                                break;
                            case 2:
                                toMatch = "Vie nocturne";
                                break;
                            default:
                                break;
                        }
                        //query += "&query=" + filter;
                        string textString = client.DownloadString(query);
                        JObject venuesJson = JObject.Parse(textString);
                        var venues = venuesJson["response"]["venues"];
                        foreach (var v in venues)
                        {
                            try
                            {
                                var categories = v["categories"];
                                var found = false;
                                foreach (var c in categories)
                                {
                                    var cat  = (string)c["name"];
                                    if (cat.Contains(toMatch) || (string.Compare((string)c["name"], toMatch, true) == 0))
                                    {
                                        found = true;
                                        break;
                                    }
                                }
                                if (found == false)
                                    continue;
                                var toAdd = new FunPlace()
                                {
                                    Name = (string)v["name"],
                                    Latitude = (double)v["location"]["lat"],
                                    Longitude = (double)v["location"]["lng"],
                                    FoursquareId = (string)v["id"],
                                    FriendCount = (int)v["hereNow"]["count"]
                                };
                                results.Add(toAdd);
                            }
                            catch (Exception)
                            {
                                continue;
                            }
                        }
                    }
                    catch (WebException)
                    {
                    }
                }
            }

            return View(results);
        }
Exemplo n.º 2
0
        public ActionResult Results()
        {
            var results = new List<Place>();
            var search = GetSearch(Request.Params);

            //process eworky search
            using (var client = new WebClient())
            {
                client.Encoding = Encoding.UTF8;
                try
                {
                    var offerType = -1;
                    switch(search.OfferType)
                    {
                        case 0:
                            offerType = 0;
                            break;
                        case 1:
                            offerType = 2;
                            break;
                        case 2:
                            offerType = 4;
                            break;
                        default:
                            break;
                    }
                    var features = string.Empty;
                    if (search.Wifi)
                        features += 0 + ",";
                    if (search.Cafe)
                        features += 1 + ",";
                    if (search.Resto)
                        features += 2 + ",";
                    if (search.Parking)
                        features += 3 + ",";
                    if (search.EasyAccess)
                        features += 4 + ",";
                    features = features.Trim(',');
                    if (!string.IsNullOrEmpty(features))
                        features = "[" + features + "]";

                    var path = eworkySearch + "&latitude=" + search.Latitude + "&longitude=" + search.Longitude;
                    if (offerType != -1)
                        path += "&offerType=" + offerType;
                    if (!string.IsNullOrEmpty(features))
                        path += "&features=" + features;
                    string textString = client.DownloadString(path);
                    JObject eworkyJson = JObject.Parse(textString);
                    var places = eworkyJson["response"];
                    foreach (var item in places)
                    {
                        results.Add(new Place()
                        {
                            Name = (string)item["name"],
                            Latitude = (double)item["latitude"],
                            Longitude = (double)item["longitude"],
                            EworkyId = (int)item["id"]
                        });
                    }
                }
                catch (WebException)
                {
                }
            }

            //find nearest 4SQ
            foreach (var item in results)
            {
                using (var client = new WebClient())
                {
                    client.Encoding = Encoding.UTF8;
                    try
                    {
                        var path = string.Format(FoursquareSearch, GetTokenKey());
                        var query = path + "&ll=" + item.Latitude + "," + item.Longitude + "&limit=20";
                        string textString = client.DownloadString(query);
                        JObject venuesJson = JObject.Parse(textString);
                        var venues = venuesJson["response"]["venues"];
                        var distList = new List<VenueSummary>();
                        foreach (var venue in venues)
                        {
                            var lat  = (double)venue["location"]["lat"];
                            var lng = (double)venue["location"]["lng"];
                            var id = (string)venue["id"];
                            var toAdd = new VenueSummary
                            {
                                Lat = (double)venue["location"]["lat"],
                                Lng = (double)venue["location"]["lng"],
                                Id = (string)venue["id"],
                                HereNow = (int)venue["hereNow"]["count"],
                                Dist = GetDistance(item.Latitude, item.Longitude, lat, lng)
                            };
                            distList.Add(toAdd);
                        }
                        var nearest = distList.Min(v => v.Dist);
                        var nearestVenue = (from v in distList where v.Dist == nearest select v).FirstOrDefault();
                        item.FoursquareId = nearestVenue.Id;
                        item.FriendCount = nearestVenue.HereNow;
                    }
                    catch (WebException)
                    {
                        item.FoursquareId = "";
                    }
                }
            }

            return View(results);
        }
        public IApiResponseParsingResult ParseResponse(ParseApiResponse message)
        {
            var responseType = ApiResponseType.Unknown;

            try
            {
                object response;

                var responseXml = DynamicXml.Parse(message.Data);
                responseType = responseXml.GetName();

                switch (responseType)
                {
                case ApiResponseType.CompetitorProfile:
                    response = CompetitorProfile.Parse(responseXml);
                    break;

                case ApiResponseType.FixtureChanges:
                    response = FixtureChangeList.Parse(responseXml);
                    break;

                case ApiResponseType.FixturesFixture:
                    response = FixtureList.Parse(responseXml);
                    break;

                case ApiResponseType.MatchSummary:
                    response = MatchSummary.Parse(responseXml);
                    break;

                case ApiResponseType.MarketDescriptions:
                    response = MarketDescriptionList.Parse(responseXml);
                    break;

                case ApiResponseType.MatchTimeline:
                    response = MatchTimeline.Parse(responseXml);
                    break;

                case ApiResponseType.PlayerProfile:
                    response = PlayerProfile.Parse(responseXml);
                    break;

                case ApiResponseType.RaceTournamentInfo:
                case ApiResponseType.SimpleTournamentInfo:
                case ApiResponseType.StandardTournamentInfo:
                case ApiResponseType.TournamentInfo:
                    response = TournamentInfo.Parse(responseXml);
                    break;

                case ApiResponseType.Response:
                    response = Response.Parse(responseXml);
                    break;

                case ApiResponseType.Schedule:
                    response = Schedule.Parse(responseXml);
                    break;

                case ApiResponseType.SportCategories:
                    response = SportCategoriesList.Parse(responseXml);
                    break;

                case ApiResponseType.Sports:
                    response = SportList.Parse(responseXml);
                    break;

                case ApiResponseType.SportTournaments:
                    response = SportTournamentList.Parse(responseXml);
                    break;

                case ApiResponseType.Tournaments:
                    response = TournamentList.Parse(responseXml);
                    break;

                case ApiResponseType.VenueSummary:
                    response = VenueSummary.Parse(responseXml);
                    break;

                default:
                    throw new NotSupportedException(
                              $"Message of specified type ['{responseType}'] can not be parsed.");
                }

                return(new ApiResponseParsed
                {
                    Language = message.Language,
                    Response = response,
                    RequestId = message.RequestId,
                    EventId = message.EventId,
                    ProductType = message.ProductType
                });
            }
            catch (Exception e)
            {
                throw;
                //return new ApiResponseParsingFailed(
                //    failureReason: e,
                //    requestId: message.RequestId,
                //    responseData: message.Data,
                //    responseType: responseType
                //);
            }
        }