Skip to content

Instantly share code, notes, and snippets.

@bcse
Created September 8, 2011 04:01
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 bcse/1202576 to your computer and use it in GitHub Desktop.
Save bcse/1202576 to your computer and use it in GitHub Desktop.
Automatic post latest smiley on your timeline (Plurk API 1.0)
#!/usr/bin/env python
APIKEY = 'YOUR_APIKEY'
USERNAME = 'YOUR_USERNAME'
PASSWORD = 'YOUR_PASSWORD'
#--- Setup ---------------------------------------------------
import sys, os, urllib, urllib2, cookielib
try:
import json
except:
import simplejson as json
base_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
get_path = lambda x: '%s/%s' % (base_dir, x)
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())
api_key = APIKEY
get_api_url = lambda x: 'http://www.plurk.com/API%s' % x
encode = urllib.urlencode
def new_emotions():
fp = opener.open(get_api_url('/Emoticons/get'),
encode({'api_key': api_key}))
resp = json.loads(fp.read())
# strip unused info
new_emoticons = []
for i in [[icon[0] for icon in icons] for (karma, icons) in resp['karma'].iteritems()]:
for j in i:
new_emoticons.append(j)
# load old emoticons
old_emoticons = json.load(open(get_path('emoticons.cache'), 'r'))
# compare them
diff = [i for i in new_emoticons if i not in old_emoticons]
if diff:
# cache latest emoticons
json.dump(new_emoticons, open(get_path('emoticons.cache'), 'w'))
return diff
def login(username, password):
fp = opener.open(get_api_url('/Users/login'),
encode({'username': username,
'password': password,
'no_data': '1',
'api_key': api_key}))
resp = json.loads(fp.read())
return resp.has_key('success_text')
def post(message, qualifier='says', lang='en'):
fp = opener.open(get_api_url('/Timeline/plurkAdd'),
encode({'content': message,
'qualifier': qualifier,
'lang': lang,
'api_key': api_key}))
return json.loads(fp.read())
if __name__ == '__main__':
new_emotions = new_emotions()
if new_emotions:
print 'New emotions found.'
if login(USERNAME, PASSWORD):
print 'Login success.'
post(str.join(' ', new_emotions).encode('utf-8'), qualifier='wonders', lang='tr_ch')
print '%d new emoticons were added: %s' % (len(new_emotions), str.join(', ', new_emotions))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment