Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update client and library to just use a listen URL #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
*.pyc
build
dist
tynio.egg-info
6 changes: 3 additions & 3 deletions docs/libnotify-example.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from twisted.python import log as twistedLog
from twisted.internet import reactor

API_KEY = 'YOUR_API_KEY_GOES_HERE'
LISTEN_TO = 'EMAIL_ADDRESS_TO_LISTEN_TO'
# Replace with the contents of your .ListenURL file
LISTEN_URL = 'http://listen.notify.io/~1/listen/XXX'

class Notifier(object):
''' A small wrapper around libnotify. Because it has a crappy API. '''
Expand All @@ -26,6 +26,6 @@ def notify(self, note):

n = Notifier('NIO Linux Client')

nio = tynio.NotifyIO(API_KEY, LISTEN_TO, n.notify)
nio = tynio.NotifyIO(LISTEN_URL, n.notify)
nio.listen()
reactor.run()
18 changes: 11 additions & 7 deletions tynio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from twisted.internet.protocol import ReconnectingClientFactory
from twisted.internet import reactor
from twisted.python import log as twistedLog
from urlparse import urlparse

try:
import simplejson as json
Expand Down Expand Up @@ -37,16 +38,17 @@ def lineReceived(self, line):

def connectionMade(self):
self.sendCommand('GET', self.factory.path)
self.sendHeader('Host', 'api.notify.io')
self.sendHeader('Host', self.factory.host)
self.sendHeader('User-Agent', self.factory.agent)
self.endHeaders()
twistedLog.msg('Connected and receiving...')

class _CometFactory(ReconnectingClientFactory):
protocol = _CometStream

def __init__(self, callback, path, agent=None):
def __init__(self, callback, host, path, agent=None):
self.callback = callback
self.host = host
self.path = path
self.agent = agent

Expand All @@ -61,14 +63,16 @@ def clientConnectionLost(self, connector, reason):
ReconnectingClientFactory.clientConnectionLost(self, connector, reason)

class NotifyIO(object):
def __init__(self, api_key, email, callback, api_version=1):
def __init__(self, listen_url, callback):
self.callback = callback
path = '/v%(version)i/listen/%(email)s?api_key=%(api)s' % \
dict(version = api_version, email = md5(email).hexdigest(), api = api_key)
self.factory = _CometFactory(callback, path)
url = urlparse(listen_url)
self.path = url.path
if url.query: self.path += '?' + url.query
self.host = url.hostname
self.factory = _CometFactory(callback, self.host, self.path)

def listen(self):
reactor.connectTCP('api.notify.io', 80, self.factory)
reactor.connectTCP(self.host, 80, self.factory)

def kill(self):
self.factory.doStop()