aboutsummaryrefslogtreecommitdiff
path: root/src/Jellyfin.LiveTv/Listings/EpgChannelData.cs
blob: 81437f791372aad92455a145f57ff39ed640dd4c (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
#pragma warning disable CS1591

using System;
using System.Collections.Generic;
using MediaBrowser.Controller.LiveTv;

namespace Jellyfin.LiveTv.Listings
{
    internal class EpgChannelData
    {
        private readonly Dictionary<string, ChannelInfo> _channelsById;

        private readonly Dictionary<string, ChannelInfo> _channelsByNumber;

        private readonly Dictionary<string, ChannelInfo> _channelsByName;

        public EpgChannelData(IEnumerable<ChannelInfo> channels)
        {
            _channelsById = new Dictionary<string, ChannelInfo>(StringComparer.OrdinalIgnoreCase);
            _channelsByNumber = new Dictionary<string, ChannelInfo>(StringComparer.OrdinalIgnoreCase);
            _channelsByName = new Dictionary<string, ChannelInfo>(StringComparer.OrdinalIgnoreCase);

            foreach (var channel in channels)
            {
                _channelsById[channel.Id] = channel;

                if (!string.IsNullOrEmpty(channel.Number))
                {
                    _channelsByNumber[channel.Number] = channel;
                }

                var normalizedName = NormalizeName(channel.Name ?? string.Empty);
                if (!string.IsNullOrWhiteSpace(normalizedName))
                {
                    _channelsByName[normalizedName] = channel;
                }
            }
        }

        public ChannelInfo? GetChannelById(string id)
            => _channelsById.GetValueOrDefault(id);

        public ChannelInfo? GetChannelByNumber(string number)
            => _channelsByNumber.GetValueOrDefault(number);

        public ChannelInfo? GetChannelByName(string name)
            => _channelsByName.GetValueOrDefault(name);

        public static string NormalizeName(string value)
        {
            return value.Replace(" ", string.Empty, StringComparison.Ordinal).Replace("-", string.Empty, StringComparison.Ordinal);
        }
    }
}