-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathtkpull.cc
More file actions
179 lines (155 loc) · 5.52 KB
/
tkpull.cc
File metadata and controls
179 lines (155 loc) · 5.52 KB
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
#include <fmt/format.h>
#include <fmt/printf.h>
#include <fmt/ranges.h>
#include <mutex>
#include <iostream>
#include "sqlwriter.hh"
#include <atomic>
#include "httplib.h"
#include <set>
#include "support.hh"
#include "meta.hh"
using namespace std;
void ifExistsThenRename(const std::string& fname)
{
struct stat sb;
if(stat(fname.c_str(), &sb) < 0)
return;
string newname = fmt::sprintf("%s.%d", fname, sb.st_mtime);
if(rename(fname.c_str(), newname.c_str()) == 0) {
fmt::print("Already had a file for {}, renamed to {}\n",
fname, newname);
}
}
void storeDocument(const std::string& id, const std::string& content, const string& prefix)
{
string fname=makePathForId(id, prefix, "", true);
ifExistsThenRename(fname);
FILE* t = fopen((fname+".tmp").c_str(), "w");
if(!t)
throw runtime_error("Unable to open file "+fname+": "+string(strerror(errno)));
shared_ptr<FILE> fp(t, fclose);
if(fwrite(content.c_str(), 1, content.size(), fp.get()) != content.size()) {
unlink(fname.c_str());
throw runtime_error("Partial write to file "+fname);
}
fp.reset();
if(rename((fname+".tmp").c_str(), fname.c_str()) < 0) {
int e = errno;
unlink((fname+".tmp").c_str());
throw runtime_error("Unable to rename saved file "+fname+".tmp - "+strerror(e));
}
}
int main(int argc, char** argv)
{
SQLiteWriter sqlw("tk.sqlite3", SQLWFlag::ReadOnly);
int sizlim = 250000000;
string limit="2007-01-01";
auto wantDocs = sqlw.queryT("select id,enclosure,contentLength from Document where datum > ? and contentLength < ?", {limit, sizlim});
auto alleVerslagen = sqlw.queryT("select Verslag.id as id, vergadering.id as vergaderingid,enclosure,contentLength,datum from Verslag,Vergadering where Verslag.vergaderingId=Vergadering.id and datum > ? order by datum desc, verslag.updated desc", {limit});
auto wantPhotos = sqlw.queryT("select Persoon.id as id, enclosure, contentLength from Persoon where contentLength > 0");
set<string> seenvergadering;
decltype(alleVerslagen) wantVerslagen, todelete;
for(auto& v: alleVerslagen) {
string vid = get<string>(v["vergaderingid"]);
if(seenvergadering.count(vid)) {
todelete.push_back(v);
continue;
}
wantVerslagen.push_back(v);
seenvergadering.insert(vid);
}
fmt::print("We desire {} documents and {} photos and {} verslagen, and found {} older verslagen\n", wantDocs.size(), wantPhotos.size(), wantVerslagen.size(), todelete.size());
int unlinked=0;
for(auto& td : todelete) {
string verslagid=get<string>(td["id"]);
string fname=makePathForId(verslagid);
int rc = unlink(fname.c_str());
if(!rc)
unlinked++;
else {
if(errno != ENOENT)
fmt::print("Error removing file {}: {}\n", fname, strerror(errno));
}
}
fmt::print("{} niet-nieuwste versies van verslagen gewist\n", unlinked);
struct RetStore
{
string id;
string enclosure;
int64_t contentLength;
bool operator<(const RetStore& rhs) const
{
return id < rhs.id;
}
};
ThrottleDB tdb;
map<string, decltype(&wantDocs)> work = {
{"docs", &wantDocs},
{"verslagen", &wantVerslagen},
{"photos", &wantPhotos}};
for(auto& [name, store] : work) {
int present=0;
int toolarge=0, retrieved=0;
int error=0;
cout<<"Starting from store '"<<name<<"', got "<<store->size()<<" docs to go"<<endl;
set<RetStore> toRetrieve;
string prefix = (store == &wantPhotos) ? "photos" : "docs";
for(auto& d : *store) {
if(isPresentRightSize(get<string>(d["id"]), get<int64_t>(d["contentLength"]), prefix) )
present++;
else {
auto contentLength = get_if<int64_t>(&d["contentLength"]);
toRetrieve.insert({get<string>(d["id"]), get<string>(d["enclosure"]),
contentLength ? *contentLength : 0});
size_t siz;
if(isPresentNonEmpty(get<string>(d["id"]), prefix, "", &siz)) {
fmt::print("Re-retrieving {} {}, has wrong size on disk {}, should be {}\n",
name, get<string>(d["id"]), siz, get<int64_t>(d["contentLength"]));
}
}
}
fmt::print("We have {} files to retrieve, {} are already present\n", toRetrieve.size(), present);
for(const auto& need : toRetrieve) {
if(!need.contentLength || need.contentLength > sizlim) {
toolarge++;
fmt::print("Skipping {}, too large for server ({}) or unknown\n",
need.id, need.contentLength);
continue;
}
if(tdb.shouldThrottle(need.enclosure, 86400, 2)) {
fmt::print("Not retrieving {}, throttled\n", need.enclosure);
continue;
}
httplib::Client cli("https://gegevensmagazijn.tweedekamer.nl");
cli.set_connection_timeout(10, 0);
cli.set_read_timeout(10, 0);
cli.set_write_timeout(10, 0);
fmt::print("Retrieving from {} (expect {} bytes).. ", need.enclosure, need.contentLength);
cout.flush();
auto res = cli.Get(need.enclosure);
if(!res) {
auto err = res.error();
fmt::print("Oops retrieving from {} -> {}\n", need.enclosure, httplib::to_string(err));
error++;
continue;
}
if(res->status != 200) {
fmt::print("Wrong status code {} for url {}, not storing\n",
res->status, need.enclosure);
error++;
continue;
}
fmt::print("Got {} bytes\n", res->body.size());
if(res->body.size() == (unsigned int)need.contentLength) {
storeDocument(need.id, res->body, prefix);
retrieved++;
}
else {
tdb.report(need.enclosure, "wrong size");
fmt::print("Unexpected size received, not storing document\n");
}
}
fmt::print("Retrieved {} documents, {} were too large, {} errors\n", retrieved, toolarge, error);
}
}