-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.cpp
121 lines (106 loc) · 5.29 KB
/
example.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
#include "nekos-best++.hpp"
#include <fstream>
#include <stdio.h>
int
main (const int argc, const char *argv[])
{ // nekos_best::fetch
printf ("%s\n", nekos_best::get_base_url ().c_str ());
// as we call get_available_endpoints() here thus caching the endpoints
// data from the API, we don't need to initialize the client anymore.
const std::map<std::string, nekos_best::EndpointSpec> res
= nekos_best::get_available_endpoints ();
for (const auto &d : res)
{
printf ("KEY: \"%s\"\n", d.first.c_str ());
printf ("FORMAT: \"%s\"\n",
nekos_best::get_str_format (d.second.format)
.c_str ());
printf ("MAX: \"%s\"\n", d.second.max.c_str ());
printf ("MIN: \"%s\"\n", d.second.min.c_str ());
printf ("NAME: \"%s\"\n", d.second.name.c_str ());
printf ("\n");
}
const auto res2 = nekos_best::fetch ("neko", 50);
for (const auto &r : res2.results)
{
printf ("artist_href: \"%s\"\n",
r.artist_href.c_str ());
printf ("artist_name: \"%s\"\n",
r.artist_name.c_str ());
printf ("source_url: \"%s\"\n", r.source_url.c_str ());
printf ("url: \"%s\"\n", r.url.c_str ());
printf ("anime_name: \"%s\"\n", r.anime_name.c_str ());
printf ("\n");
}
const auto res3 = nekos_best::fetch ("hug", 50);
for (const auto &r : res3.results)
{
printf ("artist_href: \"%s\"\n",
r.artist_href.c_str ());
printf ("artist_name: \"%s\"\n",
r.artist_name.c_str ());
printf ("source_url: \"%s\"\n", r.source_url.c_str ());
printf ("url: \"%s\"\n", r.url.c_str ());
printf ("anime_name: \"%s\"\n", r.anime_name.c_str ());
printf ("\n");
}
{
nekos_best::Meta meta = nekos_best::download (
"https://nekos.best/api/v2/hug/"
"2363175c-4cda-414e-8929-a87f53fc1cc1.gif");
printf ("artist_href: \"%s\"\n", meta.artist_href.c_str ());
printf ("artist_name: \"%s\"\n", meta.artist_name.c_str ());
printf ("source_url: \"%s\"\n", meta.source_url.c_str ());
printf ("url: \"%s\"\n", meta.url.c_str ());
printf ("anime_name: \"%s\"\n", meta.anime_name.c_str ());
printf ("\n");
}
{
nekos_best::Meta meta = nekos_best::download (
"https://nekos.best/api/v2/hug/"
"b1136a84-2653-4c87-b80f-ea3cc0bf6ab9.gif");
printf ("artist_href: \"%s\"\n", meta.artist_href.c_str ());
printf ("artist_name: \"%s\"\n", meta.artist_name.c_str ());
printf ("source_url: \"%s\"\n", meta.source_url.c_str ());
printf ("url: \"%s\"\n", meta.url.c_str ());
printf ("anime_name: \"%s\"\n", meta.anime_name.c_str ());
printf ("\n");
}
{
// download
std::ostringstream download_stream;
nekos_best::Meta meta = nekos_best::download (
"https://nekos.best/api/v2/hug/"
"548daa04-3dff-4524-bb28-c229e5542e9f.gif",
&download_stream);
std::ofstream file;
// no image info from the API, we can just name it this for now
// or better specify endpoint you know which gives which
// and have a proper filename with an extension
file.open ("image", std::ios::out);
if (!file.is_open ())
{
fprintf (stderr, "[download ERROR] Can't open "
"stream for writing\n");
}
else
{
// get header size
const auto response
= nekos_best::get_last_request_response ();
// strip header from actual image
// data
file << download_stream.str ().substr (
response.header_size);
file.close ();
printf ("Image saved to \'image\'\n");
}
printf ("artist_href: \"%s\"\n", meta.artist_href.c_str ());
printf ("artist_name: \"%s\"\n", meta.artist_name.c_str ());
printf ("source_url: \"%s\"\n", meta.source_url.c_str ());
printf ("url: \"%s\"\n", meta.url.c_str ());
printf ("anime_name: \"%s\"\n", meta.anime_name.c_str ());
printf ("\n");
}
return 0;
}