-
Notifications
You must be signed in to change notification settings - Fork 0
/
Processor.cs
86 lines (75 loc) · 2.96 KB
/
Processor.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
using System;
using System.Collections.Concurrent;
using System.IO;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace GoComicsToGo
{
internal static class Processor
{
internal static void Process(string name, DateTime min, DateTime max)
{
Directory.CreateDirectory(Path.Combine(Directory.GetCurrentDirectory(), name));
ConcurrentBag<DateTime> dates = new ConcurrentBag<DateTime>(Enumerable.Range(0, 1 + max.Subtract(min).Days)
.Select(x => min.AddDays(x)));
Parallel.ForEach(dates, x => ProcessItem(name, x));
}
private static void ProcessItem(string name, DateTime date)
{
try
{
string webpageURL = GetWebpageURL(name, date);
string webpageHTML = GetWebpageHTML(webpageURL);
string stripURL = GetStripURL(webpageHTML);
SaveStripSuccess(stripURL, name, date);
Messenger.ProcessedItem(date, true);
}
catch
{
SaveStripFailure(name, date);
Messenger.ProcessedItem(date, false);
}
}
private static string GetWebpageURL(string name, DateTime date)
{
return string.Format("http://www.gocomics.com/{0}/{3}/{2}/{1}",
name,
date.Day.ToString().PadLeft(2, '0'),
date.Month.ToString().PadLeft(2, '0'),
date.Year.ToString().PadLeft(4, '0'));
}
private static string GetWebpageHTML(string webpageURL)
{
using (WebClient webClient = new WebClient())
{
return webClient.DownloadString(webpageURL);
}
}
private static string GetStripURL(string webpageHTML)
{
Regex regex = new Regex(@"<meta property=""og:image"" content=""(.*)"" \/>");
Match match = regex.Match(webpageHTML);
if (match.Groups.Count == 2 && match.Groups[0].Success && match.Groups[1].Success)
{
return match.Groups[1].Value;
}
else
{
return null;
}
}
private static void SaveStripSuccess(string stripURL, string name, DateTime date)
{
using (WebClient webClient = new WebClient())
{
webClient.DownloadFile(stripURL, Path.Combine(Directory.GetCurrentDirectory(), name, string.Concat(date.ToString(Constants.DATE_MASK), ".gif")));
}
}
private static void SaveStripFailure(string name, DateTime date)
{
File.Create(Path.Combine(Directory.GetCurrentDirectory(), name, string.Concat(date.ToString(Constants.DATE_MASK), ".error")));
}
}
}