Advertisement
typwo

follower_popup.py

Sep 26th, 2014
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. """
  2. follower_popup.py
  3. ~~~~~~~~~~~~~~~~~
  4.  
  5. Uses Twitch's API to grab follower information and show alert popups from the
  6. systray when a user follows. Each notification stays active for approximately
  7. 20 seconds. Notifications sit in a queue and do not overlap, so long queues
  8. will definitely occur if you're particularly popular.
  9.  
  10. """
  11.  
  12. from win32api import *
  13. from win32gui import *
  14. from datetime import datetime, timedelta
  15.  
  16. import win32con
  17. import sys, os
  18. import struct
  19. import time
  20. import requests
  21. import string
  22. import random
  23.  
  24. # Configuration
  25.  
  26. USERNAME = "typwo"
  27. UTC_OFFSET = 4
  28. FOLLOWER_LIMIT = 10
  29.  
  30. # End configuration
  31.  
  32. class SystrayNote:
  33. def __init__(self, title, msg):
  34. wc = WNDCLASS()
  35.  
  36. hinst = wc.hInstance = GetModuleHandle(None)
  37. wc.lpszClassName = "PythonTaskbar"
  38. wc.lpfnWndProc = { win32con.WM_DESTROY: self.destroy, }
  39.  
  40. # Create class for usage
  41. classAtom = RegisterClass(wc)
  42.  
  43. style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU
  44. self.hwnd = CreateWindow(classAtom, "Taskbar", style, 0, 0, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, 0, 0, hinst, None)
  45.  
  46. UpdateWindow(self.hwnd)
  47. iconPathName = os.path.abspath(os.path.join(sys.path[0], "balloontip.ico"))
  48. icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE
  49.  
  50. try:
  51. hicon = LoadImage(hinst, iconPathName, win32con.IMAGE_ICON, 0, 0, icon_flags)
  52. except:
  53. hicon = LoadIcon(0, win32con.IDI_APPLICATION)
  54.  
  55. flags = NIF_ICON | NIF_MESSAGE | NIF_TIP
  56. nid = (self.hwnd, 0, flags, win32con.WM_USER+20, hicon, "tooltip")
  57.  
  58. Shell_NotifyIcon(NIM_ADD, nid)
  59. Shell_NotifyIcon(NIM_MODIFY, (self.hwnd, 0, NIF_INFO, win32con.WM_USER+20, hicon, "Balloon tooltip", msg, 200, title))
  60.  
  61. time.sleep(19)
  62. DestroyWindow(self.hwnd)
  63.  
  64. # Unregister class to avoid conflicts
  65. classAtom = UnregisterClass(classAtom, hinst)
  66.  
  67. def destroy(self, hwnd, msg, wparam, lparam):
  68. nid = (self.hwnd, 0)
  69. Shell_NotifyIcon(NIM_DELETE, nid)
  70. PostQuitMessage(0)
  71.  
  72. def note_instance(title, msg):
  73. inst = SystrayNote(title, msg)
  74.  
  75. def process_date(jsondate):
  76. udate, utime = jsondate[:-1].split("T")
  77. ds = map(int, udate.split("-"))
  78. ts = map(int, utime.split(":"))
  79. fdate = datetime(ds[0], ds[1], ds[2], ts[0], ts[1], ts[2])
  80. return (fdate - timedelta(hours=UTC_OFFSET))
  81.  
  82. if __name__ == '__main__':
  83. queue = []
  84. followers = []
  85. pdate = None
  86. limit = 1
  87.  
  88. while True:
  89. scandate = format(datetime.utcnow())
  90. cid = ''.join(random.choice(string.ascii_lowercase) for i in range(16))
  91. base_url = "https://api.twitch.tv/kraken/channels/{0}".format(USERNAME)
  92. params = "&limit={0}&offset=0&client_id={1}".format(limit, cid)
  93.  
  94. r = requests.get(base_url + '/follows?direction=desc' + params)
  95. json = r.json()
  96.  
  97. for entry in reversed(json["follows"]):
  98. fdate = process_date(entry["created_at"])
  99. uname = entry["user"]["name"]
  100. print fdate, " ", uname
  101.  
  102. if (pdate is None or fdate > pdate) and \
  103. uname not in [x[0] for x in queue] and \
  104. uname not in followers:
  105. queue.append([uname, fdate])
  106.  
  107. print "---"
  108.  
  109. if not queue:
  110. time.sleep(10)
  111. else:
  112. print "Current Queue: {0}\n---".format([str(x[0]) for x in queue])
  113.  
  114. queue.reverse()
  115. data = queue.pop()
  116. queue.reverse()
  117.  
  118. if data[0] not in followers:
  119. note_instance(data[0], "User followed at {0}.".format(data[1]))
  120. pdate = fdate
  121.  
  122. if uname not in followers:
  123. followers.append(data[0])
  124.  
  125. time.sleep(1)
  126.  
  127. limit = FOLLOWER_LIMIT
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement