-
Notifications
You must be signed in to change notification settings - Fork 4
/
Updater.cs
167 lines (139 loc) · 5.77 KB
/
Updater.cs
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
using IPTVTuner.Model;
using Newtonsoft.Json;
using System;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
namespace IPTVTuner
{
/**
* Builds the local lineup and EPG data by from the provider data.
*/
class Updater
{
private readonly Config config;
private readonly Lineup lineup;
public Updater(Config config, Lineup lineup)
{
this.config = config;
this.lineup = lineup;
}
/**
* Update the local lineup and EPG.
*/
public async Task Update()
{
try
{
// Update the available channels from.
var channels = await UpdateChannels();
// Use the channels to build a local EPG.
await UpdateEPG(channels);
}
catch (Exception e)
{
config.WriteLog(true, "Channel update failed: {0}", e.Message);
}
}
/**
* Download the provider data and build the local lineup.
*/
public async Task<ProviderChannels> UpdateChannels()
{
// Log the start of this long running update process.
config.WriteLog(false, "Beginning provider channel update for {0}.", config.M3UURL);
// Obtain the channel list from the provider.
var channels = await GetChannelsFromM3U();
// Fail if there are too many channels for Plex.
if (channels.Count() > config.MaxChannels)
{
throw new TooManyChannelsException(channels.Count(), config.MaxChannels);
}
// Generate IDs for channels without one.
channels.ForEach(channel => channel.ID = channel.GetOrCreateID());
// Generate a lineup item for each channel.
var localChannels = channels.Select((channel, index) =>
{
var channelNumber = (config.StartChannel + index).ToString();
return new HDHomeRunLineupItem
{
GuideName = channel.Name,
GuideNumber = channelNumber,
HD = channel.IsHD(),
// The URL to tune this channel on our proxy service.
URL = config.ServerUrl("/auto/" + channelNumber)
};
}).ToList();
// Store the lineup in memory so channels may be served.
lineup.Channels = channels.Select((channel, index) =>
{
var channelNumber = (config.StartChannel + index).ToString();
return new ChannelQuad
{
ID = channel.ID,
Name = channel.Name,
ChannelNumber = channelNumber,
// The actual URL of the provider's stream.
URL = channel.URL
};
}).ToList();
// Serialize the lineup items to lineup.json on disk.
var outputPath = Path.Combine(config.DataPath, "lineup.json");
using (StreamWriter writer = new StreamWriter(outputPath, false))
{
JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(writer, localChannels);
}
// Log the number of channels loaded into the lineup.
config.WriteLog(false, "Loaded {0} channels into the lineup.", localChannels.Count());
// Log the end of this long running update process.
config.WriteLog(false, "Wrote updated local lineup to {0}.", outputPath);
return channels;
}
/**
* Fetch and filter the M3U from the provider.
*/
public async Task<ProviderChannels> GetChannelsFromM3U()
{
// Prepare a filter to match channels.
var filter = new ChannelFilter(config);
// Prepare a parser to parse the M3U.
var parser = new M3UParser
{
Filter = filter.Predicate
};
using (HttpClient client = new HttpClient())
{
// Open a stream to the Provider M3U.
var res = await client.GetStreamAsync(config.M3UURL);
var streamReader = new StreamReader(res);
// Parse the M3U and extract the channels.
return await parser.Parse(streamReader);
}
}
/**
* Download the provider EPG data and build the local EPG.
*/
public async Task UpdateEPG(ProviderChannels channels)
{
// Log the start of this long running update process.
config.WriteLog(false, "Beginning provider EPG update for {0}.", config.EPGURL);
// Build a skeleton EPG using the channels in the lineup.
EPGBuilder epg = new EPGBuilder(config, lineup);
epg.CreateChannels(channels);
// Fetch and merge the EPG from the provider.
using (HttpClient client = new HttpClient())
{
var res = await client.GetStreamAsync(config.EPGURL);
var streamReader = new StreamReader(res);
epg.MergeXMLTV(streamReader);
}
// Serialize the EPG items to epg.xml on disk.
var outputPath = Path.Combine(config.DataPath, "epg.xml");
epg.WriteToDisk(outputPath);
// Log the end of this long running update process.
config.WriteLog(false, "Wrote updated local EPG to {0}.", outputPath);
}
}
}