-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
121 lines (95 loc) · 3.74 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
// The great GIFs of @beesandbombs made me do this
// https://twitter.com/beesandbombs
#include <iostream>
#include <fstream>
#include <filesystem>
#include <cpr/cpr.h>
#include <json.hpp>
// story of this namespace: this is a hobby project of @nlohmann
// https://github.com/nlohmann/json/issues/9
using json = nlohmann::json;
namespace fs = std::filesystem;
int main(int argc, char *argv[])
{
// thanks to short-circuit evaluation this works, but i'm going to
// use a c++ command line option parser library for next projects
if (argc != 2 or std::string(argv[1]) == "--help" or std::string(argv[1]) == "-h")
{
std::cout << "Downloads all gists of a given GitHub user\n"
<< "Example usage: gist-downloader beesandbombs\n";
return 0;
}
else
{
json j;
int page = 1;
bool firstRun = true;
do
{
// https://developer.github.com/v3/gists/
// https://developer.github.com/v3/#pagination
std::string gistsAddress = std::string("https://api.github.com/users/") +
argv[1] +
"/gists?page=" +
std::to_string(page) +
"&per_page=100";
// https://whoshuu.github.io/cpr/
auto r = cpr::Get(cpr::Url{gistsAddress},
cpr::Header{{"Content-Type", "application/json"}},
cpr::Header{{"User-Agent", "maidis"}});
// https://nlohmann.github.io/json/
j = json::parse(r.text);
// ex: torvalds
if(firstRun and j.empty())
{
std::cout << "User has no gist\n";
return 0;
}
auto checkErrors = j.find("message");
//std::cout << *checkErrors << '\n';
// ex: --hel
if(firstRun and checkErrors != j.end())
{
std::cout << "There is no such user at GitHub\n"
<< "or something else went wrong.\n";
return 0;
}
firstRun = false;
for (auto& element : j)
{
json hede = element["files"];
for (auto& element2 : hede)
{
auto filename = element2.find("filename");
//std::cout << *filename << '\n';
std::string filename2 = *filename;
auto id = element.find("id");
//std::cout << *id << '\n';
std::string id2 = *id;
std::string theName = id2 + "-" + filename2;
if(!fs::exists(theName))
{
auto raw_url = element2.find("raw_url");
//std::cout << *raw_url << '\n';
std::string gistAddress= *raw_url;
auto r2 = cpr::Get(cpr::Url{gistAddress},
cpr::Header{{"Content-Type", "application/json"}},
cpr::Header{{"User-Agent", "maidis"}});
std::ofstream gistFile;
gistFile.open(theName);
gistFile << r2.text;
gistFile.close();
std::cout << theName << "\tsaved ✔️\n";
}
else
{
std::cout << theName << "\talready exists 🆗\n";
}
}
}
++page;
}while(!j.empty());
}
std::cout << "Happy Hacking 🐱💻\n";
return 0;
}