-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHomerun.cs
605 lines (537 loc) · 20.6 KB
/
Homerun.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
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
/*
The MIT License (MIT)
Copyright © 2022 David Zangger
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the “Software”), to deal in the Software without
restriction, including without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom
the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Spectre.Console;
namespace HdHomerun
{
/// <summary>
/// This is the main HDHomerun API.
/// </summary>
public class Keep
{
public string SeriesID { get; set; }
public int? EpisodesToKeep { get; set; }
public Keep(string seriesID)
{
this.SeriesID = seriesID;
}
}
public class Protect
{
public string ProgramId { get; set; }
}
public class Discovery
{
public string FriendlyName { get; set; }
public string ModelNumber { get; set; }
public string FirmwareName { get; set; }
public string FirmwareVersion { get; set; }
public string DeviceID { get; set; }
public string DeviceAuth { get; set; }
public string BaseURL { get; set; }
public string LineupURL { get; set; }
public int TunerCount { get; set; }
public string StorageID { get; set; }
public string StorageURL { get; set; }
public long TotalSpace { get; set; }
public long FreeSpace { get; set; }
public long UsedSpace {
get { return TotalSpace - FreeSpace; }
}
}
public class Device
{
public string DeviceId { get; set; }
public string LocalIP { get; set; }
public string BaseURL { get; set; }
public string DiscoverURL { get; set; }
public string LineupURL { get; set; }
}
public class Channel
{
public int Seq { get; set; }
public string GuideNumber { get; set; }
public string GuideName { get; set; }
public string VideoCodec { get; set; }
public string AudioCodec { get; set; }
public string URL { get; set; }
public Channel()
{
VideoCodec = "";
AudioCodec = "";
}
}
public class Serial
{
public List<Recording> Recordings { get; set; }
public int Seq { get; set; }
public string SeriesID { get; set; }
public string Title { get; set; }
public string Category { get; set; }
public string ImageURL { get; set; }
public long StartTime { get; set; }
public string EpisodesURL { get; set; }
public string UpdateID { get; set; }
public int? EpisodesToKeep { get; set; }
public Serial()
{
this.Recordings = new List<Recording>();
this.EpisodesToKeep = null;
}
/// <summary>
/// Clean up any recordings beyond what they want to keep
/// </summary>
/// <param name="DoClean"></param>
/// <returns>The number of recordings removed</returns>
public int Clean(bool DoClean)
{
int RecordingsRemoved = 0;
// Make sure we check for recordings if we don't have any
if (Recordings.Count == 0)
{
Homerun.GetRecordingsForSerial(this);
}
// Make sure there is a number here. If not, we intend to keep all the recordings
if (EpisodesToKeep != null)
{
int count = 0;
// Find all the episodes after the last one we want to keep
foreach(Recording recording in Recordings)
{
// We skip any recording that's protected by the user
if (!recording.Protect)
{
if (++count > this.EpisodesToKeep)
{
AnsiConsole.MarkupLine($"[white on red] Delete -> [/] Seq #{recording.Seq} - {recording.Title}");
recording.Delete(DoClean);
recording.Deleted = true;
RecordingsRemoved++;
}
}
}
}
return RecordingsRemoved;
}
}
public class Recording
{
public int Seq { get; set; }
public string Category { get; set; }
public string ChannelAffiliate { get; set; }
public string ChannelImageURL { get; set; }
public string ChannelName { get; set; }
public string ChannelNumber { get; set; }
public long EndTime { get; set; }
public string EpisodeNumber { get; set; }
public string EpisodeTitle { get; set; }
public string ImageURL { get; set; }
public long OriginalAirdate { get; set; }
public string ProgramID { get; set; }
public long RecordedEndTime { get; set; }
public long RecordStartTime { get; set; }
public string SeriesID { get; set; }
public long StartTime { get; set; }
public string Synopsis { get; set; }
public string Title { get; set; }
public string Filename { get; set; }
public string PlayURL { get; set; }
public string CmdURL { get; set; }
public bool Deleted { get; set; }
public long? FileSize { get; set; }
public bool Protect { get; set; }
public Recording()
{
Deleted = false;
FileSize = null;
Protect = false;
}
/// <summary>
/// Deletes the recording.
/// </summary>
/// <param name="DoDelete">If we are in simulation mode, this will be false.</param>
public void Delete(bool DoDelete)
{
// If we are not in simulation mode, then delete the file
if (DoDelete)
{
// Use the recordings CmdURL and append &cmd=delete to it
string uri = $"{this.CmdURL}&cmd=delete";
// Try and delete the recording
try
{
Deleted = WebAPI.DeleteRecording(uri);
}
catch (Exception)
{
Deleted = false;
}
}
else
{
AnsiConsole.MarkupLine("[red]We didn't really delete your recording...[/]");
Deleted = true;
}
}
/// <summary>
/// Gets the filesize of the recording using a web request. The FileSize property will be
/// set so we don't have to keep making extra web requests.
/// </summary>
/// <returns>The filesize in Megabytes. -1 if there was an error</returns>
public long GetFileSize()
{
try
{
// If we haven't determined the filesize yet, do it now.
if (FileSize == null)
{
long fileSize = WebAPI.GetRecoringFileSize(this.PlayURL);
FileSize = (fileSize / 1000 / 1000);
}
// return the filesize
return FileSize.Value;
}
catch (Exception)
{
return -1;
}
}
}
public class Rule
{
public long RecordingRuleID { get; set; }
public string SeriesID { get; set; }
public string Title { get; set; }
public string Synopsis { get; set; }
public string Category { get; set; }
public string ImageURL { get; set; }
public string ChannelOnly { get; set; }
public int RecentOnly { get; set; }
public int Priority { get; set; }
}
public class Status
{
public string Resource { get; set; }
public string VctNumber { get; set; }
public string VctName { get; set; }
public string Frequency { get; set; }
public int SignalStrengthPercent { get; set; }
public int SignalQualityPercent { get; set; }
public int SymbolQualityPercent { get; set; }
public string TargetIP { get; set; }
public int NetworkRate { get; set; }
public string Name { get; set; }
}
internal static class Homerun
{
public static Device DeviceInfo;
public static Discovery DiscoveryInfo = null;
public static List<Serial> Series = new List<Serial>();
public static List<Channel> Channels = new List<Channel>();
public static List<Keep> Keeps = new List<Keep>();
public static List<Protect> Protects = new List<Protect>();
public static List<Rule> Rules = new List<Rule>();
public static List<Status> Statuses = new List<Status>();
/// <summary>
/// Get's the current log file
/// </summary>
/// <returns></returns>
public static string GetLog()
{
string uri = DiscoveryInfo.BaseURL + "/log.html";
string sLogContents = WebAPI.GetContents(uri);
int index1 = sLogContents.IndexOf("<pre>");
int index2 = sLogContents.IndexOf("</pre>");
string log = sLogContents.Substring(index1 + 6, index2 - index1 - 6);
return log;
}
/// <summary>
/// Do the discovery
/// </summary>
public static void DoDiscovery()
{
// Discover the other details
string sDiscoveryInfoAsJson = WebAPI.GetContents(DeviceInfo.DiscoverURL);
// Convert the JSON returned into an actual Discovery Info object
var discoveryInfo = JsonConvert.DeserializeObject<Discovery>(sDiscoveryInfoAsJson);
DiscoveryInfo = discoveryInfo;
}
/// <summary>
/// Do the discovery using Async
/// </summary>
public static void DoDiscoveryAsync()
{
// Discover the other details
Task<string> sDiscoveryInfoAsJson = WebAPI.GetContentsAsync(DeviceInfo.DiscoverURL);
// Convert the JSON returned into an actual Discovery Info object
var discoveryInfo = JsonConvert.DeserializeObject<Discovery>(sDiscoveryInfoAsJson.ToString());
DiscoveryInfo = discoveryInfo;
}
/// <summary>
/// Init the device by querying the data from hdhomerun.com
/// </summary>
public static void Init()
{
try
{
// This will query the device information
string sDeviceInfoasJson = WebAPI.GetContents(@"https://ipv4-api.hdhomerun.com/discover");
// Convert the JSON returned into an actual Device Info object
var deviceInfo = JsonConvert.DeserializeObject<Device[]>(sDeviceInfoasJson);
// Assign the information to the object
DeviceInfo = deviceInfo[0];
// Do the discovery
DoDiscovery();
// Look for serial keeps. These are the number of recorings to keep for each serial
string keepsFile = $"{DeviceInfo.DeviceId}_Keeps.json";
if (File.Exists(keepsFile))
{
// Read the current values
Keeps = JsonConvert.DeserializeObject<List<Keep>>(File.ReadAllText(keepsFile));
}
else
{
// Save an empty file
SaveKeeps();
}
string protectFile = $"{DeviceInfo.DeviceId}_Protects.json";
if (File.Exists(protectFile))
{
Protects = JsonConvert.DeserializeObject<List<Protect>>(File.ReadAllText(protectFile));
}
else
{
SaveProtects();
}
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// Get the current status of the device
/// </summary>
public static void GetStatus()
{
string uri = $"{DiscoveryInfo.BaseURL}/status.json";
string sStatusAsJson = WebAPI.GetContents(uri);
Statuses.Clear();
var statuses = JsonConvert.DeserializeObject<Status[]>(sStatusAsJson);
foreach (Status status in statuses)
{
Statuses.Add(status);
}
}
/// <summary>
/// Protect, or unprotect, a recording
/// </summary>
/// <param name="recording">The recording to protect/unprotect</param>
public static void ProtectRecording(Recording recording)
{
// First determine if it's in the list of protected recordings already
Protect protect = Protects.FirstOrDefault(p => p.ProgramId == recording.ProgramID);
// If we are to protect this recording, add it to the list of protected recordings
if (recording.Protect)
{
if (protect == null)
Protects.Add(new Protect { ProgramId = recording.ProgramID });
}
else
{
// Remove it from the list of protected recordings
Protects.Remove(protect);
}
// Save the list of protected recordings
SaveProtects();
}
/// <summary>
/// Save the protected recordings to the json file
/// </summary>
private static void SaveProtects()
{
// set the name of the protects file
string protectFile = $"{DeviceInfo.DeviceId}_Protects.json";
// write the protects info to the file
File.WriteAllText(protectFile, JsonConvert.SerializeObject(Protects));
}
/// <summary>
/// Save the serial keep details to a file
/// </summary>
private static void SaveKeeps()
{
// Set the name of the keeps file
string keepsFile = $"{DeviceInfo.DeviceId}_Keeps.json";
// Write the keeps data to the file
File.WriteAllText(keepsFile, JsonConvert.SerializeObject(Keeps));
}
/// <summary>
/// Get the rules
/// </summary>
public static void GetRules()
{
if (DiscoveryInfo == null)
{
DoDiscovery();
}
string uri = $"https://api.hdhomerun.com/api/recording_rules?DeviceAuth={DiscoveryInfo.DeviceAuth}";
string sRulesAsJson = WebAPI.GetContents(uri);
var rules = JsonConvert.DeserializeObject<Rule[]>(sRulesAsJson);
// Add each rule
foreach(Rule rule in rules)
{
Rules.Add(rule);
}
}
/*
* GetRecordingsForSerial
* Get all the recordings for a serial
*/
public static void GetRecordingsForSerial(Serial serial)
{
// if a serial was found, find the recordings
if (serial != null)
{
int sequence = 1;
string sRecordingsAsJson = WebAPI.GetContents(serial.EpisodesURL);
if (sRecordingsAsJson != null)
{
// We need to replace any recordings we've already seen since we are
// getting a new list of recordings
serial.Recordings.Clear();
var recordings = JsonConvert.DeserializeObject<Recording[]>(sRecordingsAsJson);
foreach (Recording recording in recordings)
{
recording.Seq = sequence++;
// Look for the recording to see if it's protected (not to be deleted)
Protect protect = Protects.FirstOrDefault(p => p.ProgramId == recording.ProgramID);
recording.Protect = (protect != null);
// Add the recording to the list of recordings for this serial
serial.Recordings.Add(recording);
}
}
}
else
throw new Exception("Serial not found");
}
/*
* GetChannels
* Obtains a list of all the available channels
*/
public static void GetChannels(bool force = false)
{
if (Channels.Count == 0 || force)
{
int sequence = 1;
string uri = DeviceInfo.LineupURL;
string sChannelsAsJson = WebAPI.GetContents(uri);
Channels.Clear();
var channels = JsonConvert.DeserializeObject<Channel[]>(sChannelsAsJson);
foreach (Channel channel in channels)
{
channel.Seq = sequence++;
Channels.Add(channel);
}
}
}
/*
* GetAllSeries
* Obtains a list of all the scheduled series (shows)
*/
public static void GetAllSeries(bool force = false)
{
// if we haven't been here yet, get a list of the series
if (Series.Count == 0 || force)
{
try
{
int sequence = 1;
string sSeriesAsJson = WebAPI.GetContents(DiscoveryInfo.StorageURL);
Series.Clear();
var series = JsonConvert.DeserializeObject<Serial[]>(sSeriesAsJson);
foreach (Serial serial in series)
{
serial.Seq = sequence++;
// Look for keeps
Keep keep = Keeps.FirstOrDefault(k => k.SeriesID == serial.SeriesID);
// If we found a matching seriesId,
// set the number of recordings to keep for the series
if (keep != null)
serial.EpisodesToKeep = keep.EpisodesToKeep;
// Add the serial to the collection
Series.Add(serial);
}
}
catch (Exception)
{
throw;
}
}
}
/*
* SetEpisodesToKeep
* Sets the number of episodes to keep for a serial. If episodesToKeep is null,
* it means all episodes will be saved
*/
public static void SetEpisodesToKeep(Serial serial, int? episodesToKeep)
{
// Set the filename for the keeps file
string keepsFile = $"{DeviceInfo.DeviceId}_Keeps.json";
try
{
// Do we already have this in our list?
Keep keep = Keeps.First(k => k.SeriesID == serial.SeriesID);
// If we want to keep some episodes, update the number
if (episodesToKeep != null)
{
keep.EpisodesToKeep = episodesToKeep.Value;
}
else
{
serial.EpisodesToKeep = null;
Keeps.Remove(keep);
}
}
catch (Exception)
{
// Add a new record to the keeps list
Keep keep = new Keep(serial.SeriesID);
keep.EpisodesToKeep = episodesToKeep;
Keeps.Add(keep);
}
// Save the changes
serial.EpisodesToKeep = episodesToKeep;
SaveKeeps();
}
/*
* ToLocalTime
* Converts a unix time to the local time
*/
public static string ToLocalTime(long l)
{
DateTimeOffset offset = DateTimeOffset.FromUnixTimeSeconds(l);
DateTime currentTime = offset.DateTime.ToLocalTime();
return currentTime.ToString("dd-MMM-yyyy HH:mm:ss");
}
}
}