Google+ to Twitter
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 | |
require 'open-uri' | |
require 'time' | |
require 'tmpdir' | |
require 'rubygems' | |
require 'oauth' | |
require 'json/pure' | |
user_id = '(Your Google+ user ID)' | |
api_key = '(Your Google API key)' | |
consumer_key = '(Your OAuth consumer key)' | |
consumer_secret = '(Your OAuth consumer secret)' | |
consumer = OAuth::Consumer.new( consumer_key, consumer_secret, :site => "http://twitter.com") | |
access_token = '(Your OAuth access token)' | |
access_token_secret = '(Your OAuth access token secret)' | |
token = OAuth::AccessToken.new( consumer, access_token, access_token_secret ) | |
tmpfile = Dir.tmpdir+'/gplus2twitter' | |
obj = open( "https://www.googleapis.com/plus/v1/people/#{user_id}/activities/public?key=#{api_key}" ){|f| JSON.parse( f.read )} rescue nil | |
exit unless obj | |
if File.exist?( tmpfile ) then | |
prev = open( tmpfile ){|f| Time.parse( f.read )} | |
else | |
prev = Time.now - 600 # 10 minutes if first time | |
end | |
r = [] | |
obj['items'].each do |item| | |
title = item['title'] | |
url = item['url'] | |
date = Time.parse( item['published'] ) | |
if date > prev then | |
r << ["#{title} #{url}", date] | |
end | |
end | |
r.sort_by{|i| i.last}.each do |item, date| | |
res = token.post( 'http://twitter.com/statuses/update.json', 'status' => item ) | |
break if res.code != '200' | |
open( tmpfile, "w" ){|f| f.print date.to_s} | |
end | |
exit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment