Created
June 2, 2013 13:36
-
-
Save chagel/5693675 to your computer and use it in GitHub Desktop.
Sync Google+ to Twitter, including post/photo/article sharing etc.
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
require 'google_plus' | |
require 'twitter' | |
require 'time' | |
require 'open-uri' | |
GooglePlus.api_key = '111' | |
UID = 111 | |
Twitter.configure do |config| | |
config.consumer_key = '111' | |
config.consumer_secret = '111' | |
config.oauth_token = '111' | |
config.oauth_token_secret = '111' | |
end | |
SYNC_FILE = "#{File.expand_path(File.dirname(__FILE__))}/last.log" | |
person = GooglePlus::Person.get(UID) | |
activities = person.list_activities if person | |
items = activities.items if activities | |
items.first(5).reverse.each do |item| | |
verb = item.verb | |
title = item.title | |
link = item.url | |
attachments = item.attributes['object']['attachments'] | |
photo = nil | |
attachments.each do |attachment| | |
case attachment['objectType'] | |
when 'article' | |
title = title + " " + attachment['url'] | |
when 'photo' | |
OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE | |
photo = open(attachment['image']['url']) | |
end | |
end if attachments | |
published_at = Time.parse(item.attributes['published']) | |
last_sync_at = if File.exists?(SYNC_FILE) | |
Marshal.load(File.read(SYNC_FILE)) | |
else | |
Time.utc(2013, 5, 17, 12) | |
end | |
if verb == 'post' && published_at > last_sync_at | |
puts "Tweeting.. #{link}" | |
if photo | |
Twitter.update_with_media title, photo | |
else | |
Twitter.update title | |
end | |
File.open(SYNC_FILE, 'w') do |f| | |
f.write Marshal.dump(published_at) | |
end | |
end | |
end if items |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment