-
Notifications
You must be signed in to change notification settings - Fork 0
/
Show.cs
215 lines (170 loc) · 5.56 KB
/
Show.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
// READ FEEDS
using System.ServiceModel;
using System.ServiceModel.Syndication;
using System.Xml;
using System.Xml.Linq;
namespace SeriesService
{
class Show
{
#region Attributes
public string FileName { get; set; }
public FrequencyValues Frequency = FrequencyValues.weekly;
public Show()
{
SavePath = Directory.GetCurrentDirectory() + "\\Downloads\\";
}
public string Name
{
get;
set;
}
public DateTime LastDownload
{
get;
set;
}
public string URL
{
get;
set; // TODO see if it's a valid url
}
public string SavePath
{
get;
set;
}
#endregion
/// <summary>
/// checks for a new episode
/// </summary>
public void CheckForNewEpisode()
{
var downloadAll = LastDownload == DateTime.MinValue;
// Read the rss
var webClient = new WebClient();
webClient.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
// fetch feed as string
var content = webClient.OpenRead(URL);
if (content == null) return;
var contentReader = new StreamReader(content);
var rssFeedAsString = contentReader.ReadToEnd();
// convert feed to XML using LINQ to XML and finally create new XmlReader object
var feed = SyndicationFeed.Load(XDocument.Parse(rssFeedAsString).CreateReader());
if (feed == null) return;
List<SyndicationItem> downloadList;
// Reduce the set of downloads only to the desired
if (downloadAll)
{
downloadList = feed.Items.ToList();
}
else
{
var dList = from item in feed.Items
where item.PublishDate < new DateTimeOffset(LastDownload)
select item;
downloadList = dList.ToList();
}
if (downloadList.Count > 0)
{
if (!Directory.Exists(SavePath))
Directory.CreateDirectory(SavePath);
}
// Download all that apply
foreach (var item in downloadList)
{
var links = from link in item.Links
select link.Uri.AbsoluteUri;
if (!links.Any()) return;
var name = item.Id.Substring(0, item.Id.Length - 1).Split('/').Last() + ".torrent";
Console.WriteLine(name);
DownloadFile(links.ToArray(), SavePath + name);
if (LastDownload < item.PublishDate.DateTime)
LastDownload = item.PublishDate.DateTime;
}
//Console.WriteLine("");
}
private void DownloadFile(string[] links, string filename)
{
var webClient = new WebClient();
webClient.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
foreach (var url in links)
{
try
{
webClient.DownloadFile(new Uri(url), filename);
return;
}
catch (WebException webException)
{
Console.WriteLine("Message: " + webException.Message + "\nStatus:" + webException.Status);
}
}
}
#region File IO
public static Show LoadXml(string file)
{
var show = new Show()
{
FileName = file
};
try
{
var doc = XDocument.Load(file);
// iterate through the XMLDocument
foreach (var item in doc.Root.Descendants())
{
switch (item.Name.ToString())
{
case "Name":
show.Name = item.Value;
break;
case "URL":
show.URL = item.Value;
break;
case "frequency":
show.Frequency = FrequencyValues.weekly;
break;
case "LastDownload":
show.LastDownload = DateTime.Parse(item.Value);
break;
default:
break;
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
return show;
}
public void SaveToXML()
{
var file = new XDocument(
new XElement("Show",
new XElement("Name", Name),
new XElement("URL", URL),
new XElement("LastDownload", LastDownload)
));
file.Save(FileName);
}
#endregion
}
/// <summary>
/// Possible frequency values for checking new episodes
/// </summary>
public enum FrequencyValues
{
weekly,
monthly,
random
}
}