Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.20 KB | None | 0 0
  1. import praw
  2. import time
  3. from termcolor import colored
  4. import pickle as pl
  5. from collections import OrderedDict
  6. #define constants
  7. SUBREDDIT = 'ShittyRainbow6'
  8. THRESHOLD = 80
  9. global oldtop
  10. oldtop = ''
  11.  
  12. #Initialize Reddit instance, the only perms
  13. #the bot needs are flair and post
  14. r = praw.Reddit(client_id='UMYNU5MhRF-jPg',
  15. client_secret='SdZvR_VXfXeJwaO_NioflliewhI',
  16. password='1lafot12XinyL5r6XsttX14a2',
  17. user_agent='User flair points for for /r/ShittyRainbow6',
  18. username='RainbowPointsBot')
  19.  
  20.  
  21. #Check if the user already has a flair
  22. def hasFlair(user):
  23. uflair = [x for x in r.subreddit(SUBREDDIT).flair(str(user))][0]['flair_text']
  24. print(uflair)
  25. if uflair == None or 'Points:' not in uflair or uflair == 'None' or str(uflair) == 'None':
  26. return False
  27. else:
  28. return True
  29.  
  30. #add flair to new user
  31. def addFlair(user):
  32. r.subreddit(SUBREDDIT).flair.set(user, 'Points: 0', css_class='memer')
  33.  
  34.  
  35. def saveChanges(user, score):
  36. allUsers = pl.load(open('save.p','rb'))
  37. allUsers[user] = score
  38. pl.dump(allUsers, open('save.p', 'wb'))
  39.  
  40. def top5():
  41. sdict = pl.load(open('save.p','rb'))
  42. fDict = OrderedDict()
  43. tUsers = sorted(sdict, key=sdict.get)
  44. x = 0
  45. for user in tUsers[::-1]:
  46. x += 1
  47. if x > 5:
  48. pass
  49. else:
  50. fDict[user] = sdict[user]
  51. return fDict
  52.  
  53.  
  54. def updateSidebar(topusers):
  55. print(topusers)
  56. sidebar_current = r.subreddit(SUBREDDIT).mod.settings()['description'].split('[MemePointsLeaderBoards](#noshow)')
  57. print(sidebar_current)
  58. sidebar_current[1] = '''[MemePointsLeaderBoards](#noshow)\n\n
  59. User | Score
  60. ---|---
  61. {}
  62. [MemePointsLeaderBoards](#noshow)'''.format('\n'.join(['/u/' + list(topusers.keys())[x] + ' | ' + str(list(topusers.values())[x]) for x in range(len(topusers))]))
  63. sidebar_final = ''.join(sidebar_current)
  64. r.subreddit(SUBREDDIT).mod.update(description=sidebar_final)
  65.  
  66.  
  67.  
  68. #increment the flair of a user
  69. def incrementFlair(user, post):
  70. uflair = [x for x in r.subreddit(SUBREDDIT).flair(str(user))][0]['flair_text']
  71. while uflair == None or 'Points:' not in uflair or uflair == 'None' or str(uflair) == 'None':
  72. uflair = [x for x in r.subreddit(SUBREDDIT).flair(str(user))][0]['flair_text']
  73. print('UFLAIR HERE:')
  74. print(uflair)
  75. num = str(int(uflair.strip('Points: '))+1)
  76. r.subreddit(SUBREDDIT).flair.set(user, 'Points: {}'.format(num), css_class='memer')
  77. #myComment = post.reply('''Hey, {}! Congratulations on getting over {} points on this post. I've added **1 point** to your user flair as a reward.'''.format(str(post.author), str(THRESHOLD)))
  78. #myComment.mod.distinguish(how='yes', sticky=True)
  79. saveChanges(str(post.author), int(num))
  80. try:
  81. print(colored('Updated flair of {} to {}'.format(str(post.author), str(num)), 'blue'))
  82. except:
  83. pass
  84. newtop = top5()
  85. if oldtop != newtop:
  86. global oldtop
  87. oldtop = newtop
  88. updateSidebar(newtop)
  89.  
  90. #check score of the post
  91. def checkScore(post):
  92. return True if post.score >= THRESHOLD else False
  93.  
  94.  
  95. def checkPosts():
  96. for post in [post for post in r.subreddit(SUBREDDIT).new(limit=100) if post.id not in open('aldone.txt','r').read().split('\n')]:
  97. if checkScore(post) and hasFlair(post.author):
  98. open('aldone.txt','a').write(post.id + '\n')
  99. incrementFlair(post.author, post)
  100. elif checkScore(post):
  101. open('aldone.txt','a').write(post.id + '\n')
  102. if not hasFlair(post.author):
  103. print('CREATING FLAIR')
  104. addFlair(post.author)
  105. while not hasFlair(post.author):
  106. time.sleep(0.01)
  107. incrementFlair(post.author, post)
  108.  
  109. def undoAll():
  110. for post in [post for post in r.subreddit(SUBREDDIT).new(limit=100) if post.score >= 80]:
  111. r.subreddit(SUBREDDIT).flair.set(post.author, '', css_class='')
  112.  
  113.  
  114. while True:
  115. time.sleep(30)
  116. try:
  117. checkPosts()
  118. except Exception as e:
  119. print(colored(str(e), 'red'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement