/cc-explore/Filters/MyHandler.cs
C# | 112 lines | 78 code | 16 blank | 18 comment | 15 complexity | 447b401480a09e85e60718d400e6de5e MD5 | raw file
- using CCFlickrExplore.Models;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Web;
- using System.Xml;
- using System.Xml.Linq;
- using MoreLinq;
- namespace CCFlickrExplore.Filters
- {
- public class MyHandler : System.Web.Mvc.IActionFilter
- {
- public void OnActionExecuted(System.Web.Mvc.ActionExecutedContext filterContext)
- {
- //
- }
- public void OnActionExecuting(System.Web.Mvc.ActionExecutingContext filterContext)
- {
- if (MvcApplication.todayDone == false)
- {
- if (filterContext.ActionDescriptor.ActionName == "Index" && filterContext.ActionDescriptor.ControllerDescriptor.ControllerName == "Home")
- {
- // check the database to see if we have photos for today.
- DateTime now = DateTime.UtcNow.Date;
- //using (Contexts context = new Contexts())
- //{
- // var photos = from p in context.Photos
- // where p.PhotoDateAdded != now
- // select p;
- // if (photos.Count() > 0)
- // {
- // foreach (var item in photos)
- // {
- // context.Photos.Remove(item);
- // }
- // int delCount = context.SaveChanges();
- // }
- //}
- using (Contexts context = new Contexts())
- {
- var photos = from p in context.Photos
- where p.PhotoDateAdded == now
- select p;
- if (photos.Count() == 0)
- {
- // if not then get the photos from flickr and store in db.
- int page = 1;
- int perPage = 750;
- DateTime minUploadDate = now.AddDays(-2);
- DateTime maxUploadDate = now.AddDays(-1).AddSeconds(-1);
- string url = string.Format("http://api.flickr.com/services/rest/?method=flickr.photos.search&format=rest&api_key={0}&page={1}&per_page={2}&sort=interestingness-desc&filter_privacy=1&safe_search=1&content_type=1&media=photos&min_taken_date={3}&max_taken_date={4}&license=1,2,3,4,5,6,7&extras=license,owner_name,views,url_n,url_m,tags", MvcApplication.API_KEY, page, perPage, minUploadDate.ToString("yyyy-MM-dd HH:mm:ss"), maxUploadDate.ToString("yyyy-MM-dd HH:mm:ss"));
- Debug.WriteLine(url);
- XDocument xPhotos = XDocument.Load(url);
- var newPhotos = from item in xPhotos.Descendants("photo")
- where item.Attribute("url_n") != null && Convert.ToInt32(item.Attribute("height_m").Value) >= 300
- select new PhotoModel
- {
- //<photo id="7849428998" owner="47290943@N03" secret="eed76b378a" server="7106" farm="8" title="Merchants Quay, Newry" ispublic="1" isfriend="0" isfamily="0" license="7" ownername="National Library of Ireland on The Commons" views="5614" url_n="http://farm8.staticflickr.com/7106/7849428998_eed76b378a_n.jpg" height_n="311" width_n="320"/>
- UserId = item.Attribute("owner").Value,
- PhotoId = item.Attribute("id").Value,
- OwnerName = item.Attribute("ownername").Value,
- Title = item.Attribute("title").Value,
- MediumImageUrl = item.Attribute("url_m").Value,
- MediumHeight = int.Parse(item.Attribute("height_m").Value),
- MediumWidth = int.Parse(item.Attribute("width_m").Value),
- WebUrl = string.Format("http://www.flickr.com/photos/{0}/{1}/", item.Attribute("owner").Value, item.Attribute("id").Value),
- TagString = item.Attribute("tags") == null ? "" : item.Attribute("tags").Value.ToLowerInvariant().Trim(),
- ViewCount = int.Parse(item.Attribute("views").Value),
- PhotoDateAdded = now
- };
- var dist = newPhotos.DistinctBy(p => p.OwnerName).Take(500);
- foreach (PhotoModel item in dist)
- {
- if (item.Title.Trim().Length == 0)
- {
- item.Title = "Untitled";
- }
- context.Photos.Add(item);
- }
- int saveCount = context.SaveChanges();
- if (saveCount > 0)
- {
- MvcApplication.todayDone = true;
- }
- }
- else
- {
- MvcApplication.todayDone = true;
- }
- }
- }
- }
- }
- }
- }