forked from berthubert/tkconv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtkpull.cc
143 lines (125 loc) · 4.61 KB
/
tkpull.cc
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
#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"
using namespace std;
void storeDocument(const std::string& id, const std::string& content, const string& prefix)
{
string fname=makePathForId(id, prefix, "", true);
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");
int sizlim = 50000000;
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;
}
};
for(auto store : {&wantDocs, &wantVerslagen, &wantPhotos}) {
int present=0;
int toolarge=0, retrieved=0;
int error=0;
cout<<"Starting from a store, 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});
if(isPresentNonEmpty(get<string>(d["id"]), prefix))
fmt::print("Re-retrieving {}, has wrong size on disk\n", get<string>(d["id"]));
}
}
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;
}
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());
storeDocument(need.id, res->body, prefix);
retrieved++;
usleep(100000);
}
fmt::print("Retrieved {} documents, {} were too large, {} errors\n", retrieved, toolarge, error);
}
}