Skip to content

Instantly share code, notes, and snippets.

@tobocop2
Last active November 13, 2016 17:09
Show Gist options
  • Save tobocop2/96aabab78958491d1037 to your computer and use it in GitHub Desktop.
Save tobocop2/96aabab78958491d1037 to your computer and use it in GitHub Desktop.
Only death is real...
import facebook
import config
import time
import platform
def best_wishes():
'''
This function loads an access token stored in a
text file (these expire so they need to be stored on disk somehow).
It then securly accesses the facebook graph api. Once connected,
the post id is found and then the comment is posted so that the post could be bumped.
Once the post is made, a new access token is retrieved and overwrites the original
'''
with open('/home/tobias/bestwishes/access_token.cfg','r') as f:
access_token=f.read()
message = 'Only death is real...Death is just the beginning\n\n\n\n\
Automatically posted with python %s and the Facebook Graph API on: %s UTC\n\
Python Compiler: %s,\n\
Operating System: %s,\n\
Distribution: %s,\n\
Architecture: %s,\n\
GNU C Lib version: %s,\n\n\
and for those that care to partake in such shenanigans,\n\
The Code: https://gist.github.com/T-G-P/96aabab78958491d1037' % \
(
platform.python_version(),
time.asctime(),
platform.python_compiler(),
platform.system(),
' '.join(platform.dist()),
' '.join(platform.architecture()),
' '.join(platform.libc_ver())
)
try:
graph = facebook.GraphAPI(access_token=access_token)
post_id = get_post_id(graph)
graph.put_comment(object_id=post_id, message=message)
access_token = graph.extend_access_token(app_id=config.app_id, app_secret=config.app_secret)['access_token']
with open('/home/tobias/bestwishes/access_token.cfg','w') as f:
f.write(access_token)
except facebook.GraphAPIError as err:
print err.message
def get_post_id(graph):
'''
This is showing the steps it might take to get the post id for any arbitrary group post in
the Computer Science group. This function is not particularly necessary because the value can
be hard coded once found in the feed json. In my case, it was still the first post in the group,
so I just took the id value from that post. However, the more activity there is in the group, the
more annoying it might be for someone to find the post id due to pagination.
'''
groups = graph.get_connections(id='me', connection_name='groups')['data']
group_id = [group['id'] for group in groups if group['name'] == 'Computer Science'][0]
feed = graph.request('/%s/feed' % group_id, args = {'limit': 1000})
for post in feed['data']:
if post['created_time'] == '2014-04-08T22:01:31+0000':
post_id = post['id']
return post_id
#For reassurance in the event that there are more than 1000 posts ahead of mine in the feed
post_id='410571755637293_806288322732299'
return post_id
if __name__ == "__main__":
best_wishes()
app_id=''
app_secret=''
#This cron will run the script at 5:55 UTC every Tuesday.
55 05 * * 2 /usr/bin/python /home/tobias/bestwishes/best_wishes.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment