-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
129 lines (106 loc) · 2.89 KB
/
main.cpp
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
#include <kerbal/container/avl_ordered.hpp>
#include <kerbal/container/static_vector.hpp>
#include <kerbal/type_traits/integral_constant.hpp>
#include <cpr/cpr.h>
#include <fmt/format.h>
#include <nlohmann/json.hpp>
#include <sqlite3pp.h>
#include <iostream>
#include <filesystem>
struct BingImg
{
std::string startdate;
std::string url;
std::string copyright;
std::string copyrightlink;
std::string hsh;
};
struct BingImgExtract
{
using key_type = std::string;
const std::string & operator()(const BingImg & img) const noexcept
{
return img.startdate;
}
};
using MaxNum = kerbal::type_traits::integral_constant<std::size_t, 10>;
using BingImgSet = kerbal::container::avl_ordered<BingImg, BingImgExtract>;
BingImgSet getBingImgSet()
{
BingImgSet bingImgSet;
kerbal::container::static_vector<cpr::AsyncResponse, MaxNum::value> asrs;
for (size_t i = 0; i < asrs.max_size(); ++i) {
asrs.emplace_back(
cpr::GetAsync(
cpr::Url{"https://www.bing.com/HPImageArchive.aspx"},
cpr::Parameters{
{"format", "js"},
{"idx", std::to_string(i)},
{"n", "1"},
{"mkt", "en-Us"}
}));
}
for (auto & asr : asrs) {
cpr::Response r = asr.get();
nlohmann::json json;
try {
json = nlohmann::json::parse(r.text);
} catch (const nlohmann::detail::parse_error & e) {
std::cerr << "error occurred with receive str: " << r.text << std::endl;
continue;
}
const auto &img = json["images"][0];
bingImgSet.insert_unique(BingImg{
.startdate = img["startdate"],
.url = "http://www.bing.com/" + (std::string)img["url"],
.copyright = img["copyright"],
.copyrightlink = img["copyrightlink"],
.hsh = img["hsh"],
});
}
return bingImgSet;
}
int main()
{
using std::cout;
using std::endl;
std::filesystem::create_directories("./img");
BingImgSet bingImgSet = getBingImgSet();
cout << "download: " << bingImgSet.size() << " pictures" << endl;
cout << endl;
sqlite3pp::database db("bingimg.db");
db.execute(
"CREATE TABLE img("
" startdate TEXT UNIQUE,"
" copyright TEXT,"
" url TEXT,"
" search TEXT,"
" hsh TEXT"
")"
);
for (const auto &e : bingImgSet) {
cout << fmt::format(
"startdate: {}\n"
"copyright: {}\n"
"url: {}\n"
"search: {}\n"
"hsh: {}\n",
e.startdate,
e.copyright,
e.url,
e.copyrightlink,
e.hsh
);
auto r = cpr::Get(cpr::Url{e.url});
std::ofstream img(fmt::format("./img/{}.jpg", e.startdate), std::ios::out);
img << r.text << std::flush;
sqlite3pp::command insert_cmd(db, "INSERT INTO img (startdate, copyright, url, search, hsh) VALUES (?, ?, ?, ?, ?)");
insert_cmd.bind(1, e.startdate, sqlite3pp::nocopy);
insert_cmd.bind(2, e.copyright, sqlite3pp::nocopy);
insert_cmd.bind(3, e.url, sqlite3pp::nocopy);
insert_cmd.bind(4, e.copyrightlink, sqlite3pp::nocopy);
insert_cmd.bind(5, e.hsh, sqlite3pp::nocopy);
insert_cmd.execute();
cout << endl;
}
}