Posts Tagged ‘ c++ ’

A simple Command line API for Twitter update

After having wasted almost half of my vacations, one night I though of developing a simple command line API for Twitter, though there are thousands of applications available for tweeting, I developed it to only get more knowledge of libcurl .

/*
AUTHOR: Mohit Kothari
It's a simple Command Line API for updating your status

USAGE:
$./twit
Enter Username: abc
Enter Password(Max 100):
Enter the message: add

RETURN:
It will return a blob of JSON information
*/

#include<iostream>
#include<curl/curl.h>
#include<string>
#include<cstring>
#include<curses.h>
using namespace std;

void input_user(string &user){
	cout<<"Enter Username: ";
	cin>>user;
	return;
}

void input_pass(string &pass){
	char ch[100];
	initscr();
	printw("Enter Password(Max 100): ");
	noecho();
	getstr(ch);
	pass = ch;
	echo();
	endwin();
}
int main(int argc, char* argv[]){
	string msg,user,pass,final="";
	string status = "";
	char *fina, *sta;

	input_user(user);
	input_pass(pass);

	cout<<"Enter the message: ";
	getline(cin,msg);
	getline(cin,msg);
	if(msg.size()>140){
		msg.erase(140);
	}

	status += "status="+msg;
	final += "http://"+user+":"+pass+"@twitter.com/statuses/update.json";

	fina = new char[final.size()+1];
	sta=new char[status.size()+1];
	strcpy(fina,final.c_str());
	strcpy(sta,status.c_str());
	CURL *curl;
	CURLcode res;
	curl = curl_easy_init();

	if(curl){
		curl_easy_setopt(curl,CURLOPT_URL,fina);
		curl_easy_setopt(curl,CURLOPT_POSTFIELDS,sta);
		res=curl_easy_perform(curl);
		curl_easy_cleanup(curl);
	}

	return 0;
}

I have shared the Source in git-hub: http://github.com/zarthon/git_twit

It just updates your status, it doesn’t have the function to get recent tweets, I am trying to implement it
and also is looking forward to build a GUI using Qt.
Lets hope it turns out good.

Advertisements