PyTwitFace (24-Apr @ 14:53)

j4mie

Syntax Highlighted Code

  1. import twitter, feedparser, socket
  2.  
  3. #
  4. # SETTINGS
  5. [62 more lines...]

Plain Code

import twitter, feedparser, socket
 
#
# SETTINGS
#
 
twitteruser = 'username'
twitterpassword = 'password'
facebook_feed_address = "Your full Facebook status feed URL"
 
# Will be appended to facebook status
to_append = "(from Facebook)"
 
# These words will be removed from the
# beginning of your status if present
verbs = ['is', 'has']
 
 
 
# Make sure the script doesn't hang if it can't get through to
# Twitter or Facebook (ie network is down).
timeout = 60 # seconds
socket.setdefaulttimeout(timeout)
 
#
# FACEBOOK STUFF
#
 
# Get most recent facebook status.
feed = feedparser.parse(facebook_feed_address)
most_recent_facebook = feed.entries[0].title
 
# Convert facebook status string into a list of words
most_recent_facebook_words = most_recent_facebook.split()
 
# Get rid of name from start of string.
del most_recent_facebook_words[0]
 
# If next word is in verb list, remove it.
if most_recent_facebook_words[0] in verbs:
del most_recent_facebook_words[0]
 
# Capitalise first word.
most_recent_facebook_words[0] = most_recent_facebook_words[0].title()
 
# Append selected string.
most_recent_facebook_words.append(to_append)
 
# Convert back into a string
most_recent_facebook = " ".join(most_recent_facebook_words)
 
#
# TWITTER STUFF
#
 
# Set up API
twitterapi = twitter.Api(username=twitteruser, password=twitterpassword)
 
# Get list of statuses
twitter_statuses = twitterapi.GetUserTimeline(twitteruser)
twitter_statuses_text = [s.text for s in twitter_statuses]
 
# Check if your current Facebook status
# has already been posted to Twitter
if not most_recent_facebook in twitter_statuses_text:
# If not, post it!
twitterapi.PostUpdate(most_recent_facebook)