Advertisement
Guest User

Untitled

a guest
Jan 7th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.29 KB | None | 0 0
  1. import re
  2. import random
  3. import praw
  4. import copy
  5. import pandas as pd
  6. import numpy as np
  7.  
  8. reddit = praw.Reddit(client_id = 'client_id',
  9.                      client_secret = 'client_secret',
  10.                      user_agent = 'user_agent',
  11.                      username = 'username',
  12.                      password = 'password')
  13.  
  14. submission = reddit.submission(id='7oe8x8') #get submission object
  15. submission.comments.replace_more(limit=None)
  16.  
  17. #get all top level comments and parse for game IDs
  18. entries = list()
  19. for top_level_comment in submission.comments:
  20.     ids = re.findall('(GM\d{3})',top_level_comment.body, re.IGNORECASE)
  21.     ids[:] = [x.upper() for x in ids]
  22.     ids.insert(0, top_level_comment.author)
  23.     entries.append(ids)
  24.  
  25. # choose the winners
  26. winners = list()
  27. exhausted = list()
  28.  
  29. while (len(exhausted) < 154): #all games must go
  30.     entries_selection = copy.deepcopy(entries)
  31.    
  32.     for sublist in entries_selection: #remove IDs for games that already have a winner
  33.         sublist[:] = [x for x in sublist if x not in exhausted]
  34.        
  35.     entries_selection[:] = [x for x in entries_selection if len(x) != 1] #removes entries without valid games
  36.  
  37.     while (len(entries_selection) > 0):  
  38.         winner_index = random.randint(0,len(entries_selection)-1) #choose a random winner
  39.         winners.append(entries_selection[winner_index][0:2]) #add winner name and game to list
  40.         exhausted.append(entries_selection[winner_index][1]) #add won game to list
  41.  
  42.         del entries_selection[winner_index] #remove winner for this round
  43.        
  44.         for sublist in entries_selection: #remove IDs for games that already have a winner
  45.             sublist[:] = [x for x in sublist if x not in exhausted]
  46.  
  47.         entries_selection[:] = [x for x in entries_selection if len(x) != 1] #removes entries without valid games
  48.  
  49. df = pd.read_csv('game_keys.csv') #csv with game title and key
  50.  
  51. #make a df with the winners and key
  52. winners_df = pd.DataFrame(winners, columns=['User', 'ID'])
  53. winners_df = winners_df.merge(df, how='left', on='ID')
  54.  
  55. #send winners pm with key
  56. for row in winners_df.itertuples():
  57.     body = 'Congradulaions!! \n\nYou won {}: {} \n\nKey: {} \n\nEnjoy!!'.format(row[2], row[3], row[4])
  58.     reddit.redditor(str(row[1])).message('Giveaway Winner!', body)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement