/
DownloadFilesActivity.cs
109 lines (86 loc) · 3.26 KB
/
DownloadFilesActivity.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
using AnimeDrive.ActivityTasks;
using AnimeDrive.Irc;
using AnimeDrive.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace AnimeDrive
{
public class DownloadFilesActivity
{
private AnimeFileDatabase db;
private ProgramSettings ps;
GetFilteredPacksActivityTask gfpat;
DccDownloadActivityTask ddat;
private IrcClient ircClient;
private DCCClient dccClient;
private const string rizon = @"irc.rizon.net";
private const string hsChannel = @"#horriblesubs";
private const string userName = @"maievBot";
public DownloadFilesActivity(AnimeFileDatabase db, ProgramSettings ps)
{
this.db = db;
this.ps = ps;
ddat = new DccDownloadActivityTask(db, ps);
gfpat = new GetFilteredPacksActivityTask(db, ps);
dccClient = new DCCClient();
dccClient.OnDccDebugMessage += dccDebug;
ircClient = new IrcClient();
ircClient.SetConnectionInformation(
rizon, "maievBot", hsChannel, dccClient, ps.settings.FolderInput);
//ircClient.OnMessageReceived += IrcClient_OnMessageReceived;
//ircClient.OnRawMessageReceived += IrcClient_OnRawMessageReceived;
var res = ircClient.Connect();
}
private void IrcClient_OnRawMessageReceived(object sender, IrcRawReceivedEventArgs e)
{
Console.WriteLine(sender.ToString());
Console.WriteLine(e.Message);
}
private void IrcClient_OnMessageReceived(object sender, IrcReceivedEventArgs e)
{
Console.WriteLine(sender.ToString());
Console.WriteLine(e.User + "::" + e.Message);
}
public void dccDebug(object Sender, DCCDebugMessageArgs args)
{
Console.WriteLine(args.Message);
}
public void Execute()
{
Console.WriteLine("Downloading Files...");
var packs = gfpat.Execute();
var limitPacks = packs.Take(5);
foreach (var pack in limitPacks)
{
ircClient.SendMessageToAll(pack.ToString());
Thread.Sleep(10000);
var down = ircClient.CheckIfDownloading();
while(ircClient.CheckIfDownloading())
{
Thread.Sleep(5000);
}
// Check filesize is correct before marking as done
FileInfo fi = new FileInfo(Path.Combine(ps.settings.FolderInput, pack.Filename));
double packSize = 0;
Double.TryParse(pack.Size, out packSize);
// make sure the file exists and only differs by 1 mb (rounding)
if (fi.Exists && Math.Abs(packSize - fi.Length/(1024*1024)) < 2)
{
db.files.TryAdd(fi.Name, new AnimeFile(fi.FullName));
db.FlushData();
}
else
{
Console.WriteLine("Could not download: " + pack.Filename);
}
}
ircClient.StopClient();
}
}
}