Exemple #1
0
        public void AddLandmark(IEnumerable <LandmarkImage> images, string location)
        {
            foreach (var image in images)
            {
                var imageFound = new LandmarkImage();
                var owner      = _imageOwnerRepository.AddOwner(_imageOwnerRepository.GetOwner(image));

                if (!_dbContext.LandmarkImages.Any())
                {
                    imageFound = _dbContext.LandmarkImages.SingleOrDefault(i => i.MediumImageUrl == image.MediumImageUrl);
                }

                if (imageFound == null || imageFound.MediumImageUrl == null)
                {
                    var landmarkImage = new LandmarkImage()
                    {
                        Title          = image.Title,
                        Description    = image.Description,
                        Path           = image.Path,
                        PathAlias      = image.PathAlias,
                        Published      = image.Published,
                        Views          = image.Views,
                        MediumImageUrl = image.MediumImageUrl,
                        LocationId     = _dbContext.Locations.First(l => l.Name == location).Id,
                        OwnerId        = _dbContext.ImageOwners.First(o => o.OwnerName == owner.OwnerName).Id
                    };

                    _dbContext.LandmarkImages.Add(landmarkImage);
                    _dbContext.SaveChanges();
                }
            }
        }
Exemple #2
0
        private async Task <ICollection <LandmarkImage> > GetOrCreateImageAsync(ICollection <LandmarkImage> images)
        {
            var imagesCollection = new List <LandmarkImage>();

            foreach (var image in images)
            {
                var imageUrl = this.imagesRepository
                               .AllAsNoTracking()
                               .FirstOrDefault(x => x.RemoteImageUrl == image.RemoteImageUrl);

                if (imageUrl == null)
                {
                    imageUrl = new LandmarkImage
                    {
                        RemoteImageUrl = image.RemoteImageUrl,
                        Extension      = image.RemoteImageUrl.Split('.')[1],
                    };

                    await this.imagesRepository.AddAsync(imageUrl);

                    await this.imagesRepository.SaveChangesAsync();

                    imagesCollection.Add(imageUrl);
                }
            }

            return(imagesCollection);
        }
Exemple #3
0
        public ImageOwnerViewModel GetOwner(LandmarkImage image)
        {
            var owner = new ImageOwnerViewModel()
            {
                OwnerName     = image.Owner.OwnerName,
                OwnerImageUrl = image.Owner.OwnerImageUrl
            };

            return(owner);
        }
Exemple #4
0
        public async Task CreateAsync(CreateLandmarkInputModel input, string userId, string imagePath)
        {
            var landmark = new Landmark
            {
                Name        = input.Name,
                CategoryId  = input.CategoryId,
                RegionId    = input.RegionId,
                TownId      = input.TownId,
                MountainId  = input.MountainId,
                Description = input.Description,
                Latitude    = input.Latitude,
                Longitute   = input.Longitute,
                Websate     = input.Websate,
                Address     = input.Address,
                PhoneNumber = input.PhoneNumber,
                WorkTime    = input.WorkTime,
                DayOff      = input.DayOff,
                EntranceFee = input.EntranceFee,
                Difficulty  = input.Difficulty,
                UserId      = userId,
            };

            // landmark.Town.IsTown = true;
            Directory.CreateDirectory($"{imagePath}/landmarks/");
            foreach (var image in input.LandmarkImages)
            {
                var extension = Path.GetExtension(image.FileName).TrimStart('.');

                if (!this.allowedExtensions.Any(x => extension.EndsWith(x)))
                {
                    throw new Exception($"Invalid image extension {extension}");
                }

                var dbImage = new LandmarkImage
                {
                    UserId    = userId,
                    Extension = extension,
                };

                var physicalPath = $"{imagePath}/landmarks/{dbImage.Id}.{extension}";

                string localImgUrl = physicalPath.Split("wwwroot")[1];
                dbImage.RemoteImageUrl = localImgUrl;
                landmark.LandmarkImages.Add(dbImage);

                using Stream fileStream = new FileStream(physicalPath, FileMode.Create);
                await image.CopyToAsync(fileStream);
            }

            await this.landmarksRepository.AddAsync(landmark);

            await this.landmarksRepository.SaveChangesAsync();
        }
