# A simple twitch notifier for fedora. # I only tested this on fedora but maby it works on other distributions as well. # The syntax is : "python scriptname streamer". # It will make a sound if the streamer goes on-air. import urllib2 import json import sys import os import time import pygame import dbus def alertbox(user, state): bus = dbus.SessionBus() notifications = bus.get_object('org.freedesktop.Notifications', '/org/freedesktop/Notifications') interface = dbus.Interface(notifications, 'org.freedesktop.Notifications') id = 4856 timeout = 2500 if state == 1: title = getTitle(user) interface.Notify('name',id,'', user + ' is online', title,'','',timeout) else: interface.Notify('name',id,'', user + ' is offline', '','','',timeout) def getTitle(user): url = 'http://api.justin.tv/api/stream/list.json?channel=' + user result =json.loads(urllib2.urlopen(url, timeout = 100).read().decode('utf-8')) return result[0]['title'] def checkuser(user): try: url = 'http://api.justin.tv/api/stream/list.json?channel=' + user result =json.loads(urllib2.urlopen(url, timeout = 100).read().decode('utf-8')) if result: return True else: return False except: print "error" return "error" def alertsound(): #edit this to your own alert sound path urlSound = "alert.wav" pygame.init() pygame.mixer.music.load(urlSound) pygame.mixer.music.play() time.sleep(5) pygame.mixer.music.stop() def init(): prev = 0 user = sys.argv[1] while True: if prev == 0: while True: online = checkuser(user) if online == True: alertbox(user, 1) alertsound() prev = 1 break time.sleep(10) else: while True: online = checkuser(user) if online == False: alertbox(user, 0) alertsound() prev = 0 break time.sleep(10) init()