PageRenderTime 40ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/cc-explore/Filters/MyHandler.cs

https://github.com/fallenidol/cc-flickr-explore
C# | 112 lines | 78 code | 16 blank | 18 comment | 15 complexity | 447b401480a09e85e60718d400e6de5e MD5 | raw file
  1. using CCFlickrExplore.Models;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using System.Web;
  7. using System.Xml;
  8. using System.Xml.Linq;
  9. using MoreLinq;
  10. namespace CCFlickrExplore.Filters
  11. {
  12. public class MyHandler : System.Web.Mvc.IActionFilter
  13. {
  14. public void OnActionExecuted(System.Web.Mvc.ActionExecutedContext filterContext)
  15. {
  16. //
  17. }
  18. public void OnActionExecuting(System.Web.Mvc.ActionExecutingContext filterContext)
  19. {
  20. if (MvcApplication.todayDone == false)
  21. {
  22. if (filterContext.ActionDescriptor.ActionName == "Index" && filterContext.ActionDescriptor.ControllerDescriptor.ControllerName == "Home")
  23. {
  24. // check the database to see if we have photos for today.
  25. DateTime now = DateTime.UtcNow.Date;
  26. //using (Contexts context = new Contexts())
  27. //{
  28. // var photos = from p in context.Photos
  29. // where p.PhotoDateAdded != now
  30. // select p;
  31. // if (photos.Count() > 0)
  32. // {
  33. // foreach (var item in photos)
  34. // {
  35. // context.Photos.Remove(item);
  36. // }
  37. // int delCount = context.SaveChanges();
  38. // }
  39. //}
  40. using (Contexts context = new Contexts())
  41. {
  42. var photos = from p in context.Photos
  43. where p.PhotoDateAdded == now
  44. select p;
  45. if (photos.Count() == 0)
  46. {
  47. // if not then get the photos from flickr and store in db.
  48. int page = 1;
  49. int perPage = 750;
  50. DateTime minUploadDate = now.AddDays(-2);
  51. DateTime maxUploadDate = now.AddDays(-1).AddSeconds(-1);
  52. 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"));
  53. Debug.WriteLine(url);
  54. XDocument xPhotos = XDocument.Load(url);
  55. var newPhotos = from item in xPhotos.Descendants("photo")
  56. where item.Attribute("url_n") != null && Convert.ToInt32(item.Attribute("height_m").Value) >= 300
  57. select new PhotoModel
  58. {
  59. //<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"/>
  60. UserId = item.Attribute("owner").Value,
  61. PhotoId = item.Attribute("id").Value,
  62. OwnerName = item.Attribute("ownername").Value,
  63. Title = item.Attribute("title").Value,
  64. MediumImageUrl = item.Attribute("url_m").Value,
  65. MediumHeight = int.Parse(item.Attribute("height_m").Value),
  66. MediumWidth = int.Parse(item.Attribute("width_m").Value),
  67. WebUrl = string.Format("http://www.flickr.com/photos/{0}/{1}/", item.Attribute("owner").Value, item.Attribute("id").Value),
  68. TagString = item.Attribute("tags") == null ? "" : item.Attribute("tags").Value.ToLowerInvariant().Trim(),
  69. ViewCount = int.Parse(item.Attribute("views").Value),
  70. PhotoDateAdded = now
  71. };
  72. var dist = newPhotos.DistinctBy(p => p.OwnerName).Take(500);
  73. foreach (PhotoModel item in dist)
  74. {
  75. if (item.Title.Trim().Length == 0)
  76. {
  77. item.Title = "Untitled";
  78. }
  79. context.Photos.Add(item);
  80. }
  81. int saveCount = context.SaveChanges();
  82. if (saveCount > 0)
  83. {
  84. MvcApplication.todayDone = true;
  85. }
  86. }
  87. else
  88. {
  89. MvcApplication.todayDone = true;
  90. }
  91. }
  92. }
  93. }
  94. }
  95. }
  96. }