aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Providers/Manager/ProviderUtils.cs
blob: fa4840f1032623d02f9841534106c5c63f567676 (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using System;
using System.Collections.Generic;

namespace MediaBrowser.Providers.Manager
{
    public static class ProviderUtils
    {
        public static void MergeBaseItemData<T>(MetadataResult<T> sourceResult,
            MetadataResult<T> targetResult, 
            List<MetadataFields> lockedFields, 
            bool replaceData, 
            bool mergeMetadataSettings)
            where T : BaseItem
        {
            var source = sourceResult.Item;
            var target = targetResult.Item;

            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            if (target == null)
            {
                throw new ArgumentNullException("target");
            }

            if (!lockedFields.Contains(MetadataFields.Name))
            {
                if (replaceData || string.IsNullOrEmpty(target.Name))
                {
                    // Safeguard against incoming data having an emtpy name
                    if (!string.IsNullOrWhiteSpace(source.Name))
                    {
                        target.Name = source.Name;
                    }
                }
            }

            if (replaceData || !target.CommunityRating.HasValue)
            {
                target.CommunityRating = source.CommunityRating;
            }

            if (replaceData || !target.EndDate.HasValue)
            {
                target.EndDate = source.EndDate;
            }

            if (!lockedFields.Contains(MetadataFields.Genres))
            {
                if (replaceData || target.Genres.Count == 0)
                {
                    target.Genres = source.Genres;
                }
            }

            if (replaceData || string.IsNullOrEmpty(target.HomePageUrl))
            {
                target.HomePageUrl = source.HomePageUrl;
                if (!string.IsNullOrWhiteSpace(target.HomePageUrl) && target.HomePageUrl.IndexOf("http", StringComparison.OrdinalIgnoreCase) != 0)
                {
                    target.HomePageUrl = "http://" + target.HomePageUrl;
                }
            }

            if (replaceData || !target.IndexNumber.HasValue)
            {
                target.IndexNumber = source.IndexNumber;
            }

            if (!lockedFields.Contains(MetadataFields.OfficialRating))
            {
                if (replaceData || string.IsNullOrEmpty(target.OfficialRating))
                {
                    target.OfficialRating = source.OfficialRating;
                }
            }

            if (replaceData || string.IsNullOrEmpty(target.OfficialRatingDescription))
            {
                target.OfficialRatingDescription = source.OfficialRatingDescription;
            }

            if (replaceData || string.IsNullOrEmpty(target.CustomRating))
            {
                target.CustomRating = source.CustomRating;
            }
            
            if (!lockedFields.Contains(MetadataFields.Overview))
            {
                if (replaceData || string.IsNullOrEmpty(target.Overview))
                {
                    target.Overview = source.Overview;
                }
            }

            if (replaceData || !target.ParentIndexNumber.HasValue)
            {
                target.ParentIndexNumber = source.ParentIndexNumber;
            }

            if (!lockedFields.Contains(MetadataFields.Cast))
            {
                if (replaceData || targetResult.People == null || targetResult.People.Count == 0)
                {
                    targetResult.People = sourceResult.People ?? new List<PersonInfo>();
                }
            }

            if (replaceData || !target.PremiereDate.HasValue)
            {
                target.PremiereDate = source.PremiereDate;
            }

            if (replaceData || !target.ProductionYear.HasValue)
            {
                target.ProductionYear = source.ProductionYear;
            }

            if (!lockedFields.Contains(MetadataFields.Runtime))
            {
                if (replaceData || !target.RunTimeTicks.HasValue)
                {
                    if (!(target is Audio) && !(target is Video))
                    {
                        target.RunTimeTicks = source.RunTimeTicks;
                    }
                }
            }

            if (!lockedFields.Contains(MetadataFields.Studios))
            {
                if (replaceData || target.Studios.Count == 0)
                {
                    target.Studios = source.Studios;
                }
            }

            if (!lockedFields.Contains(MetadataFields.Tags))
            {
                var sourceHasTags = source as IHasTags;
                var targetHasTags = target as IHasTags;

                if (sourceHasTags != null && targetHasTags != null)
                {
                    if (replaceData || targetHasTags.Tags.Count == 0)
                    {
                        targetHasTags.Tags = sourceHasTags.Tags;
                    }
                }
            }

            if (!lockedFields.Contains(MetadataFields.Keywords))
            {
                var sourceHasKeywords = source as IHasKeywords;
                var targetHasKeywords = target as IHasKeywords;

                if (sourceHasKeywords != null && targetHasKeywords != null)
                {
                    if (replaceData || targetHasKeywords.Keywords.Count == 0)
                    {
                        targetHasKeywords.Keywords = sourceHasKeywords.Keywords;
                    }
                }
            }

            if (!lockedFields.Contains(MetadataFields.ProductionLocations))
            {
                var sourceHasProductionLocations = source as IHasProductionLocations;
                var targetHasProductionLocations = target as IHasProductionLocations;

                if (sourceHasProductionLocations != null && targetHasProductionLocations != null)
                {
                    if (replaceData || targetHasProductionLocations.ProductionLocations.Count == 0)
                    {
                        targetHasProductionLocations.ProductionLocations = sourceHasProductionLocations.ProductionLocations;
                    }
                }
            }

            if (replaceData || !target.VoteCount.HasValue)
            {
                target.VoteCount = source.VoteCount;
            }

            foreach (var id in source.ProviderIds)
            {
                var key = id.Key;

                // Don't replace existing Id's.
                if (replaceData || !target.ProviderIds.ContainsKey(key))
                {
                    target.ProviderIds[key] = id.Value;
                }
            }

            MergeAlbumArtist(source, target, lockedFields, replaceData);
            MergeBudget(source, target, lockedFields, replaceData);
            MergeMetascore(source, target, lockedFields, replaceData);
            MergeCriticRating(source, target, lockedFields, replaceData);
            MergeAwards(source, target, lockedFields, replaceData);
            MergeTaglines(source, target, lockedFields, replaceData);
            MergeTrailers(source, target, lockedFields, replaceData);
            MergeShortOverview(source, target, lockedFields, replaceData);

            if (mergeMetadataSettings)
            {
                MergeMetadataSettings(source, target);
            }
        }

        public static void MergeMetadataSettings(BaseItem source,
           BaseItem target)
        {
            target.ForcedSortName = source.ForcedSortName;
            target.LockedFields = source.LockedFields;
            target.IsLocked = source.IsLocked;
            target.DisplayMediaType = source.DisplayMediaType;

            // Grab the value if it's there, but if not then don't overwrite the default
            if (source.DateCreated != default(DateTime))
            {
                target.DateCreated = source.DateCreated;
            }

            var sourceHasLanguageSettings = source as IHasPreferredMetadataLanguage;
            var targetHasLanguageSettings = target as IHasPreferredMetadataLanguage;

            if (sourceHasLanguageSettings != null && targetHasLanguageSettings != null)
            {
                targetHasLanguageSettings.PreferredMetadataCountryCode = sourceHasLanguageSettings.PreferredMetadataCountryCode;
                targetHasLanguageSettings.PreferredMetadataLanguage = sourceHasLanguageSettings.PreferredMetadataLanguage;
            }

            var sourceHasDisplayOrder = source as IHasDisplayOrder;
            var targetHasDisplayOrder = target as IHasDisplayOrder;

            if (sourceHasDisplayOrder != null && targetHasDisplayOrder != null)
            {
                targetHasDisplayOrder.DisplayOrder = sourceHasDisplayOrder.DisplayOrder;
            }
        }
        
        private static void MergeShortOverview(BaseItem source, BaseItem target, List<MetadataFields> lockedFields, bool replaceData)
        {
            var sourceHasShortOverview = source as IHasShortOverview;
            var targetHasShortOverview = target as IHasShortOverview;

            if (sourceHasShortOverview != null && targetHasShortOverview != null)
            {
                if (replaceData || string.IsNullOrEmpty(targetHasShortOverview.ShortOverview))
                {
                    targetHasShortOverview.ShortOverview = sourceHasShortOverview.ShortOverview;
                }
            }
        }

        private static void MergeAlbumArtist(BaseItem source, BaseItem target, List<MetadataFields> lockedFields, bool replaceData)
        {
            var sourceHasAlbumArtist = source as IHasAlbumArtist;
            var targetHasAlbumArtist = target as IHasAlbumArtist;

            if (sourceHasAlbumArtist != null && targetHasAlbumArtist != null)
            {
                if (replaceData || targetHasAlbumArtist.AlbumArtists.Count == 0)
                {
                    targetHasAlbumArtist.AlbumArtists = sourceHasAlbumArtist.AlbumArtists;
                }
            }
        }

        private static void MergeBudget(BaseItem source, BaseItem target, List<MetadataFields> lockedFields, bool replaceData)
        {
            var sourceHasBudget = source as IHasBudget;
            var targetHasBudget = target as IHasBudget;

            if (sourceHasBudget != null && targetHasBudget != null)
            {
                if (replaceData || !targetHasBudget.Budget.HasValue)
                {
                    targetHasBudget.Budget = sourceHasBudget.Budget;
                }

                if (replaceData || !targetHasBudget.Revenue.HasValue)
                {
                    targetHasBudget.Revenue = sourceHasBudget.Revenue;
                }
            }
        }

        private static void MergeMetascore(BaseItem source, BaseItem target, List<MetadataFields> lockedFields, bool replaceData)
        {
            var sourceCast = source as IHasMetascore;
            var targetCast = target as IHasMetascore;

            if (sourceCast != null && targetCast != null)
            {
                if (replaceData || !targetCast.Metascore.HasValue)
                {
                    targetCast.Metascore = sourceCast.Metascore;
                }
            }
        }

        private static void MergeAwards(BaseItem source, BaseItem target, List<MetadataFields> lockedFields, bool replaceData)
        {
            var sourceCast = source as IHasAwards;
            var targetCast = target as IHasAwards;

            if (sourceCast != null && targetCast != null)
            {
                if (replaceData || string.IsNullOrEmpty(targetCast.AwardSummary))
                {
                    targetCast.AwardSummary = sourceCast.AwardSummary;
                }
            }
        }

        private static void MergeCriticRating(BaseItem source, BaseItem target, List<MetadataFields> lockedFields, bool replaceData)
        {
            var sourceCast = source as IHasCriticRating;
            var targetCast = target as IHasCriticRating;

            if (sourceCast != null && targetCast != null)
            {
                if (replaceData || !targetCast.CriticRating.HasValue)
                {
                    targetCast.CriticRating = sourceCast.CriticRating;
                }

                if (replaceData || string.IsNullOrEmpty(targetCast.CriticRatingSummary))
                {
                    targetCast.CriticRatingSummary = sourceCast.CriticRatingSummary;
                }
            }
        }

        private static void MergeTaglines(BaseItem source, BaseItem target, List<MetadataFields> lockedFields, bool replaceData)
        {
            var sourceCast = source as IHasTaglines;
            var targetCast = target as IHasTaglines;

            if (sourceCast != null && targetCast != null)
            {
                if (replaceData || targetCast.Taglines.Count == 0)
                {
                    targetCast.Taglines = sourceCast.Taglines;
                }
            }
        }

        private static void MergeTrailers(BaseItem source, BaseItem target, List<MetadataFields> lockedFields, bool replaceData)
        {
            var sourceCast = source as IHasTrailers;
            var targetCast = target as IHasTrailers;

            if (sourceCast != null && targetCast != null)
            {
                if (replaceData || targetCast.RemoteTrailers.Count == 0)
                {
                    targetCast.RemoteTrailers = sourceCast.RemoteTrailers;
                }
            }
        }
    }
}