public async Task<ActionResult> Create([Bind(Include = "Url,Name")] PhotoAlbum photoAlbum) { try { if (ModelState.IsValid) { RajcePhotoProvider photoProvider = new RajcePhotoProvider(); var photoList = await photoProvider.GetPhotoList(photoAlbum.Url); if (photoList != null && photoList.Count() > 0) { foreach (var photo in photoList) { var photoModel = new Photo() { Url = photo.Url, ThumbnailUrl = photo.ThumbnailUrl }; photoAlbum.Photos.Add(photoModel); } } db.PhotoAlbumSet.Add(photoAlbum); await db.SaveChangesAsync(); await DetectBibNumbers(photoAlbum); //foreach(var photo in photoAlbum.Photos) //{ // await DetectBibNumbers(photo); //} return Redirect("~/PhotoAlbums/Details/" + photoAlbum.Id); } } catch(Exception ex) { } return View(photoAlbum); }
public async Task<IEnumerable<IPhoto>> GetPhotoList(string url) { string[] photosUrls = null; List<IPhoto> photos = null; var html = new HtmlWeb().Load(url); if (html.DocumentNode != null) { string htmlString = html.DocumentNode.InnerHtml; if (!string.IsNullOrWhiteSpace(htmlString)) { var photoIdPatter = ".*photoID\\s*:\\s*\"([^\"]*)\".*"; var photoIdMatches = Regex.Matches(htmlString, photoIdPatter); List<string> photoIds = new List<string>(); if(photoIdMatches != null && photoIdMatches.Count > 0) { foreach(Match photoIdMatch in photoIdMatches) { if(photoIdMatch.Groups.Count == 2) { var photoId = photoIdMatch.Groups[1].Value; photoIds.Add(photoId); } } } var storagePattern = ".*var storage = \"([^\"]*)\";.*"; var storageMatch = Regex.Match(htmlString, storagePattern); if (storageMatch != null && storageMatch.Success && storageMatch.Groups.Count == 2) { var storageUrl = storageMatch.Groups[1].Value; photosUrls = html.DocumentNode.Descendants("a") .Select(a => a.GetAttributeValue("href", null)) .Where(u => !String.IsNullOrEmpty(u) && u.StartsWith(storageUrl)).ToArray(); } photos = new List<IPhoto>(); bool includeThumbnail = photoIds.Count == photosUrls.Count(); for(int photoIndex = 0; photoIndex < photosUrls.Count(); photoIndex++) { var photoUrl = photosUrls[photoIndex]; IPhoto photo = new Photo() { Url = photoUrl }; if (includeThumbnail) { var photoId = photoIds[photoIndex]; photo.ThumbnailUrl = "http://www.rajce.idnes.cz/f" + photoId + "/res/300.jpg"; } photos.Add(photo); } } } return photos; }
public async Task DetectBibNumbers(Photo photo) { try { CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["AzureWebJobsStorage"].ConnectionString);//CloudStorageAccount.Parse("UseDevelopmentStorage=true"); CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient(); CloudQueue queue = queueClient.GetQueueReference("detectbibnumbersqueue"); queue.CreateIfNotExists(); var photoJson = await JsonConvert.SerializeObjectAsync(photo); queue.AddMessage(new CloudQueueMessage(photoJson)); } catch (Exception ex) { } }