Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- import random
- import praw
- import copy
- import pandas as pd
- import numpy as np
- reddit = praw.Reddit(client_id = 'client_id',
- client_secret = 'client_secret',
- user_agent = 'user_agent',
- username = 'username',
- password = 'password')
- submission = reddit.submission(id='7oe8x8') #get submission object
- submission.comments.replace_more(limit=None)
- #get all top level comments and parse for game IDs
- entries = list()
- for top_level_comment in submission.comments:
- ids = re.findall('(GM\d{3})',top_level_comment.body, re.IGNORECASE)
- ids[:] = [x.upper() for x in ids]
- ids.insert(0, top_level_comment.author)
- entries.append(ids)
- # choose the winners
- winners = list()
- exhausted = list()
- while (len(exhausted) < 154): #all games must go
- entries_selection = copy.deepcopy(entries)
- for sublist in entries_selection: #remove IDs for games that already have a winner
- sublist[:] = [x for x in sublist if x not in exhausted]
- entries_selection[:] = [x for x in entries_selection if len(x) != 1] #removes entries without valid games
- while (len(entries_selection) > 0):
- winner_index = random.randint(0,len(entries_selection)-1) #choose a random winner
- winners.append(entries_selection[winner_index][0:2]) #add winner name and game to list
- exhausted.append(entries_selection[winner_index][1]) #add won game to list
- del entries_selection[winner_index] #remove winner for this round
- for sublist in entries_selection: #remove IDs for games that already have a winner
- sublist[:] = [x for x in sublist if x not in exhausted]
- entries_selection[:] = [x for x in entries_selection if len(x) != 1] #removes entries without valid games
- df = pd.read_csv('game_keys.csv') #csv with game title and key
- #make a df with the winners and key
- winners_df = pd.DataFrame(winners, columns=['User', 'ID'])
- winners_df = winners_df.merge(df, how='left', on='ID')
- #send winners pm with key
- for row in winners_df.itertuples():
- body = 'Congradulaions!! \n\nYou won {}: {} \n\nKey: {} \n\nEnjoy!!'.format(row[2], row[3], row[4])
- reddit.redditor(str(row[1])).message('Giveaway Winner!', body)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement