aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Controller/Providers/BaseMetadataProvider.cs
blob: 50004be442df02e4f7ea93de2be459ceed59e292 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Common.Extensions;
using System.Threading.Tasks;
using System;

namespace MediaBrowser.Controller.Providers
{
    public abstract class BaseMetadataProvider
    {
        protected Guid _id;
        public virtual Guid Id
        {
            get
            {
                if (_id == null) _id = this.GetType().FullName.GetMD5();
                return _id;
            }
        }

        public abstract bool Supports(BaseEntity item);

        public virtual bool RequiresInternet
        {
            get
            {
                return false;
            }
        }

        /// <summary>
        /// Returns the last refresh time of this provider for this item. Providers that care should
        /// call SetLastRefreshed to update this value.
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        protected virtual DateTime LastRefreshed(BaseEntity item)
        {
            return (item.ProviderData.GetValueOrDefault(this.Id, new BaseProviderInfo())).LastRefreshed;
        }

        /// <summary>
        /// Sets the persisted last refresh date on the item for this provider.
        /// </summary>
        /// <param name="item"></param>
        /// <param name="value"></param>
        protected virtual void SetLastRefreshed(BaseEntity item, DateTime value)
        {
            var data = item.ProviderData.GetValueOrDefault(this.Id, new BaseProviderInfo());
            data.LastRefreshed = value;
            item.ProviderData[this.Id] = data;
        }

        /// <summary>
        /// Returns whether or not this provider should be re-fetched.  Default functionality can
        /// compare a provided date with a last refresh time.  This can be overridden for more complex
        /// determinations.
        /// </summary>
        /// <returns></returns>
        public virtual bool NeedsRefresh(BaseEntity item)
        {
            return CompareDate(item) > LastRefreshed(item);
        }

        /// <summary>
        /// Override this to return the date that should be compared to the last refresh date
        /// to determine if this provider should be re-fetched.
        /// </summary>
        protected virtual DateTime CompareDate(BaseEntity item)
        {
            return DateTime.MinValue.AddMinutes(1); // want this to be greater than mindate so new items will refresh
        }

        public virtual Task FetchIfNeededAsync(BaseEntity item)
        {
            if (this.NeedsRefresh(item))
                return FetchAsync(item, item.ResolveArgs);
            else
                return new Task(() => { });
        }
            
        public abstract Task FetchAsync(BaseEntity item, ItemResolveEventArgs args);

        public abstract MetadataProviderPriority Priority { get; }
    }

    /// <summary>
    /// Determines when a provider should execute, relative to others
    /// </summary>
    public enum MetadataProviderPriority
    {
        // Run this provider at the beginning
        First = 1,

        // Run this provider after all first priority providers
        Second = 2,

        // Run this provider after all second priority providers
        Third = 3,

        // Run this provider last
        Last = 4
    }
}