Skip to content

Instantly share code, notes, and snippets.

@auscompgeek
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save auscompgeek/0f8108b665c98239ff81 to your computer and use it in GitHub Desktop.
Save auscompgeek/0f8108b665c98239ff81 to your computer and use it in GitHub Desktop.
grab a list of votes from a Facebook group post's comments
#!/usr/bin/env python3
import collections
import re
import requests
import sys
###### CHANGE THESE WHEN NECESSARY
access_token = 'generate one, use the Graph API Explorer if lazy'
post_id = 925687554138366
cutoff = '2015-01-23T13:00:00+0000'
###### END OF CONFIG
URI_TEMPLATE = 'https://graph.facebook.com/v2.2/{post_id}/comments?fields=from,message,message_tags,created_time&limit=200&access_token={access_token}'
PRINT_COMMENT_TEMPLATE = '{created_time} {from[name]}\n{message}\n'
PRINT_VOTES_TEMPLATE = '{0}: {1}\t({2})'
REAL_NAME_MAP = {
'Zark': 'Zark Friggin Dinkenburg',
}
VOTABLE_PLAYERS = {
# ...
"David Vo",
}
def print_comment(comment):
print(PRINT_COMMENT_TEMPLATE.format_map(comment), file=sys.stderr)
def query_yn(question, default=True):
user = input('{0} [{1}] '.format(question, 'Yn' if default else 'yN'))
return user[0] == 'y' if user else default
find_vote_re = re.compile(r'\bV(?:OTE|ote): (\S+(?: \w+(?: [A-Z]\w+)?)?)')
find_vote = find_vote_re.search
# TL;DR: I hate people
#find_maybe_vote_re = re.compile(r'\bVOTE @?(\S+(?: \w+)?)', re.I)
#find_maybe_vote = find_maybe_vote_re.search
r = requests.get(URI_TEMPLATE.format(access_token=access_token, post_id=post_id))
comments = [x for x in r.json()['data'] if 'VOTE: ' in x['message'].upper()]
votes = collections.defaultdict(list)
voted = set()
for comment in reversed(comments):
voter = comment['from']['name']
if voter in voted:
continue
timestamp = comment['created_time']
if timestamp >= cutoff: # oh god what have I done
continue
message = comment['message']
match = find_vote(message)
if match:
print_comment(comment)
else:
#match = find_maybe_vote(message)
#if not match:
if 'UNVOTE: ' in message.upper():
print_comment(comment)
voted.add(voter)
continue
# print_comment(comment)
# if not query_yn('Is this a vote?'):
# continue
votee = match.group(1)
votee = votee.title()
votee = REAL_NAME_MAP.get(votee, votee)
votes[votee].append(voter)
voted.add(voter)
len_longest_votee = max(map(len, votes))
for votee in votes:
voters = votes[votee]
print(PRINT_VOTES_TEMPLATE.format(votee.rjust(len_longest_votee), len(voters), ', '.join(voters)))
print('\nNon-voters:', ', '.join(VOTABLE_PLAYERS - voted))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment