forked from CyberShadow/DFeed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mldownload.d
133 lines (118 loc) · 3.33 KB
/
mldownload.d
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
/* Copyright (C) 2011, 2012 Vladimir Panteleev <vladimir@thecybershadow.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
module mldownload;
import std.getopt;
import std.string;
import std.regex;
import ae.net.asockets;
import ae.net.http.client;
import ae.utils.gzip;
import ae.sys.data;
import ae.sys.log;
import ae.sys.timing;
import common;
import database;
import messagedb;
import rfc850;
bool update;
MessageDBSink sink;
MLDownloader downloader;
class MLDownloader : NewsSource
{
bool stopping;
this()
{
super("ML-Downloader");
}
override void start()
{
foreach (list; ["dmd-beta", "dmd-concurrency", "dmd-internals", "phobos", "d-runtime"])
downloadList(list);
}
override void stop()
{
stopping = true;
}
void downloadList(string list)
{
if (stopping) return;
httpGet("http://lists.puremagic.com/pipermail/" ~ list ~ "/",
(string html)
{
log("Got list index: " ~ list);
auto re = regex(`<A href="(\d+-\w+\.txt\.gz)">`);
foreach (line; splitLines(html))
{
auto m = match(line, re);
if (!m.empty)
downloadFile(list, m.captures[1]);
}
},
(string error)
{
log("Error downloading list " ~ list ~ ": " ~ error);
setTimeout({ downloadList(list); }, TickDuration.from!"seconds"(10));
});
}
void downloadFile(string list, string fn)
{
if (stopping) return;
auto url = "http://lists.puremagic.com/pipermail/" ~ list ~ "/" ~ fn;
httpGet(url,
(Data data)
{
scope(failure) std.file.write("errorfile", data.contents);
auto text = cast(string)(uncompress(data).contents).idup;
text = text[text.indexOf('\n')+1..$]; // skip first From line
auto fromline = regex("\n\nFrom .* at .* \\w\\w\\w \\w\\w\\w [\\d ]\\d \\d\\d:\\d\\d:\\d\\d \\d\\d\\d\\d\n");
foreach (msg; splitter(text, fromline))
{
msg = "List-ID: " ~ list ~ "\n" ~ msg;
scope(failure) std.file.write("errormsg", msg);
auto post = new Rfc850Post(msg);
foreach (int n; query("SELECT COUNT(*) FROM `Posts` WHERE `ID` = ?").iterate(post.id))
if (n == 0)
{
log("Found new post: " ~ post.id);
announcePost(post);
}
else if (update)
{
log("Updating post: " ~ post.id);
sink.updatePost(post);
}
}
},
(string error)
{
log("Error downloading file " ~ fn ~ " on list " ~ list ~ ": " ~ error);
setTimeout({ downloadFile(list, fn); }, TickDuration.from!"seconds"(10));
});
}
}
void main(string[] args)
{
getopt(args,
"q|quiet", &common.quiet,
"u|update", &update);
sink = new MessageDBSink();
downloader = new MLDownloader();
startNewsSources();
db.exec("BEGIN"); allowTransactions = false;
socketManager.loop();
downloader.log("Committing...");
db.exec("COMMIT");
}