PageRenderTime 54ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/src/ZebraFlickr/Core/Processing/Retrieval/PhotosetRetriever.cs

https://bitbucket.org/garethl/zebraflickr
C# | 125 lines | 62 code | 23 blank | 40 comment | 5 complexity | afc55024bdf5a1808b46cae79633b28b MD5 | raw file
  1. #region License
  2. // Copyright (c) 2012 Gareth Lennox (garethl@dwakn.com)
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without modification,
  6. // are permitted provided that the following conditions are met:
  7. //
  8. // * Redistributions of source code must retain the above copyright notice,
  9. // this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above copyright notice,
  11. // this list of conditions and the following disclaimer in the documentation
  12. // and/or other materials provided with the distribution.
  13. // * Neither the name of Gareth Lennox nor the names of its
  14. // contributors may be used to endorse or promote products derived from this
  15. // software without specific prior written permission.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  18. // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  21. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  23. // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  25. // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. #endregion
  28. using System;
  29. using System.Collections;
  30. using System.Collections.Generic;
  31. using System.Linq;
  32. using System.Threading.Tasks;
  33. using FlickrNet;
  34. using ZebraFlickr.Core.Store;
  35. namespace ZebraFlickr.Core.Processing.Retrieval
  36. {
  37. public class PhotosetRetriever : IEnumerable<Tuple<SetEntry, Photoset>>
  38. {
  39. private readonly Flickr _flickr;
  40. private readonly TimeSpan _maxCacheAge;
  41. private readonly LocalState _state;
  42. public PhotosetRetriever(LocalState state, UserDetails details, TimeSpan maxCacheAge)
  43. {
  44. _state = state;
  45. _maxCacheAge = maxCacheAge;
  46. _flickr = FlickrClient.Create(details);
  47. }
  48. #region Implementation of IEnumerable
  49. /// <summary>
  50. /// Returns an enumerator that iterates through the collection.
  51. /// </summary>
  52. /// <returns>
  53. /// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
  54. /// </returns>
  55. /// <filterpriority>1</filterpriority>
  56. public IEnumerator<Tuple<SetEntry, Photoset>> GetEnumerator()
  57. {
  58. Log.Info("Retrieving photo sets from flickr...");
  59. var items = Try.Execute(5,
  60. delegate
  61. {
  62. var key = "Flickr_Photosets";
  63. var photoSets = _state.Cache.GetIfYoungerThan<PhotosetCollection>(_maxCacheAge, key);
  64. if (photoSets != null)
  65. return photoSets;
  66. photoSets = _flickr.PhotosetsGetList();
  67. Task.Factory.StartNew(() => _state.Cache.Set(key, photoSets));
  68. return photoSets;
  69. });
  70. var sortedItems = items.OrderBy(x => x.Title);
  71. foreach (var item in sortedItems)
  72. {
  73. var set = _state.GetSetByFlickrSetId(item.PhotosetId)
  74. ?? _state.CreateSet(item.Title, item.PhotosetId);
  75. //if it hasn't changed since we last synced it, ignore
  76. if (set.Synced != null && set.Synced.Value >= item.DateUpdated)
  77. continue;
  78. set.Synced = DateTime.UtcNow;
  79. yield return Tuple.Create(set, item);
  80. }
  81. var noSet = _state.GetSetByFlickrSetId("-1") ?? new SetEntry
  82. {
  83. Name = "Not in set",
  84. SetId = "-1"
  85. };
  86. noSet.Synced = DateTime.Now;
  87. yield return Tuple.Create(noSet, (Photoset)null);
  88. }
  89. /// <summary>
  90. /// Returns an enumerator that iterates through a collection.
  91. /// </summary>
  92. /// <returns>
  93. /// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
  94. /// </returns>
  95. /// <filterpriority>2</filterpriority>
  96. IEnumerator IEnumerable.GetEnumerator()
  97. {
  98. return GetEnumerator();
  99. }
  100. #endregion
  101. }
  102. }