Advertisement
shadowsdieaway

Python Twitch chat listener voting script:

Apr 7th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. # Python Twitch chat listener voting script:
  2.  
  3. inputs = [
  4. {"username": "ShadowsDieAway", "input": "12 16"},
  5. {"username": "TheNewGeezer", "input": "5 24"},
  6. {"username": "YoYoYoDude1", "input": "24 16"},
  7. {"username": "ShadowsDieAway", "input": "11 11"},
  8. {"username": "TheNewGeezer", "input": "24 5"},
  9. {"username": "YoYoYoDude1", "input": "lol 16"},
  10. {"username": "ShadowsDieAway", "input": "5 5"},
  11. {"username": "TheNewGeezer", "input": "9 9"},
  12. {"username": "YoYoYoDude1", "input": "2 2"},
  13. {"username": "failure", "input": "lol hahaha"},
  14. {"username": "failure", "input": "3"},
  15. {"username": "failure", "input": "5 7 8"},
  16. {"username": "failure", "input": "nope"}
  17. ]
  18.  
  19. votes = []
  20. votes_x = {}
  21. votes_y = {}
  22.  
  23. def range_x(x): return 1 <= x <= 25
  24. def range_y(y): return 1 <= y <= 19
  25. def top_votes(type):
  26. sort = sorted(type.items(), key=lambda (k,v): v, reverse=True)
  27. return map(lambda x: "{0}: {1}".format(x[0], x[1]), sort)
  28.  
  29. for input in inputs:
  30. # parse
  31. username = input['username']
  32. vote = input['input'].split(" ")
  33. for i, v in enumerate(vote):
  34. try: vote[i] = int(v)
  35. except ValueError: vote[i] = 0
  36. # cull
  37. usernames = map(lambda x: x[0], votes)
  38. # validation
  39. if username not in usernames and len(vote) == 2 and range_x(vote[0]) and range_y(vote[1]):
  40. votes.append([username, vote[0], vote[1]])
  41. if vote[0] in votes_x: votes_x[vote[0]] += 1
  42. else: votes_x[vote[0]] = 1
  43. if vote[1] in votes_y: votes_y[vote[1]] += 1
  44. else: votes_y[vote[1]] = 1
  45.  
  46. print votes
  47. print top_votes(votes_x)
  48. print top_votes(votes_y)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement