Rayhan’s blog (raynux.com)

Rayhan’s Personal Web Blog Site

Entries Comments


Update your social networking status from command line

28 February, 2009 (13:27) | Linux, programming, Python, web, Web Services

Tags: , , ,


This is my first python script, Throughout the learning process I want to make something use full and I know that the best way to learn a new programming language is to start with some real project.

Updating your social networking status is now a frequent job. To make this job easier there are lots of web services, desktop applications, mobile applications, plug-ins available to the users. Some people wants to update there status from a single place. Like others Ping.fm, Hellotxt.com are providing single place service with a variety of options. But I think that updating status from command line is the easiest way for most of the Linux users who loves their terminal.

I have used ping.fm REST API for my script as a result It can update all the services registered with ping.fm at once or a single service putting additional parameter. You just need the Internet connection and terminal to put the command.

File: ~/bin/raypm

#! /usr/bin/env python
"""
A Simple program to update your social networking status using ping.fm

This program will update all your social networking sites via ping.fm.

requirements
	- sign for a account at ping.fm
	- setup ping.fm for desired social networking sites.
	- get your developer api key http://www.ping.fm/developers
	- get your user key from http://www.ping.fm/key

@example	
	- shell > raypm "your message text to update your all social networking status, like twitter, facebook, linkedin"
	- shell > raypm twitter "your message text to update twitter status only"
	- shell > raypm facebook "your message text to update facebook status only"

@author		: Md. Rayhan Chowdhury
@email		: ray@raynux.com
@website	: www.raynux.com
@license	: GPL
@copy		: All right are reserved
"""

import urllib
import urllib2
import sys
from xml.etree import ElementTree as ET

# modify __apiKey__ with your own
# get your apiKey from http://www.ping.fm/developers
__apiKey__ = 'YOUR DEVELOPER API KEY HERE' 

# modify __userKey__ with your own user key
# get your user key from http://www.ping.fm/key
__UserKey__ = "YOUR USER APP KEY HERE"

__apiUrl__ = 'http://api.ping.fm/v1/'

# debug
# 1 - will not update your status, dump xml response text
# 0 - will update your status
debug = 0

class RayStatus:


	def __init__(self, msg = None, service = None):
		"create message"    

		global debug
		
		data = {	'api_key'		: __apiKey__,
					'user_app_key'	: __UserKey__,
					'post_method'	: 'default',
					'body'			: msg,
					'debug'			: debug 
                  }  
		if service is not None:
			data['service'] = service		
		
		#Encode the data to be posted 
		data = urllib.urlencode(data)	

		print "Loading..."
		# post the data
		req = urllib2.Request(__apiUrl__ + 'user.post', data)		
		try:		
			response = urllib2.urlopen(req)

			responseXML = response.read()
		
			# parse response data
			result = ET.XML(responseXML)
			if result.attrib is not None:
				if result.attrib['status'] == 'OK':
					print 'Congratulations! your message posted successfully!';
				else:
					print 'Error: ' + result[2].text
			
			if debug:	
				print 		
				print "debug info: "
				print responseXML
		
		except urllib2.URLError, e:
			if hasattr(e, 'reason'):
				print 'Could not connect to the server.'
				print 'Reason: ', e.reason
			elif hasattr(e, 'code'):
				print 'The server couldn\'t fulfill the request.'
				print 'Error code: ', e.code
		else:
			print

if __name__ == '__main__':
	try:
		if (len(sys.argv) == 2):
			RayStatus(sys.argv[1], None)
		elif (len(sys.argv) == 3):
			RayStatus(sys.argv[2], sys.argv[1])
	except IndexError:
		print "Please enter your desired message to be posted."

To make the script working, you need to sign up for a ping.fm user account, and registrar for services you want, they support all major social networking sites including twitter, facebook, linkedIn.

You will need to get these two keys.
- developer api_keys from http://ping.fm/developers/
- user app key from http://ping.fm/key/

I have my developer api key from ping.fm which will work for you, but I can’t disclose it openly. It will be great to have your own api key and user key is unique for each user.

Installation:

  1. create a directory inside your home folder named bin
  2. create a file named raymp inside the bin folder and copy and paste the code
  3. replace api key and user key with your own.
  4. make the file executable with this command
    # chmod +x ~/bin/raypm
  5. you may need to restart the system to add the newly added bin folder to PATH.

& done.

now you can update your status with the following commands
# raypm "update all social networking status at once"
or
# raypm twitter "update twitter status only"
or
# raypm facebook "update facebook status only"

«

  »

Comments

Comment from kleinski
Time: March 11, 2009, 10:12 pm

Hey Rayhan,

I am sorry, but I think that I need your application key, since they say:
“Note: API keys are meant for application developers. If you are using a Ping.fm app that someone has created, it shouldn’t require you submit an API key. Since we’ve recently changed this process, some of the apps might not have been updated to meet new requirements.

If you are using an app that requires an API key, contact the developer, or visit the app’s website for an up-to-date version. ”

Can’t you put it as a DL beside the program?

Best, marcus

Comment from Md. Rayhan Chowdhury
Time: March 11, 2009, 11:23 pm

@Marcus,

I understand your point. I shouldn’t ask the user to get their own APP Key, but the problem is If I disclose the APP key, someone may misuse it for other purpose as the source code is open and may violate fing.fm terms of use. I am thinking to encode the source code.

Thanks for your suggestion

Pingback from Topics about Table-tennis » Update your social networking status from command line | Rayhan's …
Time: March 22, 2009, 2:54 am

[...] rayhan placed an interesting blog post on Update your social networking status from command line | Rayhan's …Here’s a brief overviewYou just need the Internet connection and terminal to put the command. File: ~/bin/raypm #! /usr/bin/env python “”” A Simple program to update your social networking status using ping.fm This program will update all your social … [...]

Comment from Barbara
Time: April 3, 2009, 11:11 pm

The best way to learn what it – try it in practice. Using ping.fm is very straight forward.
Thank you for your ideas!

Comment from Jeffrey
Time: July 6, 2009, 7:52 pm

It’s really a good idea to use ping.fm REST API from command line. I’ll try to do it on my sites.

Comment from ClubPenguinCheats
Time: March 22, 2010, 11:44 am

I am sorry, but I think that I need your application key, since they say:
“Note: API keys are meant for application developers. If you are using a Ping.fm app that someone has created, it shouldn’t require you submit an API key. Since we’ve recently changed this process, some of the apps might not have been updated to meet new requirements.

Comment from shadyabhi
Time: April 16, 2010, 12:42 am

I was just about to write my own script, but before writing my own I decided to do a google search and I got yours.. Thanx.. You saved my time..

Comment from mnajem
Time: November 10, 2010, 9:09 am

Hi Raynux,

Ping.fm won’t give API key anymore.

Can you share with me yours? Thanks.

Comment from poity
Time: February 22, 2011, 11:03 am

The best way to learn what it – try it in practice. Using ping.fm is very straight forward.
Thank you for your ideas!

Comment from dick
Time: February 22, 2011, 11:07 am

I shouldn’t ask the user to get their own APP Key, but the problem is If I disclose the APP key, someone may misuse it for other purpose as the source code is open and may violate fing.fm terms of use. I am thinking to encode the source code.

Write a comment