Skip to content

Commit

Permalink
adding oAuth for Twitter
Browse files Browse the repository at this point in the history
  • Loading branch information
ttscoff committed Jul 5, 2013
1 parent d3850ee commit f8b2700
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 38 deletions.
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
source 'https://rubygems.org'

gem 'feed-normalizer'
gem 'twitter'
gem 'twitter_oauth'
gem 'system_timer'

12 changes: 12 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
GEM
remote: https://rubygems.org/
specs:
faraday (0.8.7)
multipart-post (~> 1.1)
feed-normalizer (1.5.2)
hpricot (>= 0.6)
simple-rss (>= 1.1)
hpricot (0.8.6)
multi_json (1.7.7)
multipart-post (1.2.0)
simple-rss (1.2.3)
simple_oauth (0.2.0)
system_timer (1.2.4)
twitter (4.8.1)
faraday (~> 0.8, < 0.10)
multi_json (~> 1.0)
simple_oauth (~> 0.2)

PLATFORMS
ruby

DEPENDENCIES
feed-normalizer
system_timer
twitter
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,12 @@ Slogger indexes various public social services and creates Day One (<http://dayo
## Install ##

1. Download and unzip (or clone using git) the Slogger project. It can be stored in your home directory, a scripts folder or anywhere else on your drive.
2. Default plugins are stored in `/plugins/`, additional plugins are usually found in `/plugins_disabled/`. Plugins are enabled and disabled by adding/removing them from the `/plugins/` folder. Move any additional plugins you want to use into `/plugins/` and disable any other plugins by moving them from `/plugins/` to `plugins_disabled`. (Plugins that are found in `plugins` but not configured will not break anything, but you'll see warnings when run.)
3. From within the Slogger folder, run `./slogger --update-config` to create the initial configuration file. If this doesn't work, you may need to make the file executable: `chmod a+x slogger` from within the Slogger folder. Note that any time you add new plugins or update existing ones, you'll want to run `./slogger --update-config` to ensure that your available options are up to date.
4. Edit the file `slogger_config` that shows up in your Slogger folder
2. From the command line, change to the Slogger folder and run the following commands:
sudo gem install bundle
bundle install
3. Default plugins are stored in `/plugins/`, additional plugins are usually found in `/plugins_disabled/`. Plugins are enabled and disabled by adding/removing them from the `/plugins/` folder. Move any additional plugins you want to use into `/plugins/` and disable any other plugins by moving them from `/plugins/` to `plugins_disabled`. (Plugins that are found in `plugins` but not configured will not break anything, but you'll see warnings when run.)
4. From within the Slogger folder, run `./slogger --update-config` to create the initial configuration file. If this doesn't work, you may need to make the file executable: `chmod a+x slogger` from within the Slogger folder. Note that any time you add new plugins or update existing ones, you'll want to run `./slogger --update-config` to ensure that your available options are up to date.
5. Edit the file `slogger_config` that shows up in your Slogger folder
- The required options will be 'storage:', 'image_filename_is_title:', 'date_format:' and 'time_format:'
- storage: should be one of
- 'icloud'
Expand All @@ -73,8 +76,9 @@ Slogger indexes various public social services and creates Day One (<http://dayo
- image_filename_is_title: should be set to true or false. If true, it will use the base filename (without extension) as the title of images imported individually.
- date_format and time_format should be set to your preferred style (strftime)

5. Edit additional configuration options for any plugins defined. The config file is formatted as YAML, and your options need to conform to that syntax. For the most part, you can just maintain the formatting (quotes, dashes, brackets, etc.) of the default settings when updating.
6. Next time you run `./slogger`, it will execute the enabled and configured plugins and generate your journal entries.
6. Edit additional configuration options for any plugins defined. The config file is formatted as YAML, and your options need to conform to that syntax. For the most part, you can just maintain the formatting (quotes, dashes, brackets, etc.) of the default settings when updating.
- **Note:** Some plugins have options that will be filled in automatically. For example, the Twitter plugin requires you to log in on the command line and enter a PIN, after which it completes the authorization and saves your token to the configuration. If you install a plugin which requires oAuth, be sure to run Slogger from the command line with "./slogger -o plugin_name" once to complete the login procedure and save your credentials.
7. Next time you run `./slogger`, it will execute the enabled and configured plugins and generate your journal entries.

## Usage ##

Expand Down Expand Up @@ -124,7 +128,7 @@ When developing plugins you can create a directory called 'plugins_develop' in t

`@log` is a global logger object. use `@log.info("Message")` (or `warn`/`error`/`fatal`) to generate log messages using the default formatter.

`@config` is the global configuration object. Your plugin settings will be stored under `@config[PluginClassName]`.
`@config` is the global configuration object. Your plugin settings will be stored under `@config[PluginClassName]`. If you return the config object at the end of your do_log function, any modifications will be stored (e.g. for saving oAuth tokens).

`$options` contains options parsed from the command line. Use `$options[:optionname]` to read the setting.

Expand Down
2 changes: 1 addition & 1 deletion plugins/flickrlogger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
'flickr_description' => [
'Logs today\'s photos from Flickr.',
'flickr_ids is an array of one or more IDs',
'flickr_datetype can be the "upload" or "taken" date that has tpo be used',
'flickr_datetype can be the "upload" or "taken" date to be used',
'Get your Flickr ID at <http://idgettr.com/>',
'Get your Flickr API key at <http://www.flickr.com/services/apps/create/noncommercial/>'],
'flickr_api_key' => '',
Expand Down
99 changes: 72 additions & 27 deletions plugins/twitterlogger.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
=begin
Plugin: Twitter Logger
Version: 2.0
Version: 3.0
Description: Logs updates and favorites for specified Twitter users
Author: [Brett Terpstra](http://brettterpstra.com)
Configuration:
Expand All @@ -20,19 +20,24 @@
'save_images_from_favorites (true/false) determines whether TwitterLogger will download images for the favorites of the given usernames and include them in the entry',
'save_retweets (true/false) determines whether TwitterLogger will look for the retweets of the given usernames and include them in the entry',
'save_images_from_retweets (true/false) determines whether TwitterLogger will download images for the retweets of the given usernames and include them in the entry',
'droplr_domain: if you have a custom droplr domain, enter it here, otherwise leave it as d.pr '],
'droplr_domain: if you have a custom droplr domain, enter it here, otherwise leave it as d.pr ',
'oauth_token and oauth_secret should be left blank and will be filled in by the plugin'],
'twitter_users' => [],
'save_favorites' => true,
'save_images' => true,
'save_images_from_favorites' => true,
'save_retweets' => true,
'save_images_from_retweets' => true,
'droplr_domain' => 'd.pr',
'twitter_tags' => '#social #twitter'
'twitter_tags' => '#social #twitter',
'oauth_token' => '',
'oauth_token_secret' => '',
'exclude_replies' => true
}
$slog.register_plugin({ 'class' => 'TwitterLogger', 'config' => config })

require 'rexml/document'
require 'twitter'
require 'twitter_oauth'

class TwitterLogger < Slogger

Expand Down Expand Up @@ -68,50 +73,55 @@ def download_images(images)

def get_tweets(user,type='timeline')
@log.info("Getting Twitter #{type} for #{user}")

Twitter.configure do |auth_config|
auth_config.consumer_key = "53aMoQiFaQfoUtxyJIkGdw"
auth_config.consumer_secret = "Twnh3SnDdtQZkJwJ3p8Tu5rPbL5Gt1I0dEMBBtQ6w"
auth_config.oauth_token = @twitter_config["oauth_token"]
auth_config.oauth_token_secret = @twitter_config["oauth_token_secret"]
end

case type

when 'favorites'
url = URI.parse("http://api.twitter.com/1/favorites.xml?count=200&screen_name=#{user}&include_entities=true&count=200")
params = { "count" => 250, "screen_name" => user, "include_entities" => true }
tweet_obj = Twitter.favorites(params)

when 'timeline'
url = URI.parse("http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=#{user}&count=200&exclude_replies=true&include_entities=true")
params = { "count" => 250, "screen_name" => user, "include_entities" => true, "exclude_replies" => @twitter_config['exclude_replies']}
tweet_obj = Twitter.user_timeline(params)

when 'retweets'
url = URI.parse("http://api.twitter.com/1/statuses/retweeted_by_user.xml?screen_name=#{user}&count=200&include_entities=true")
params = { "count" => 250, "screen_name" => user, "include_entities" => true }
tweet_obj = Twitter.retweeted_by_user(params)

end

tweets = []
images = []
tweets = []
begin
begin
res = Net::HTTP.get_response(url).body
rescue Exception => e
@log.warn("Failure getting response from Twitter")
# p e
return false
end
REXML::Document.new(res).elements.each("statuses/status") { |tweet|
tweet_obj.each { |tweet|
today = @timespan
tweet_date = Time.parse(tweet.elements['created_at'].text)
tweet_date = tweet.created_at
break if tweet_date < today
tweet_text = tweet.elements['text'].text.gsub(/\n/,"\n\t")
tweet_text = tweet.text.gsub(/\n/,"\n\t")
if type == 'favorites'
# TODO: Prepend favorite's username/link
screen_name = tweet.elements['user/screen_name'].text
screen_name = tweet.user.status.retweeted_status.user.screen_name
tweet_text = "[#{screen_name}](http://twitter.com/#{screen_name}): #{tweet_text}"
end
tweet_id = tweet.elements['id'].text
unless tweet.elements['entities/urls'].nil? || tweet.elements['entities/urls'].length == 0
tweet.elements.each("entities/urls/url") { |url|
tweet_text.gsub!(/#{url.elements['url'].text}/,"[#{url.elements['display_url'].text}](#{url.elements['expanded_url'].text})")
tweet_id = tweet.id
unless tweet.urls.empty?
tweet.urls.each { |url|
tweet_text.gsub!(/#{url.url}/,"[#{url.display_url}](#{url.expanded_url})")
}
end
begin
if @twitter_config['save_images']
tweet_images = []
unless tweet.elements['entities/media'].nil? || tweet.elements['entities/media'].length == 0
tweet.elements.each("entities/media/creative") { |img|
tweet_images << { 'content' => tweet_text, 'date' => tweet_date.utc.iso8601, 'url' => img.elements['media_url'].text }
unless tweet.media.empty?
tweet.media.each { |img|
tweet_images << { 'content' => tweet_text, 'date' => tweet_date.utc.iso8601, 'url' => img.media_url }
}
end

Expand Down Expand Up @@ -153,6 +163,7 @@ def get_tweets(user,type='timeline')
@log.warn("Failure gathering image urls")
p e
end

if tweet_images.empty? or !@twitter_config["save_images_from_#{type}"]
tweets.push("* [[#{tweet_date.strftime(@time_format)}](https://twitter.com/#{user}/status/#{tweet_id})] #{tweet_text}")
else
Expand Down Expand Up @@ -188,11 +199,43 @@ def do_log
return
end

if @twitter_config['oauth_token'] == '' || @twitter_config['oauth_token_secret'] == ''
client = TwitterOAuth::Client.new(
:consumer_key => "53aMoQiFaQfoUtxyJIkGdw",
:consumer_secret => "Twnh3SnDdtQZkJwJ3p8Tu5rPbL5Gt1I0dEMBBtQ6w"
)

request_token = client.authentication_request_token(
:oauth_callback => 'oob'
)
@log.info("Twitter requires configuration, please run from the command line and follow the prompts")
puts
puts "------------- Twitter Configuration --------------"
puts "Slogger will now open an authorization page in your default web browser. Copy the code you receive and return here."
print "Press Enter to continue..."
gets
%x{open "#{request_token.authorize_url}"}
print "Paste the code you received here: "
code = gets.strip

access_token = client.authorize(
request_token.token,
request_token.secret,
:oauth_verifier => code
)
if client.authorized?
@twitter_config['oauth_token'] = access_token.params["oauth_token"]
@twitter_config['oauth_token_secret'] = access_token.params["oauth_token_secret"]
puts
log.info("Twitter successfully configured, run Slogger again to continue")
return @twitter_config
end
end
@twitter_config['save_images'] ||= true
@twitter_config['droplr_domain'] ||= 'd.pr'

sl = DayOne.new
@twitter_config['twitter_tags'] ||= ''
@twitter_config['twitter_tags'] ||= '#social #twitter'
tags = "\n\n#{@twitter_config['twitter_tags']}\n" unless @twitter_config['twitter_tags'] == ''

@twitter_config['twitter_users'].each do |user|
Expand Down Expand Up @@ -224,6 +267,8 @@ def do_log
sl.to_dayone({'content' => retweets})
end
end

return @twitter_config
end

def try(&action)
Expand Down
6 changes: 3 additions & 3 deletions slogger
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
# Copyright 2012, Brett Terpstra
# http://brettterpstra.com
# --------------------
MAJOR_VERSION = 2.0
MINOR_VERSION = 18
BUILD_NUMBER = 1
MAJOR_VERSION = 2.1
MINOR_VERSION = 0
BUILD_NUMBER = 0

init_env = ENV['SLOGGER_NO_INITIALIZE'].to_s
ENV['SLOGGER_NO_INITIALIZE'] = "false"
Expand Down
3 changes: 2 additions & 1 deletion slogger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@
require 'optparse'
require 'fileutils'
require 'rexml/parsers/pullparser'
require 'rubygems'

SLOGGER_HOME = File.dirname(File.expand_path(__FILE__))
ENV['SLOGGER_HOME'] = SLOGGER_HOME

require SLOGGER_HOME + '/lib/sociallogger'
require SLOGGER_HOME + '/lib/configtools'
require SLOGGER_HOME + '/lib/json'
# require SLOGGER_HOME + '/lib/json'

class String
def markdownify
Expand Down

0 comments on commit f8b2700

Please sign in to comment.