Skip to content

Instantly share code, notes, and snippets.

@alexclare
Created August 21, 2012 01:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save alexclare/3410169 to your computer and use it in GitHub Desktop.
Save alexclare/3410169 to your computer and use it in GitHub Desktop.
Hacky IRCCloud ping script
import json, pycurl, subprocess, urllib, urllib2
def session_cookie(email, pw):
'''Small helper to grab the session ID if necessary.
You don't need to do this often; session IDs don't expire frequently.
'''
req = urllib2.Request(
url='https://irccloud.com/chat/login',
data=urllib.urlencode([('email', email), ('password', pw)]))
res = urllib2.urlopen(req)
return json.loads(res.readline())['session']
class ChatStream(object):
def __init__(self, sessid, interval):
self.interval = interval
self.last_notify = 0.0
self.buffer = ''
self.curl = pycurl.Curl()
self.curl.setopt(pycurl.URL, 'https://irccloud.com/chat/stream')
self.curl.setopt(pycurl.COOKIE, 'session=' + sessid)
self.curl.setopt(pycurl.WRITEFUNCTION, self.chunk)
self.curl.perform()
def chunk(self, data):
linebreak = data.find('\n')
if linebreak >= 0:
self.handler(self.buffer + data[:linebreak])
self.buffer = data[linebreak+1:]
else:
self.buffer += data
def handler(msg):
if len(msg) > 0:
decoded = json.loads(msg)
try:
if decoded['highlight'] or decoded['from'] == decoded['chan']:
current = time.time()
if current - self.last_notify > self.interval:
self.last_notify = current
subprocess.call(['/usr/bin/notify-send',
decoded['from'][:40],
decoded['msg'][:100]])
except KeyError:
pass
# insert your session ID and minimum seconds between notifications below
ChatStream(sessionID, interval)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment