Example #1
0
 private IEnumerable <double> CalculateDistances(MooiCluster cluster1, MooiCluster cluster2)
 {
     return(from pm1 in cluster2.Placemarks
            from pm2 in cluster1.Placemarks
            where pm1 != pm2
            select _kmlCalculator.GetDistanceInMeters(pm1, pm2));
 }
Example #2
0
        private async Task <DiscoveredPlace[]> DiscoverOnFoursquareForExploring(IReadOnlyList <KmlPlacemark> placemarks, string language, IDiscoveringProgress progressTracker, CancellationToken cancellationToken)
        {
            var result = new ConcurrentBag <DiscoveredPlace>();

            var placemarksToExplore = new List <KmlPlacemark> {
                placemarks.First()
            };

            foreach (var placemark in placemarks.Skip(1))
            {
                if (!placemarksToExplore.Any(p => _kmlCalculator.GetDistanceInMeters(p, placemark) < EXPLORE_ON_PLACEMARKS_AFTER_METERS))
                {
                    placemarksToExplore.Add(placemark);
                }
                else
                {
                    progressTracker.ReportItemProcessed();
                }
            }

            await placemarksToExplore.ForEachAsync(DEGREE_OF_PARALLELISM_PER_SERVICE, async (placemark) =>
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    return;
                }

                var venues = await _foursquare.ExplorePopular(placemark, language, cancellationToken);
                if (venues != null)
                {
                    foreach (var venue in venues)
                    {
                        if (placemarks.Any(p => p.Name.Equals(venue.Title, StringComparison.OrdinalIgnoreCase)))
                        {
                            continue;
                        }

                        result.Add(new DiscoveredPlace
                        {
                            Venue = venue
                        });
                        _logger.Info($"Explored a recommended venue on Foursquare: {venue.Title}");
                    }
                }

                progressTracker.ReportItemProcessed();
            });

            return(result.ToArray());
        }
Example #3
0
        private FoursquareVenue CreateVenueModel(FoursquareResponseVenue responseVenue, GeoCoordinate originCoordinate = null)
        {
            var       facebookUser      = responseVenue.Contact?.FacebookUsername ?? responseVenue.Contact?.Facebook;
            var       facebookUrl       = string.IsNullOrEmpty(facebookUser) ? null : $"https://www.facebook.com/{facebookUser}";
            var       mainCategory      = responseVenue.Categories.FirstOrDefault(x => x.Primary) ?? responseVenue.Categories[0];
            var       coordinate        = new GeoCoordinate(responseVenue.Location.Lat, responseVenue.Location.Lng);
            const int takeTopCategories = 2;

            var venue = new FoursquareVenue {
                Id       = responseVenue.Id,
                Title    = responseVenue.Name,
                Category = string.Join(", ", responseVenue.Categories
                                       .OrderBy(x => x.Primary ? 1 : 2)
                                       .Select(x => x.ShortName)
                                       .Take(takeTopCategories)),
                Coordinate          = coordinate,
                Rating              = responseVenue.Rating,
                RatingColor         = responseVenue.RatingColor,
                MaxRating           = VENUE_MAX_RATING,
                LikesCount          = responseVenue.Likes?.Count,
                DistanceToPlacemark = _kmlCalculator.GetDistanceInMeters(originCoordinate, coordinate),
                Region              = string.Join(", ", new[] {
                    responseVenue.Location?.City,
                    responseVenue.Location?.State,
                    responseVenue.Location?.Country
                }.Where(x => x != null)),
                Address = responseVenue.Location?.Address == null
                    ? null
                    : string.Join(", ", responseVenue.Location?.FormattedAddress ?? new string[0]),
                ContactPhone = responseVenue.Contact?.FormattedPhone,
                Websites     = new[] {
                    responseVenue.ShortUrl,
                    responseVenue.Url,
                    facebookUrl
                }.Where(x => !string.IsNullOrEmpty(x)).ToArray(),
                IconUrl       = new Uri($"{mainCategory.Icon.Prefix}{VENUE_ICON_SIZE}{mainCategory.Icon.Suffix}"),
                PriceLevel    = responseVenue.Price?.Tier,
                PriceCurrency = responseVenue.Price?.Currency,
                PriceMaxLevel = VENUE_MAX_PRICE_LEVEL,
                Tags          = responseVenue.Tags
            };

            if ("public".Equals(responseVenue.BestPhoto?.Visibility, StringComparison.OrdinalIgnoreCase))
            {
                venue.PhotoUrls = new[] {
                    $"{responseVenue.BestPhoto?.Prefix}{VENUE_PHOTO_SIZE}{responseVenue.BestPhoto?.Suffix}"
                };
            }

            if (responseVenue.Hours?.Timeframes != null)
            {
                var sb = new StringBuilder();
                foreach (var timeframe in responseVenue.Hours.Timeframes)
                {
                    if (sb.Length > 0)
                    {
                        sb.Append(", ");
                    }

                    sb.Append($"{timeframe.Days}: ");
                    sb.Append(string.Join(", ", timeframe.Open.Select(o => o.RenderedTime)));
                }
                venue.OpeningHours = sb.ToString();
            }

            if (responseVenue.Tips != null)
            {
                var tips = responseVenue.Tips.Groups[0].Items;

                venue.Tips = tips
                             .Where(x => x.AgreeCount > 0)
                             .Take(TOP_TIPS_TO_GET)
                             .Select(tip => new FoursquareVenueTip {
                    Message       = tip.Text,
                    Likes         = tip.Likes.Count,
                    AgreeCount    = tip.AgreeCount,
                    DisagreeCount = tip.DisagreeCount
                }).ToArray();
            }

            if (venue.Websites.Length == 0)
            {
                venue.Websites = new[] {
                    $"{VENUE_WEB_URL}{venue.Id}"
                };
            }

            return(venue);
        }