Created
February 1, 2012 21:43
-
-
Save neonichu/1719653 to your computer and use it in GitHub Desktop.
Post your most recent tweets to Google+
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
## Configuration ## | |
################### | |
gplus_user = '<<<Google+ username>>>' | |
gplus_pass = '<<<Google+ password>>>' | |
tweet_count = 25 # Number of tweets to consider | |
twitter_user = '<<<Twitter username>>>' | |
################### | |
require 'rubygems' | |
require 'mechanize' | |
require 'twitter' | |
def gplus_post(username, password, content) | |
agent = Mechanize.new | |
agent.user_agent = 'HTC_Mega Opera/9.5 (Microsoft Windows; PPC Opera Mobi /17753; U; es-ES)' | |
page = agent.get('https://accounts.google.com/ServiceLoginAuth') | |
form = page.forms.first | |
form.Email = username | |
form.Passwd = password | |
agent.submit form | |
page = agent.get('https://plus.google.com') | |
post = page.links_with(:text => 'Post').first | |
page = agent.click post | |
form = page.forms.first | |
form.newcontent = content | |
post = form.button_with(:value => 'Post') | |
form.submit post | |
end | |
cache_file = File.expand_path('~/.minus') | |
read_ids = [] | |
if File.exists?(cache_file) | |
read_ids += Marshal.load File.read(cache_file) | |
end | |
for status in Twitter.user_timeline(twitter_user, :count => tweet_count).reverse | |
if status.retweeted_status.nil? and status.in_reply_to_status_id.nil? | |
unless read_ids.include?(status.id) | |
begin | |
gplus_post(gplus_user, gplus_pass, status.text) | |
read_ids.push(status.id) | |
rescue | |
puts 'Unable to post status ' + status.id.to_s() | |
end | |
end | |
end | |
end | |
read_ids = Marshal.dump(read_ids) | |
File.open(cache_file, 'w') { |f| f.write(read_ids) } |
Updated the script so that it handles errors during posting to Google+ more gracefully. If one status fails, it will move on to the next and it will always write the cache file in the end. That avoids double posts when something went wrong.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The script considers a fixed amount of tweets and ignores both replies and retweets.
IDs of tweets already posted to Google+ are cached to prevent double-posts.
The thing is designed to be run periodically as a cronjob.