Skip to content

Commit d9e4f99

Browse files
authored
Add files via upload
1 parent d85912c commit d9e4f99

File tree

7 files changed

+4459
-0
lines changed

7 files changed

+4459
-0
lines changed

source/Cache.cs

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.IO;
5+
using System.Linq;
6+
7+
namespace yrewind
8+
{
9+
// Caching the technical information about the stream in a temporary file
10+
// Line format for each live stream:
11+
// [dateTimeOfGettingInfo] [id] [idStatus] [channelId] [uriAdirect] [uriVdirect] [jsonHtmlStr]
12+
class Cache
13+
{
14+
#region Read - Read required data from cache
15+
public void Read(
16+
string id,
17+
out string idStatus,
18+
out string channelId,
19+
out string uriAdirect,
20+
out string uriVdirect,
21+
out string jsonHtmlStr
22+
)
23+
{
24+
idStatus = string.Empty;
25+
channelId = string.Empty;
26+
uriAdirect = string.Empty;
27+
uriVdirect = string.Empty;
28+
jsonHtmlStr = string.Empty;
29+
30+
foreach (var line in GetContent())
31+
{
32+
if (line == string.Empty) continue;
33+
34+
var lineId = line.Split('\t')[1];
35+
36+
if (id.Trim() == lineId.Trim())
37+
{
38+
idStatus = line.Split('\t')[2];
39+
channelId = line.Split('\t')[3];
40+
uriAdirect = line.Split('\t')[4];
41+
uriVdirect = line.Split('\t')[5];
42+
jsonHtmlStr = line.Split('\t')[6];
43+
}
44+
}
45+
}
46+
#endregion
47+
48+
#region Write - Write cache file
49+
public void Write(
50+
string id,
51+
string idStatus,
52+
string channelId,
53+
string uriAdirect,
54+
string uriVdirect,
55+
string jsonHtmlStr
56+
)
57+
{
58+
var content = GetContent();
59+
var newData =
60+
(Program.Start + Program.Timer.Elapsed).ToString("yyyyMMdd-HHmmss") + '\t' +
61+
id + '\t' +
62+
idStatus + '\t' +
63+
channelId + '\t' +
64+
uriAdirect + '\t' +
65+
uriVdirect + '\t' +
66+
jsonHtmlStr;
67+
68+
var match = content.FirstOrDefault(x => x.Contains("\t" + id + "\t"));
69+
if (match == null)
70+
{
71+
content.Add(newData);
72+
}
73+
else if ((match.Split('\t')[4] == string.Empty) & (uriAdirect != string.Empty))
74+
{
75+
// Prefer data containing direct browser URLs
76+
content.Remove(match);
77+
content.Add(newData);
78+
}
79+
80+
if (Validator.Log)
81+
{
82+
Program.Log(string.Join(Environment.NewLine, content), "cache");
83+
}
84+
85+
try
86+
{
87+
File.WriteAllLines(Constants.PathCache, content);
88+
}
89+
catch (Exception e)
90+
{
91+
Program.ErrInfo = new StackFrame(0, true).GetFileLineNumber() + " - " + e.Message;
92+
if (Validator.Log) Program.Log(Program.ErrInfo);
93+
}
94+
}
95+
#endregion
96+
97+
#region Delete - Delete cache file
98+
public void Delete()
99+
{
100+
try
101+
{
102+
File.Delete(Constants.PathCache);
103+
}
104+
catch (Exception e)
105+
{
106+
Program.ErrInfo = new StackFrame(0, true).GetFileLineNumber() + " - " + e.Message;
107+
if (Validator.Log) Program.Log(Program.ErrInfo);
108+
}
109+
}
110+
#endregion
111+
112+
#region GetContent - Read unexpired data from cache file
113+
List<string> GetContent()
114+
{
115+
DateTime dtAdded;
116+
var content = new List<string>();
117+
118+
try
119+
{
120+
if (new FileInfo(Constants.PathCache).Length > 1000000) throw new Exception();
121+
122+
foreach (var line in File.ReadAllLines(Constants.PathCache))
123+
{
124+
if (line == string.Empty) continue;
125+
126+
dtAdded = DateTime.ParseExact(line.Split('\t')[0], "yyyyMMdd-HHmmss", null);
127+
var dtExpiration = dtAdded.AddMinutes(Constants.CacheShelflifeMinutes);
128+
if (Program.Start + Program.Timer.Elapsed > dtExpiration) continue;
129+
130+
content.Add(line);
131+
}
132+
}
133+
catch (Exception e)
134+
{
135+
Program.ErrInfo = new StackFrame(0, true).GetFileLineNumber() + " - " + e.Message;
136+
if (Validator.Log) Program.Log(Program.ErrInfo);
137+
}
138+
139+
return content;
140+
}
141+
#endregion
142+
}
143+
}

0 commit comments

Comments
 (0)