Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- follower_popup.py
- ~~~~~~~~~~~~~~~~~
- Uses Twitch's API to grab follower information and show alert popups from the
- systray when a user follows. Each notification stays active for approximately
- 20 seconds. Notifications sit in a queue and do not overlap, so long queues
- will definitely occur if you're particularly popular.
- """
- from win32api import *
- from win32gui import *
- from datetime import datetime, timedelta
- import win32con
- import sys, os
- import struct
- import time
- import requests
- import string
- import random
- # Configuration
- USERNAME = "typwo"
- UTC_OFFSET = 4
- FOLLOWER_LIMIT = 10
- # End configuration
- class SystrayNote:
- def __init__(self, title, msg):
- wc = WNDCLASS()
- hinst = wc.hInstance = GetModuleHandle(None)
- wc.lpszClassName = "PythonTaskbar"
- wc.lpfnWndProc = { win32con.WM_DESTROY: self.destroy, }
- # Create class for usage
- classAtom = RegisterClass(wc)
- style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU
- self.hwnd = CreateWindow(classAtom, "Taskbar", style, 0, 0, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, 0, 0, hinst, None)
- UpdateWindow(self.hwnd)
- iconPathName = os.path.abspath(os.path.join(sys.path[0], "balloontip.ico"))
- icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE
- try:
- hicon = LoadImage(hinst, iconPathName, win32con.IMAGE_ICON, 0, 0, icon_flags)
- except:
- hicon = LoadIcon(0, win32con.IDI_APPLICATION)
- flags = NIF_ICON | NIF_MESSAGE | NIF_TIP
- nid = (self.hwnd, 0, flags, win32con.WM_USER+20, hicon, "tooltip")
- Shell_NotifyIcon(NIM_ADD, nid)
- Shell_NotifyIcon(NIM_MODIFY, (self.hwnd, 0, NIF_INFO, win32con.WM_USER+20, hicon, "Balloon tooltip", msg, 200, title))
- time.sleep(19)
- DestroyWindow(self.hwnd)
- # Unregister class to avoid conflicts
- classAtom = UnregisterClass(classAtom, hinst)
- def destroy(self, hwnd, msg, wparam, lparam):
- nid = (self.hwnd, 0)
- Shell_NotifyIcon(NIM_DELETE, nid)
- PostQuitMessage(0)
- def note_instance(title, msg):
- inst = SystrayNote(title, msg)
- def process_date(jsondate):
- udate, utime = jsondate[:-1].split("T")
- ds = map(int, udate.split("-"))
- ts = map(int, utime.split(":"))
- fdate = datetime(ds[0], ds[1], ds[2], ts[0], ts[1], ts[2])
- return (fdate - timedelta(hours=UTC_OFFSET))
- if __name__ == '__main__':
- queue = []
- followers = []
- pdate = None
- limit = 1
- while True:
- scandate = format(datetime.utcnow())
- cid = ''.join(random.choice(string.ascii_lowercase) for i in range(16))
- base_url = "https://api.twitch.tv/kraken/channels/{0}".format(USERNAME)
- params = "&limit={0}&offset=0&client_id={1}".format(limit, cid)
- r = requests.get(base_url + '/follows?direction=desc' + params)
- json = r.json()
- for entry in reversed(json["follows"]):
- fdate = process_date(entry["created_at"])
- uname = entry["user"]["name"]
- print fdate, " ", uname
- if (pdate is None or fdate > pdate) and \
- uname not in [x[0] for x in queue] and \
- uname not in followers:
- queue.append([uname, fdate])
- print "---"
- if not queue:
- time.sleep(10)
- else:
- print "Current Queue: {0}\n---".format([str(x[0]) for x in queue])
- queue.reverse()
- data = queue.pop()
- queue.reverse()
- if data[0] not in followers:
- note_instance(data[0], "User followed at {0}.".format(data[1]))
- pdate = fdate
- if uname not in followers:
- followers.append(data[0])
- time.sleep(1)
- limit = FOLLOWER_LIMIT
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement