Created
December 29, 2016 11:23
-
-
Save dotxnc/99231c10cb1c6019f1c3ac657ba2181f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Created by Lumaio on 12/28/2016. | |
// C API for pushbullet. | |
// Requires libcURL | |
// | |
#ifndef PUSHBULLET_H | |
#define PUSHBULLET_H | |
#include <stdio.h> | |
#include <curl/curl.h> | |
static char* apikey; | |
void init_pushbullet(const char* key) | |
{ | |
apikey = const_cast<char*>(key); | |
} | |
size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp) { return size * nmemb; } | |
void push_pushbullet(const char* data) | |
{ | |
CURL* api; | |
CURLcode response; | |
char token[128]; | |
sprintf(token, "Access-Token: %s", apikey); | |
struct curl_slist* headers = NULL; | |
headers = curl_slist_append(headers, token); | |
headers = curl_slist_append(headers, "Content-Type: application/json"); | |
api = curl_easy_init(); | |
std::string s; | |
curl_easy_setopt(api, CURLOPT_URL, "https://api.pushbullet.com/v2/pushes"); | |
curl_easy_setopt(api, CURLOPT_NOPROGRESS, 1L); | |
curl_easy_setopt(api, CURLOPT_POSTFIELDS, data); | |
curl_easy_setopt(api, CURLOPT_POSTFIELDSIZE_LARGE, (curl_off_t)-1); | |
curl_easy_setopt(api, CURLOPT_USERAGENT, "curl biiiiiiiitch"); | |
curl_easy_setopt(api, CURLOPT_SSL_VERIFYHOST, 1L); | |
curl_easy_setopt(api, CURLOPT_MAXREDIRS, 50L); | |
curl_easy_setopt(api, CURLOPT_CUSTOMREQUEST, "POST"); | |
curl_easy_setopt(api, CURLOPT_HTTPHEADER, headers); | |
curl_easy_setopt(api, CURLOPT_SSL_VERIFYPEER, 0); | |
curl_easy_setopt(api, CURLOPT_WRITEFUNCTION, write_data); | |
response = curl_easy_perform(api); | |
curl_easy_cleanup(api); | |
} | |
void note_pushbullet(const char* title, const char* body) | |
{ | |
char data[256]; | |
sprintf(data, "{\"type\":\"note\",\"title\":\"%s\",\"body\":\"%s\"}", title, body); | |
push_pushbullet(data); | |
} | |
void link_pushbullet(const char* title, const char* body, const char* link) | |
{ | |
char data[256]; | |
sprintf(data, "{\"type\":\"link\",\"title\":\"%s\",\"body\":\"%s\",\"url\":\"%s\"}", title, body, link); | |
push_pushbullet(data); | |
} | |
#endif //PUSHBULLET_H |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment