Skip to content

Instantly share code, notes, and snippets.

@tobocop2
Last active August 29, 2015 14:20
Show Gist options
  • Save tobocop2/0ff78a80fedde4d0a186 to your computer and use it in GitHub Desktop.
Save tobocop2/0ff78a80fedde4d0a186 to your computer and use it in GitHub Desktop.
Thanks
from config import *
import arrow
import subprocess
import facebook
'''Comment and like all posts'''
def thank_friends():
gist = 'https://gist.github.com/T-G-P/0ff78a80fedde4d0a186'
message = """
Laziness got the best of me so I am having my server send thanks and likes for me.
I know you probably think this is weird... automation can be scary sometimes.
Anyway, I know this is a bit impersonal and I apologize.
To make up for it, let me share the mediocre specs of this virtual machine with you:\n
%s\n%s\n\n For those interested in this script: %s """ % (get_cpu_info(), get_rel_info(), gist)
try:
graph=facebook.GraphAPI(access_token=access_token)
feed = graph.request('%s/feed' % facebook_id, args = {'limit': 666})
post_ids = list(set(get_post_ids(feed)))
for post_id in post_ids:
graph.put_comment(object_id=post_id, message=message)
graph.put_like(object_id=post_id)
set_post_time(feed)
except facebook.GraphAPIError as err:
print err.message
'''Get relevant posts in array form'''
def get_post_ids(feed):
post_ids = []
for comment in feed['data']:
created_time = arrow.get(comment['created_time']).datetime
unix_time = int(created_time.strftime('%s'))
if unix_time <= int(poll_time):
return post_ids
post_ids.append(comment['id'])
return post_ids
'''Parse OS info to a string'''
def get_rel_info():
command = subprocess.Popen(
['cat', '/etc/lsb-release'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
lsb, err = command.communicate()
command = subprocess.Popen(
['cat', '/etc/os-release'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
os, err = command.communicate()
return lsb+os
'''Parse cpu info to a string'''
def get_cpu_info():
command = subprocess.Popen(
['cat', '/proc/cpuinfo'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
out, err = command.communicate()
return out
'''Update the last polling time to be the latest post in the feed'''
def set_post_time(feed):
latest_post = feed['data'][0]
created_time = arrow.get(latest_post['created_time']).datetime
unix_time = int(created_time.strftime('%s'))
if unix_time < 1430089200:
return
with open(last_poll_path,'w') as f:
f.write(str(unix_time))
if __name__ == "__main__":
thank_friends()
app_id =''
app_secret = ''
facebook_id = ''
token_path = ''
last_poll = ''
with open(token_path,'r') as f: access_token = f.read()
with open(last_poll,'r') as f: poll_time = int(f.read())
#Cron runs every half hour on April 27th.
*/30 * 27-28 04 * <path to python> <path to script>
<poll time stored here>
<access token goes here>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment