Created
December 7, 2021 02:30
-
-
Save jwinterm/00523cd1c4b832725c499711962d5b31 to your computer and use it in GitHub Desktop.
Python code to select winners for 10k MOON giveaway
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
import praw | |
from praw.models import MoreComments | |
client_id='asdf' | |
client_secret='asdf' | |
username='asdf' | |
password='asdf' | |
user_agent='asdf' | |
# Connect to reddit | |
reddit = praw.Reddit( | |
user_agent=user_agent, | |
client_id=client_id, | |
client_secret=client_secret, | |
username=username, | |
password=password, | |
) | |
# Get a praw submission object | |
url="https://www.reddit.com/r/CryptoCurrency/comments/r8b8zr/four_million_subscriber_10000_moon_giveaway/" | |
submission = reddit.submission(url=url) | |
# Block info https://www.blockchain.com/btc/block/00000000000000000005bfd3649743e8909a1efe9bd66157621dbef80c4b2a0f | |
target = 731663 | |
block_time = 1638844920 | |
comments = [] | |
# Cycle through all comments and add them to list if they were made before block was found | |
submission.comments.replace_more(limit=None) | |
for top_level_comment in submission.comments: | |
if isinstance(top_level_comment, MoreComments): | |
continue | |
else: | |
# Check to make sure that the comment is not edited and was created before bitcoin block 712948 | |
if top_level_comment.edited == False and top_level_comment.created_utc < block_time: | |
# Remove commas, periods and hyphens and try to cast to int and add to list of comments | |
try: | |
comments.append([top_level_comment.author, int(top_level_comment.body.replace(',', '').replace('.', '').replace('-', '').split()[0]), top_level_comment.body]) | |
# If that fails just add permalink to list to check what it was | |
except: | |
comments.append([top_level_comment.author, top_level_comment.permalink, top_level_comment.body]) | |
# Variables to hold top three guesses and users | |
winners = [(-1e9, None), (-1e9, None), (-1e9, None)] | |
with open('10kgiveaway.csv', 'w', newline='') as output: | |
spamwriter = csv.writer(output, delimiter=',') | |
for i in comments: | |
# Write whole row (author / guess / full comment) to spreadsheet | |
spamwriter.writerow(i) | |
for idx, j in enumerate(winners): | |
# If the absolute value of difference of current comment guess minus target is less than that of the current winner, then replace that winner | |
if isinstance(i[1], int): | |
if abs(i[1]-target) < abs(j[0] - target): | |
winners[idx] = (i[1], i[0]) | |
break | |
print(winners) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment