aboutsummaryrefslogtreecommitdiff
path: root/GithubAPI.cpp
blob: 9f52e868e1060aa1385f2f74f83ab4e944f6b4bb (plain)
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
#include "GithubAPI.hpp"
#include "json.hpp"

#include <curl/curl.h>
#include <fstream>
#include <iostream>

GithubAPI::GithubAPI(std::string username)
    : username(username), avatar_url(), avatar_buffer() {
  curl_global_init(CURL_GLOBAL_ALL);
  curl = curl_easy_init();
}

GithubAPI::~GithubAPI() {
  curl_easy_cleanup(curl);
  curl_global_cleanup();
}

static size_t WriteToStringCallback(void *contents, size_t size, size_t nmemb,
                                    void *userp) {
  std::string *const out = (std::string *)userp;

  out->append((char *)contents, size * nmemb);
  return size * nmemb;
}

static size_t WriteToBufferCallback(void *contents, size_t size, size_t nmemb,
                                    void *userp) {
  std::vector<unsigned char> *const out = (std::vector<unsigned char> *)userp;

  out->insert(out->end(), (unsigned char *)contents,
              (unsigned char *)contents + (size * nmemb));
  return size * nmemb;
}

bool GithubAPI::DownloadAvatar() {
  if (GetAvatarURL() == false) {
    return false;
  }

  return GetAvatarImage();
}

size_t GithubAPI::GetAvatarBuffer(std::vector<unsigned char> &avatar_buffer) {
  avatar_buffer = this->avatar_buffer;
  return avatar_buffer.size();
}

bool GithubAPI::AvatarToFile(std::string filename) {
  std::vector<unsigned char> avatar_buffer;

  if (GetAvatarBuffer(avatar_buffer) == 0) {
    return false;
  }

  {
    std::ofstream avout(filename, std::ios::out | std::ios::binary);
    avout.write((char const *)avatar_buffer.data(), avatar_buffer.size());
    avout.close();
  }

  return true;
}

bool GithubAPI::ResetCURL() {
  if (curl == nullptr) {
    return false;
  }

  curl_easy_reset(curl);
  return true;
}

bool GithubAPI::GetAvatarURL() {
  bool ret = true;
  CURLcode res;
  struct curl_slist *headers = NULL;
  std::string json_str;
  nlohmann::json json;

  std::string gURL = "https://api.github.com/users/" + username;

  if (ResetCURL() == false) {
    ret = false;
    goto failure;
  }

  headers = curl_slist_append(headers, "Accept: application/json");
  headers = curl_slist_append(headers, "charset: utf-8");

  curl_easy_setopt(curl, CURLOPT_URL, gURL.c_str());
  curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET");
  curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
  curl_easy_setopt(curl, CURLOPT_USERAGENT, "github-avatar-ukrainify/0.1");

  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteToStringCallback);
  curl_easy_setopt(curl, CURLOPT_WRITEDATA, &json_str);

  res = curl_easy_perform(curl);
  if (res != CURLE_OK) {
    std::cerr << "libcurl returned: " << curl_easy_strerror(res) << std::endl;
    ret = false;
    goto failure;
  }

  json = nlohmann::json::parse(json_str);

  if (json["login"] != username) {
    std::cerr << "Username mismatch: " << json["login"] << " != " << username
              << std::endl;
  }

  avatar_url = json["avatar_url"];
failure:
  curl_slist_free_all(headers);

  return ret;
}

bool GithubAPI::GetAvatarImage() {
  bool ret = true;
  CURLcode res;
  struct curl_slist *headers = NULL;

  if (ResetCURL() == false) {
    ret = false;
    goto failure;
  }

  avatar_buffer.clear();
  headers = curl_slist_append(headers, "Accept: image/*");

  curl_easy_setopt(curl, CURLOPT_URL, avatar_url.c_str());
  curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET");
  curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
  curl_easy_setopt(curl, CURLOPT_USERAGENT, "github-avatar-ukrainify/0.1");

  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteToBufferCallback);
  curl_easy_setopt(curl, CURLOPT_WRITEDATA, &avatar_buffer);

  res = curl_easy_perform(curl);
  if (res != CURLE_OK) {
    std::cerr << "libcurl returned: " << curl_easy_strerror(res) << std::endl;
    ret = false;
    goto failure;
  }

failure:
  curl_slist_free_all(headers);

  return ret;
}