Skip to content

Instantly share code, notes, and snippets.

@yasuharu519
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yasuharu519/45a517e1a0f03e68c8a0 to your computer and use it in GitHub Desktop.
Save yasuharu519/45a517e1a0f03e68c8a0 to your computer and use it in GitHub Desktop.
Instagramの猫画像クローラ crawl_instagram.cpp
#include <fstream>
#include <iostream>
#include <mutex>
#include <string>
#include <thread>
#include <restclient-cpp/restclient.h>
#include <json11.hpp>
static std::string client_id = "クライアントID";
static std::string endpoint = "https://api.instagram.com/v1/tags/sheep/media/recent?client_id=" + client_id;
std::mutex mux;
static inline void synched_cout(const std::string& message)
{
std::lock_guard<std::mutex> lock{mux};
std::cout << message << std::endl << std::flush;
}
void crawl_func(const std::string& url, const std::string& id)
{
synched_cout("Download start: " + url);
// download file and save
auto raw_data = RestClient::get(url);
std::ofstream out(id + ".jpg");
out << raw_data.body;
out.close();
synched_cout("Download done: " + url);
}
int main()
{
auto res = RestClient::get(endpoint);
std::string err;
auto json = json11::Json::parse(res.body, err);
if(!err.empty())
{
std::cout << err << std::endl;
return 1;
}
std::vector<std::thread> threads;
auto data = json["data"].array_items();
for(auto& item : data)
{
auto id = item["id"].string_value();
auto url = item["images"]["standard_resolution"]["url"].string_value();
threads.emplace_back(crawl_func, url, id);
}
for(auto& th : threads)
{
th.join();
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment