Skip to content

Instantly share code, notes, and snippets.

@alagappanr
Created September 21, 2013 18:44
Show Gist options
  • Select an option

  • Save alagappanr/6653093 to your computer and use it in GitHub Desktop.

Select an option

Save alagappanr/6653093 to your computer and use it in GitHub Desktop.
A simple and quick program which calls the Facebook Graph API url for the Goyaka Radios FB Group (https://www.facebook.com/groups/goyakaradios/), reads its feed and tweets it to Twitter (http://twitter.com/GoyakaRadios). Feedback/Suggestions/Improvements are welcome.
"""Program to Tweet from Goyaka Radios FB Group"""
__author__ = 'Alagappan'
__version__ = '0.1'
import requests
import twitter
#import pdb
import time
FB_CONFIG = {'app_id' : "FB_APP_ID",
'app_secret' : "FB_APP_SECRET",
'goyaka_app_id' : "187054981393266",
'graph_url' : "https://graph.facebook.com/",
'endpoint' : "%s/feed",
}
GEN_CONFIG = {"LIMIT" : 25,
"URL_KIND" : ('youtube', 'youtu.be'),
}
TW_CONFIG = {'api_parameters':
{'consumer_key': "TWITTER_CONSUMER_KEY",
'consumer_secret': "TWITTER_CONSUMER_SECRET",
'access_token_key': "TWITTER_ACCESS_KEY",
'access_token_secret': "TWITTER_ACCESS_SECRET"},
'hashtag': '#GoyakaRadios',
}
#pdb.set_trace()
FB_CONFIG['endpoint'] = FB_CONFIG['endpoint'] % FB_CONFIG['goyaka_app_id']
URL = FB_CONFIG['graph_url']+FB_CONFIG['endpoint']
PAYLOAD = {"limit":GEN_CONFIG['LIMIT'],
"access_token":FB_CONFIG['app_id']+"|"+FB_CONFIG['app_secret']}
API_OBJ = twitter.Api(**TW_CONFIG['api_parameters'])
LISTS_OF_SONGS_TWEETED = []
def read_and_tweet():
"""Calls the Facebook Graph API to fetch the data and tweets all
new links that have not been tweeted already"""
req = requests.get(URL, params=PAYLOAD)
data = req.json()
for item in data['data']:
if 'link' in item and \
any(x in item['link'] for x in GEN_CONFIG['URL_KIND']):
if item['id'] not in LISTS_OF_SONGS_TWEETED:
try:
status = item['name'] + " " + item['link'] + \
" " + TW_CONFIG['hashtag']
API_OBJ.PostUpdate(status)
LISTS_OF_SONGS_TWEETED.insert(0, item['id'])
except:
pass
if len(LISTS_OF_SONGS_TWEETED)>GEN_CONFIG['LIMIT']:
LISTS_OF_SONGS_TWEETED.pop()
if __name__ == "__main__":
while(1):
read_and_tweet()
time.sleep(600)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment