#!/usr/bin/python
import json
import urllib2
import os

class request:
	pass

class ttrss:
	session_id = ''
	def __init__(self, url):
		self.baseurl = url

	def getResponse(self, call):
		call['sid'] = self.session_id
		httpres = urllib2.urlopen(self.baseurl, json.dumps(call))
		ttrssresponse = json.load(httpres)
		return ttrssresponse
	
	def login(self, username, password):
		req = { 'op': 'login', 'user': username, 'password':password }
		res = self.getResponse(req)
		self.session_id = res['content']['session_id']

	def getUnreadCount(self):
		req = { 'op': 'getUnread'}
		res = self.getResponse(req)
		return int( res['content']['unread'] )

# TT-RSS API URL
t = ttrss('http://example.org/tt-rss/api/')
# Username, password
t.login('yourusername','yourpassword')
unread = t.getUnreadCount()
if (unread > 0):
	cmd = "/usr/local/bin/growlnotify -a Mail -m '" + str( unread ) + " unread article(s)' Tiny Tiny RSS"
	# Or on linux, using notify-send:
	# cmd = "/usr/bin/notify-send 'Tiny Tiny RSS' '" + str( unread ) + " unread article(s)'"
	# Sample crontab entry:
	# */10 * * * * DISPLAY=:0.0 ttrssnotify.py
	os.system( cmd )

