Skip to content

Instantly share code, notes, and snippets.

@r-lyeh-archived
Created September 27, 2015 11:23
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 r-lyeh-archived/43564c5f1b485d48532f to your computer and use it in GitHub Desktop.
Save r-lyeh-archived/43564c5f1b485d48532f to your computer and use it in GitHub Desktop.
http api
#pragma once
#include <json11/json11.hpp>
#include <flow/flow.hpp>
// @toadd https://placeimg.com/
// @toadd http://www.pexels.com/
#include <deque>
#include <string>
std::string apireq( const std::deque<std::string> &api, const std::deque<std::string> &jsonfd_ ) {
std::string req;
for( auto &it : api ) {
req += it;
}
auto json_search = flow::download( req );
if( !json_search.ok ) {
std::cerr << "request failed (" << json_search.code << "): " << json_search.url << std::endl;
return std::string();
} else {
std::cout << "fetching: " << json_search.url << std::endl;
}
std::string error;
json11::Json search = json11::Json::parse( json_search.data, error );
if( !error.empty() ) {
std::cerr << "parse failed: " << error << std::endl;
std::cerr << json_search.data << std::endl;
return std::string();
}
std::deque<std::string> jsonfd( jsonfd_ );
json11::Json member = search[ jsonfd.front() ];
jsonfd.pop_front();
for( auto &it : jsonfd ) {
int seekb = ( it == "@0" ), seekm = ( it == "@N/2" ), seeke = ( it == "@N" );
if( seekb || seekm || seeke ) {
if( member.is_object() ) {
auto items = member.object_items();
if( items.size() ) {
auto jt = items.begin();
std::advance(jt, seekb ? 0 : ( seekm ? items.size()/2 : items.size()-1 ));
member = jt->second;
continue;
}
}
if( member.is_array() ) {
auto items = member.array_items();
if( items.size() ) {
auto jt = items.begin();
std::advance(jt, seekb ? 0 : ( seekm ? items.size()/2 : items.size()-1 ));
member = *jt;
continue;
}
}
}
if( member[it].type() ) {
member = member[it];
} else {
member = member[atoi(it.c_str())];
}
}
return member.string_value();
}
std::string search_google( const std::string &input ) {
std::string api = "https://www.googleapis.com/customsearch/v1?";
std::string key = "&key=9a0e5d52inserthereyourgooglekey2a18b3blablad2190";
std::string cx = "&cx=016444283805151499214:y1if1rvyvyu"; //017576662512468239146:omuauf_lfve
std::string options = "&imgSize=medium&searchType=image&filter=1&safe=off";
auto url = apireq( {api,key,cx,options,"&q=",input}, {"items","0","link"} );
return url;
}
std::string search_google_images( const std::string &input ) {
std::string api = "https://ajax.googleapis.com/ajax/services/search/images?v=1.0";
std::string options = "&safe=off&v=1.0&userip=&rsz=8&imgsz=large";
auto url = apireq( {api,options,"&q=",input}, {"responseData","results","@0","unescapedUrl"} );
return url;
}
std::string search_pixabay( const std::string &input ) {
std::string api = "http://pixabay.com/api/?";
std::string key = "&username=r-lyeh&key=9a0e5d52inserthereyourpixabaykey2a18b3blablad2190";
std::string options = "&image_type=photo";
auto url = apireq( {api,key,options,"&search_term=",input}, {"hits","0","webformatURL"} );
return url;
}
std::string search_flickr( const std::string &input ) {
std::string api = "https://api.flickr.com/services/rest/?format=json&nojsoncallback=1";
std::string method_search = "&method=flickr.photos.search";
std::string method_size = "&method=flickr.photos.getSizes";
std::string key = "&api_key=9a0e5d52inserthereyourflickrkey2a18b3blablad2190";
auto photo_id = apireq( {api,key,method_search,"&sort=relevance&text=",input}, {"photos","photo","0","id"} );
auto url = apireq( {api,key,method_size,"&photo_id=",photo_id}, {"sizes","size","@N/2","source"} );
return url;
}
std::string search_wikimedia( const std::string &input ) {
std::string api = "http://en.wikipedia.org/w/api.php?";
std::string options = "&action=query&format=json";
std::string images = "&prop=pageimages";
std::string imageinfo = "&prop=imageinfo&iiprop=url";
std::string cmd = "&titles=";
// search wikipedia contents
auto title = apireq( {api,options,"&list=search&srprop=timestamp","&srsearch=",input}, {"query","search","0","title"} );
auto file = apireq( {api,options,images,cmd,title}, {"query","pages","@0","pageimage"} );
auto url = apireq( {api,options,imageinfo,cmd,"Image:"+file}, {"query","pages","@0","imageinfo","0","url"} );
return url;
}
#include <map>
#include <functional>
std::map< std::string, std::function<std::string(const std::string &)> > get_providers() {
return {
{"google", search_google},
{"googlei", search_google_images},
{"pixabay", search_pixabay},
{"flickr", search_flickr},
{"wikimedia", search_wikimedia }
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment