-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTvHeadendLibrary.cs
More file actions
507 lines (469 loc) · 21.2 KB
/
TvHeadendLibrary.cs
File metadata and controls
507 lines (469 loc) · 21.2 KB
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
using System.Web;
namespace TvHeadendRestApiClientLibrary
{
/// <summary>
/// Provides a library of functions to access the TvHeadend server.
/// This is free software that I made for myself in my spare time. I offer these freely, without financial intentions.
/// Necessary TvHeadend-Api-Version: 19
/// TvHeadend-Version: 4.2.8
///
/// Author: Oliver Matle
/// Date: August, 2021
/// </summary>
/// <seealso cref="https://github.com/Excogitatoris69/TvHeadendRestApiClientLibrary"/>
/// <seealso cref="https://tvheadend.org/"/>
/// <seealso cref="https://www.tvbrowser.org/"/>
public class TvHeadendLibrary
{
private HttpClient tvHeadendHttpclient = null;
private readonly string defaultLanguage = "und"; // tvheadend language -> "und" = "undetermined"
private readonly string defaultcomment = "";
public static readonly string RELEASESTRING = "1.2 , Juli 2022";
string APIPATH_CHANNELLIST = "/api/channel/list";
string APIPATH_LANGUAGELIST = "/api/language/list";
string APIPATH_SERVERINFO = "/api/serverinfo";
string APIPATH_GRIDUPCOMING = "/api/dvr/entry/grid_upcoming";
string APIPATH_DVRCANCEL = "/api/dvr/entry/cancel";
string APIPATH_DVRCREATE = "/api/dvr/entry/create";
string APIPATH_DVRCONFIGGRID = "/api/dvr/config/grid";
string APIPATH_EPGEVENTSGRID = "/api/epg/events/grid";
/// <summary>
/// Returns a list of all channels defined in TvHeadend.
/// </summary>
/// <param name="requestData"></param>
/// <returns></returns>
public ChannelEntryList GetChannellist(RequestData requestData)
{
if (requestData == null)
throw new NullReferenceException("RequestData is null.");
requestData.ServerUrlApi = APIPATH_CHANNELLIST;
var resultString = CallServer(requestData);
ChannelEntryList aChannelEntryList = (ChannelEntryList)JsonSerializer.Deserialize<ChannelEntryList>(resultString);
if (aChannelEntryList == null || aChannelEntryList.Entries.Count == 0)
{
throw new TvHeadendException(Messages.MESSAGE_NO_DATA_FOUND + ": List of channel is empty. ");
}
return aChannelEntryList;
}
/// <summary>
/// Returns a list of all languages defined in TvHeadend.
/// </summary>
/// <param name="requestData"></param>
/// <returns></returns>
public LanguageEntryList GetLanguagelist(RequestData requestData)
{
if (requestData == null)
throw new NullReferenceException("RequestData is null.");
requestData.ServerUrlApi = APIPATH_LANGUAGELIST;
var resultString = CallServer(requestData);
//Console.WriteLine(resultString);
LanguageEntryList aLanguageEntryList = (LanguageEntryList)JsonSerializer.Deserialize<LanguageEntryList>(resultString);
if(aLanguageEntryList == null || aLanguageEntryList.Entries.Count == 0)
{
throw new TvHeadendException(Messages.MESSAGE_NO_DATA_FOUND + ": List of languages is empty. ");
}
return aLanguageEntryList;
}
/// <summary>
/// Returns versioninfos about TvHeadend and Rest-API.
/// </summary>
/// <param name="requestData"></param>
/// <returns></returns>
public Serverinfo GetServerinfo(RequestData requestData)
{
if (requestData == null)
throw new NullReferenceException(Messages.MESSAGE_INVALID_REQUESTDATA + ". RequestData is null.");
requestData.ServerUrlApi = APIPATH_SERVERINFO;
var resultString = CallServer(requestData);
//Console.WriteLine(resultString);
Serverinfo aServerinfo = (Serverinfo)JsonSerializer.Deserialize<Serverinfo>(resultString);
if (aServerinfo == null)
{
throw new TvHeadendException(Messages.MESSAGE_NO_DATA_FOUND + ": Serverinfo is empty. ");
}
return aServerinfo;
}
/// <summary>
/// Returns a list of the currently-scheduled recordings.
/// </summary>
/// <param name="requestData"></param>
/// <returns></returns>
public DvrUpcomingEntryList GetDvrUpcominglist(RequestData requestData)
{
if (requestData == null)
throw new NullReferenceException("RequestData is null.");
requestData.ServerUrlApi = APIPATH_GRIDUPCOMING;
var resultString = CallServer(requestData);
//Console.WriteLine(resultString);
DvrUpcomingEntryList aDvrUpcomingEntryList = (DvrUpcomingEntryList)JsonSerializer.Deserialize<DvrUpcomingEntryList>(resultString);
return aDvrUpcomingEntryList;
}
/// <summary>
/// Removes a completed recording from storage.
/// </summary>
/// <param name="requestData"></param>
/// <returns></returns>
public TvHeadendResponseData RemoveDvrEntry(RequestData requestData)
{
DvrUpcomingEntryList list = null;
string requestUuid = null;
TvHeadendResponseData aTvHeadendResponseData = new TvHeadendResponseData();
aTvHeadendResponseData.Uuid = null;
list = GetDvrUpcominglist(requestData);
if (list.Entries != null)
{
foreach (DvrUpcomingEntry entry in list.Entries)
{
//search for either starttime and channelname or for uuid
if(requestData.Uuid != null)
{
if (entry.Owner == requestData.UserName && entry.Uuid == requestData.Uuid)
{
requestUuid = entry.Uuid;
break;
}
} else if(requestData.Starttime != 0 && requestData.ChannelName != null) {
if (!CheckChannelname(requestData, requestData.ChannelName))
{
throw new TvHeadendException(Messages.MESSAGE_INVALID_CHANNELNAME);
}
if (entry.Owner == requestData.UserName && entry.Start == requestData.Starttime && entry.Channelname == requestData.ChannelName)
{
requestUuid = entry.Uuid;
break;
}
}
else
{
throw new TvHeadendException(Messages.MESSAGE_INVALID_REQUESTDATA + ". Invalid Channel, starttime or uuid.");
}
}
if (requestUuid == null)
{
throw new TvHeadendException(Messages.MESSAGE_UNKNOWN_DVR_ENTRY);
}
}
requestData.ServerUrlApi = APIPATH_DVRCANCEL + "?uuid=" + requestUuid;
var resultString = CallServer(requestData);
//Console.WriteLine(resultString);
aTvHeadendResponseData.Uuid = requestUuid;
return aTvHeadendResponseData;
}
/// <summary>
/// Creates a new epg-derived timer.
/// </summary>
/// <param name="requestData"></param>
/// <returns></returns>
public TvHeadendResponseData AddDvrEntry(RequestData requestData)
{
string dvrConfigUuid = null;
TvHeadendResponseData aTvHeadendResponseData = null;
if (requestData == null)
throw new TvHeadendException(Messages.MESSAGE_INVALID_REQUESTDATA + ". RequestData is null.");
if (requestData.DvrProfileName != null)
{
// get uuid of DVR-Profilename, if it is not null
bool validDvrConfigname = CheckDvrConfigname(requestData, requestData.DvrProfileName, out dvrConfigUuid);
if(!validDvrConfigname)
throw new TvHeadendException(Messages.MESSAGE_INVALID_DVR_CONFIGNAME + ":" + requestData.DvrProfileName);
}
if (!CheckChannelname(requestData, requestData.ChannelName))
{
throw new TvHeadendException(Messages.MESSAGE_INVALID_CHANNELNAME+":"+requestData.ChannelName);
}
if (requestData.Language != null)
{
if (!CheckLanguage(requestData, requestData.Language))
{
throw new TvHeadendException(Messages.MESSAGE_INVALID_LANGUAGE + ":" + requestData.Language);
}
}
if (requestData.Title == null || requestData.Title.Length == 0)
{
throw new TvHeadendException(Messages.MESSAGE_INVALID_REQUESTDATA + ". Title not set or empty.");
}
//if (requestData.Description == null || requestData.Description.Length == 0)
//{
// throw new TvHeadendException(Messages.MESSAGE_INVALID_REQUESTDATA + ". Description not set or empty.");
//}
if (requestData.Starttime == 0)
{
throw new TvHeadendException(Messages.MESSAGE_INVALID_REQUESTDATA + ". Starttime not set.");
}
if (requestData.Endtime == 0)
{
throw new TvHeadendException(Messages.MESSAGE_INVALID_REQUESTDATA + ". Endtime not set.");
}
if(requestData.Starttime >= requestData.Endtime)
{
throw new TvHeadendException(Messages.MESSAGE_INVALID_TIME + ". Starttime must be lower than Endtime.");
}
long unixTimestampNow = DateTimeOffset.Now.ToUnixTimeSeconds();
if (requestData.Endtime < unixTimestampNow)
{
throw new TvHeadendException(Messages.MESSAGE_INVALID_REQUESTDATA + ". The end time must not be in the past.");
}
// build Json-Object
DvrAddEntry dvrAddEntry = new DvrAddEntry();
dvrAddEntry.Enabled = true;
dvrAddEntry.Start = requestData.Starttime;
dvrAddEntry.Stop = requestData.Endtime;
dvrAddEntry.Channelname = requestData.ChannelName;
if (requestData.Priority == Priority.Unknown)
dvrAddEntry.Priority = Priority.Normal;
else
dvrAddEntry.Priority = requestData.Priority;
dvrAddEntry.DvrProfileUuid = dvrConfigUuid;
DvrTitle aTitle = new DvrTitle();
DvrTitle aSubTitle = new DvrTitle();
aTitle.Language = new Dictionary<string, object>();
aSubTitle.Language = new Dictionary<string, object>();
if (requestData.Language == null)
{
aTitle.Language.Add(defaultLanguage, requestData.Title);
aSubTitle.Language.Add(defaultLanguage, requestData.Description);
}
else{
aTitle.Language.Add(requestData.Language, requestData.Title);
aSubTitle.Language.Add(requestData.Language, requestData.Description);
}
dvrAddEntry.Title = aTitle;
dvrAddEntry.Description = aSubTitle;
if (requestData.Comment == null)
{
dvrAddEntry.Comment = defaultcomment;
}
else
{
dvrAddEntry.Comment = requestData.Comment;
}
var options = new JsonSerializerOptions
{
IgnoreNullValues = true,
Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
};
string jsonDvrRequestString = JsonSerializer.Serialize(dvrAddEntry, options);
//Console.WriteLine(jsonDvrRequestString);
string encodedUrl = HttpUtility.UrlEncode(jsonDvrRequestString);
requestData.ServerUrlApi = APIPATH_DVRCREATE + "?conf=" + encodedUrl;
var resultString = CallServer(requestData);
//Console.WriteLine(resultString);
aTvHeadendResponseData = (TvHeadendResponseData)JsonSerializer.Deserialize<TvHeadendResponseData>(resultString);
if (aTvHeadendResponseData.Uuid == null || aTvHeadendResponseData.Uuid.Length == 0)
{
throw new TvHeadendException(Messages.MESSAGE_DVR_CREATE_ENTRY_UNSUCCESSFUL);
}
return aTvHeadendResponseData;
}
public DvrConfigEntryList GetDvrConfiglist(RequestData requestData)
{
if (requestData == null)
throw new NullReferenceException("RequestData is null.");
requestData.ServerUrlApi = APIPATH_DVRCONFIGGRID;
var resultString = CallServer(requestData);
//Console.WriteLine(resultString);
DvrConfigEntryList aDvrConfigEntryList = (DvrConfigEntryList)JsonSerializer.Deserialize<DvrConfigEntryList>(resultString);
return aDvrConfigEntryList;
}
/// <summary>
/// Query the EPG and optionally apply filters.
/// </summary>
/// <param name="requestData"></param>
/// <param name="epgFilter"></param>
/// <returns></returns>
public EpgEntryList GetEpgEntryList(RequestData requestData, EpgFilter epgFilter)
{
if (requestData == null)
throw new NullReferenceException("RequestData is null.");
StringBuilder filterString = null;
//set filter
if(epgFilter == null)
{
requestData.ServerUrlApi = APIPATH_EPGEVENTSGRID;
}else
{
filterString = new StringBuilder(APIPATH_EPGEVENTSGRID);
filterString.Append('?');
if (epgFilter.Start > 0) //default is 0
filterString.Append("start=").Append(epgFilter.Start);
if (epgFilter.Limit > 0) //default is 50
filterString.Append(",limit=").Append(epgFilter.Limit);
if (epgFilter.Channelname != null)
filterString.Append(",channel=").Append(epgFilter.Channelname);
if (epgFilter.NowMode)
filterString.Append(",mode=now");
if (epgFilter.DurationMin >= 0)
filterString.Append(",durationMin=").Append(epgFilter.DurationMin);
if (epgFilter.DurationMax >= 0)
filterString.Append(",durationMax=").Append(epgFilter.DurationMax);
filterString.Replace("?,","?");
requestData.ServerUrlApi = filterString.ToString();
}
//?start=0&limit=200&channel=ProSieben,ZDF&durationMin=1&durationMax=1000
var resultString = CallServer(requestData);
Console.WriteLine(filterString.ToString());
EpgEntryList aEpgEntryList = (EpgEntryList)JsonSerializer.Deserialize<EpgEntryList>(resultString);
return aEpgEntryList;
}
public string getLivestreamUrl(RequestData requestData)
{
string streamUrl = null;
string channelUuid = null;
string protocol = null;
if (requestData == null)
throw new TvHeadendException(Messages.MESSAGE_INVALID_REQUESTDATA + ". RequestData is null.");
//search channel-uuid of channelname
ChannelEntryList list = GetChannellist(requestData);
if (requestData.ChannelName != null && requestData.ChannelName.Trim().Length > 0 && list.Entries != null)
{
foreach (ChannelEntry channelEntry in list.Entries)
{
if (channelEntry.Name.Equals(requestData.ChannelName))
{
channelUuid = channelEntry.Uuid;
break;
}
}
}else
{
throw new TvHeadendException(Messages.MESSAGE_INVALID_CHANNELNAME + ":" + requestData.ChannelName);
}
if (channelUuid != null)
{
if (requestData.ServerUrl.StartsWith("http"))
{
protocol = "http";
}else if (requestData.ServerUrl.StartsWith("https"))
{
protocol = "https";
}
streamUrl = string.Format("{0}://{1}:{2}@{3}/stream/channel/{4}",
protocol, requestData.UserName, requestData.Password,requestData.ServerUrl.Replace(protocol+"://", ""), channelUuid);
}
return streamUrl;
}
/// <summary>
/// Connect to server.
/// </summary>
/// <param name="requestData"></param>
/// <returns></returns>
private string CallServer(RequestData requestData)
{
string result = null;
HttpResponseMessage resonse = null;
if (tvHeadendHttpclient == null)
{
tvHeadendHttpclient = new HttpClient();
var authToken = Encoding.ASCII.GetBytes($"{requestData.UserName}:{requestData.Password}");
tvHeadendHttpclient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(authToken));
}
try
{
resonse = tvHeadendHttpclient.GetAsync(requestData.ServerUrl + requestData.ServerUrlApi).Result;
}
catch(System.AggregateException e)
{
throw new TvHeadendException(Messages.MESSAGE_INVALID_REQUESTDATA + ". Possibly wrong serverurl or wrong port. Error:" + e.Message);
}
if (resonse.IsSuccessStatusCode)
{
var responseContent = resonse.Content;
// by calling .Result you are synchronously reading the result
result = responseContent.ReadAsStringAsync().Result;
//Console.WriteLine("Result:"+ result);
}
else
{
switch (resonse.StatusCode)
{
case System.Net.HttpStatusCode.Unauthorized:
throw new TvHeadendException(Messages.MESSAGE_INVALID_REQUESTDATA + ": Wrong Username or password.");
default:
throw new TvHeadendException(Messages.MESSAGE_INVALID_REQUESTDATA + ": " + resonse.StatusCode);
}
}
return result;
}
/// <summary>
/// Check for valid DVR-Configurationname
/// </summary>
/// <param name="requestdata"></param>
/// <param name="configname"></param>
/// <param name="uuid"></param>
/// <returns></returns>
private bool CheckDvrConfigname(RequestData requestdata, string configname, out string uuid)
{
bool result = true;
uuid = null;
DvrConfigEntryList list = GetDvrConfiglist(requestdata);
if(list.Entries != null && list.Entries.Count > 0)
{
foreach( DvrConfigEntry entry in list.Entries)
{
if(entry.Name == configname)
{
uuid = entry.Uuid;
break;
}
}
}
else
{
result = false; // configname do not exist, if empty list.
}
return result;
}
/// <summary>
/// Check for valid channelname.
/// </summary>
/// <param name="requestdata"></param>
/// <param name="channelname"></param>
/// <returns></returns>
private bool CheckChannelname(RequestData requestdata, string channelname)
{
bool result = false;
ChannelEntryList list = GetChannellist(requestdata);
if (channelname != null && channelname.Length > 0 && list.Entries != null)
{
foreach (ChannelEntry entry in list.Entries)
{
if (entry.Name == channelname)
{
result = true;
break;
}
}
}
return result;
}
/// <summary>
/// Check for valid language.
/// </summary>
/// <param name="requestdata"></param>
/// <param name="language"></param>
/// <returns>True, if valid.</returns>
private bool CheckLanguage(RequestData requestdata, string language)
{
bool result = false;
LanguageEntryList list = GetLanguagelist(requestdata);
if (language != null && language.Length >0 && list.Entries != null)
{
foreach (LanguageEntry entry in list.Entries)
{
if (entry.Shortname == language)
{
result = true;
break;
}
}
}
return result;
}
}
}