Exemple #5
0
        public async Task <IEnumerable <LandmarkImage> > GetLandmarkDataByLatLonAsync(LocationViewModel location)
        {
            var httpClient    = new HttpClient();
            var LandmarkImage = new List <LandmarkImage>();
            // use this link to get an api_key : https://www.flickr.com/services/apps/create/noncommercial/?
            // use this website to get the user_id : http://idgettr.com/

            var json = await httpClient.GetStringAsync(
                "https://api.flickr.com/services/rest/?&method=flickr.photos.search&api_key="
                + "3af3dd185652c4f461785ef5031e8254"
                + "&text=landmarks&lat=" + location.Latitude + "&lon=" + location.Logitude + "&format=json&per_page=30&extras=description,date_upload,owner_name,tags,machine_tags,views,media,path_alias,url_sq,url_m,url_o");

            json = json.Replace("jsonFlickrApi(", "").Replace(")", "");

            try
            {
                JObject response = JsonConvert.DeserializeObject <dynamic>(json);

                var photos = response.Value <JObject>("photos").Value <JArray>("photo");

                var landmarkImages = new List <LandmarkImage>();

                foreach (var photo in photos)
                {
                    var img = new LandmarkImage()
                    {
                        PathAlias      = photo.Value <string>("pathalias"),
                        Title          = photo.Value <string>("title"),
                        Published      = photo.Value <string>("dateupload"),
                        Views          = photo.Value <int>("views"),
                        Description    = photo.Value <JObject>("description").Value <string>("_content"),
                        MediumImageUrl = photo.Value <string>("url_m")
                    };

                    img.Owner = GetImageOwner(photo);
                    landmarkImages.Add(img);
                }

                LandmarkImage = landmarkImages;
            }
            catch (Exception exception)
            {
            }

            return(LandmarkImage);
        }
Exemple #6
0
        public async Task PopulateDbWithLandmarksAsync()
        {
            var concurentBag = new ConcurrentBag <LandmarkDto>();

            for (int i = 0; i <= 15; i++)
            {
                var url = new Url($"https://tripsjournal.com/zabelejitelnosti/page/{i}");

                var landmarkDocument = this.context.OpenAsync(url).GetAwaiter().GetResult();

                var allLandmarks = landmarkDocument.QuerySelectorAll("div.list-item");

                foreach (var landmark in allLandmarks)
                {
                    var landmarkUrl = landmark.QuerySelector("a").GetAttribute("href");
                    Console.WriteLine(landmarkUrl);

                    try
                    {
                        var landmarkDto = this.GetLandmark(landmarkUrl);
                        concurentBag.Add(landmarkDto);
                    }
                    catch (Exception)
                    {
                    }
                }
            }

            foreach (var landmark in concurentBag)
            {
                var categoryId = await this.GetOrCreateCategoryAsync(landmark.CategoryName);

                var regionId = await this.GetOrCreateRegionAsync(landmark.RegionName);

                var townId = await this.GetOrCreateTownAsync(landmark.TownName);

                var mountainId = await this.GetOrCreateMountainAsync(landmark.MountainName);

                // var images = await this.GetOrCreateImageAsync(landmark.Images);
                var landmarkExists = this.landmarksRepository.AllAsNoTracking().Any(x => x.Name == landmark.Name);

                if (landmarkExists)
                {
                    continue;
                }

                var newLandmark = new Landmark()
                {
                    Name        = landmark.Name,
                    CategoryId  = categoryId,
                    RegionId    = regionId,
                    TownId      = townId,
                    MountainId  = mountainId,
                    Latitude    = landmark.Latitude,
                    Longitute   = landmark.Longitute,
                    Websate     = landmark.Websate,
                    Address     = landmark.Address,
                    PhoneNumber = landmark.PhoneNumber,
                    WorkTime    = landmark.WorkTime,
                    DayOff      = landmark.DayOff,
                    EntranceFee = landmark.EntranceFee,
                    Difficulty  = landmark.Difficulty,
                    Description = landmark.Description,
                };

                var imagesCollection = new List <LandmarkImage>();

                foreach (var image in landmark.Images)
                {
                    var imageUrl = this.imagesRepository
                                   .AllAsNoTracking()
                                   .FirstOrDefault(x => x.RemoteImageUrl == image.RemoteImageUrl);

                    if (imageUrl == null)
                    {
                        imageUrl = new LandmarkImage
                        {
                            RemoteImageUrl = image.RemoteImageUrl,
                            Extension      = image.RemoteImageUrl.Split('.').Last(),
                            Landmark       = newLandmark,
                        };

                        await this.imagesRepository.AddAsync(imageUrl);

                        imagesCollection.Add(imageUrl);
                    }
                }

                newLandmark.LandmarkImages = imagesCollection;

                await this.landmarksRepository.AddAsync(newLandmark);

                await this.landmarksRepository.SaveChangesAsync();
            }
        }
Exemple #7
0
        private LandmarkDto GetLandmark(string url)
        {
            var document = this.context.OpenAsync(url).GetAwaiter().GetResult();

            if (document.StatusCode == System.Net.HttpStatusCode.NotFound)
            {
                Console.WriteLine($"Not found url({url})");
                throw new InvalidOperationException();
            }

            var landmark = new LandmarkDto();

            // Get name
            var landmarkNames = document.QuerySelectorAll(".title > h1");

            foreach (var item in landmarkNames)
            {
                var landmarkName = item.TextContent;
                landmark.Name = landmarkName;
            }

            // Get landmark model elements
            var ladmarkModelElements = document.QuerySelectorAll(".list-table > ul > li > span");

            var categoryName = ladmarkModelElements[0].TextContent;

            landmark.CategoryName = categoryName;

            var regionName = ladmarkModelElements[1].TextContent;

            landmark.RegionName = regionName;

            var townName = ladmarkModelElements[2].TextContent;

            landmark.TownName = townName;

            var mountainName = ladmarkModelElements[3].TextContent;

            landmark.MountainName = mountainName;

            var    coordinates = ladmarkModelElements[4].TextContent.Split(", ");
            double latitude    = double.Parse(coordinates[0]);
            double longitude   = double.Parse(coordinates[1]);

            landmark.Latitude  = latitude;
            landmark.Longitute = longitude;

            var website = ladmarkModelElements[5].TextContent;

            landmark.Websate = website;

            var address = ladmarkModelElements[6].TextContent;

            landmark.Address = address;

            var phone = ladmarkModelElements[7].TextContent;

            landmark.PhoneNumber = phone;

            var workTime = ladmarkModelElements[8].TextContent;

            landmark.WorkTime = workTime;

            var daysOff = ladmarkModelElements[9].TextContent;

            landmark.DayOff = daysOff;

            var    entranceFeeAsString = ladmarkModelElements[10].TextContent.Split(' ');
            double entranceFee         = 0;
            bool   isThereFee          = double.TryParse(entranceFeeAsString[0], out entranceFee);

            landmark.EntranceFee = isThereFee ? entranceFee : 0;

            var accessibility = document.QuerySelectorAll(".list-table > ul > li > span > em.rating");

            foreach (var item in accessibility)
            {
                var currentAccessibility = int.Parse(item.GetAttribute("data-score"));
                landmark.Difficulty = 6 - currentAccessibility;
            }

            // Get description
            var landmarkDescription = document.QuerySelectorAll(".post > .entry > p");

            StringBuilder sb = new StringBuilder();

            foreach (var item in landmarkDescription)
            {
                sb.AppendLine(item.TextContent);
            }

            var allDescripition = sb.ToString().TrimEnd();

            landmark.Description = allDescripition;

            // Get images
            var imagesUrl = document.QuerySelectorAll(".gallery-slides div.gallery-slide");

            foreach (var imageUrl in imagesUrl)
            {
                var curentImageContent = imageUrl.GetAttribute("style");
                var curentImageUrl     = curentImageContent.Split("url(")[1];
                curentImageUrl = curentImageUrl.TrimEnd(')');
                LandmarkImage image = new LandmarkImage
                {
                    RemoteImageUrl = curentImageUrl,
                };
                landmark.Images.Add(image);
            }

            return(landmark);
        }