twog 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/bin/twog ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'twog'
5
+
6
+ raise "please run 'rake gen_config' to create the ~/.twog.yaml file" unless File.exists?("#{ENV['HOME']}/.twog.yaml")
7
+ conf = YAML.load_file("#{ENV['HOME']}/.twog.yaml")
8
+
9
+ Twog.run(conf)
data/lib/twog.rb ADDED
@@ -0,0 +1,27 @@
1
+ require 'rubygems'
2
+ require 'yaml'
3
+ require 'twog/rss_parser'
4
+ require 'twog/blog_posts_handler'
5
+ require 'twog/twitter_handler'
6
+
7
+
8
+ class Twog
9
+ extend RssParser
10
+ extend BlogPostsHandler
11
+ extend TwitterHandler
12
+
13
+ def self.run(conf)
14
+ posts = parse(conf['rss_feed'])
15
+ posts = get_new_blog_posts(posts, conf['last_blog_post_tweeted'])
16
+ return unless posts && posts.length > 0
17
+ bitly = get_bitly_from(conf)
18
+ tweet(posts, conf, bitly)
19
+ end
20
+
21
+ def self.get_bitly_from(conf)
22
+ bitly_username = conf['bitly_username']
23
+ bitly_api_key = conf['bitly_api_key']
24
+ return nil unless (bitly_username && bitly_api_key)
25
+ Bitly.new(bitly_username, bitly_api_key)
26
+ end
27
+ end
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'twitter_oauth'
3
+ require 'yaml'
4
+
5
+ module BlogPostsHandler
6
+ def get_new_blog_posts(posts, last_blog_post_tweeted)
7
+ return [] unless posts && posts.length > 0
8
+ return posts unless last_blog_post_tweeted
9
+ new_posts = posts.reject { |post| Time.parse(post.updated.content.to_s) <= Time.parse(last_blog_post_tweeted.to_s) }
10
+ end
11
+ end
@@ -0,0 +1,16 @@
1
+ require 'rubygems'
2
+ require 'rss'
3
+
4
+ module RssParser
5
+ def parse(rss_feed_url)
6
+ raise Exception.new('RSS feed missing') unless rss_feed_url
7
+ rss = RSS::Parser.parse(get_content(rss_feed_url), false)
8
+ rss.items
9
+ end
10
+
11
+ def get_content(rss_feed_url)
12
+ open(rss_feed_url) do |f|
13
+ f.read
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,57 @@
1
+ require 'rubygems'
2
+ require 'twitter_oauth'
3
+ require 'yaml'
4
+ require 'rss'
5
+ require 'bitly'
6
+
7
+ class RSS::Atom::Feed::Entry
8
+ def <=>(other_entry)
9
+ this_time = Time.parse(updated.content.to_s)
10
+ other_time = Time.parse(other_entry.updated.content.to_s)
11
+ if this_time < other_time
12
+ -1
13
+ elsif this_time > other_time
14
+ 1
15
+ else
16
+ 0
17
+ end
18
+ end
19
+ end
20
+
21
+ module TwitterHandler
22
+ def tweet(posts, conf, bitly)
23
+ return unless posts && posts.length > 0
24
+ raise Exception.new('OAuth Consumer Key missing') unless conf['consumer_key']
25
+ raise Exception.new('OAuth Consumer Secret missing') unless conf['consumer_secret']
26
+ raise Exception.new('OAuth Access Token missing') unless conf['access_token']
27
+ raise Exception.new('OAuth Access Secret missing') unless conf['access_secret']
28
+ posts.sort.each do |item|
29
+ link = bitly ? bitly.shorten(item.link.href).short_url : item.link.href
30
+ use_twitter_oauth(item, link, conf)
31
+ update_config_file_with_latest_tweet_date(item.updated.content.to_s, conf)
32
+ end
33
+ end
34
+
35
+ def use_twitter_oauth(item, link, conf)
36
+ client = TwitterOAuth::Client.new(
37
+ :consumer_key => conf['consumer_key'],
38
+ :consumer_secret => conf['consumer_secret'],
39
+ :token => conf['access_token'],
40
+ :secret => conf['access_secret']
41
+ )
42
+ raise Exception.new('TwitterOAuth unauthorized') unless client.authorized?
43
+ text = ensure_text_is_of_length(140, item.title.content, link)
44
+ client.update(text)
45
+ end
46
+
47
+ def ensure_text_is_of_length(length, title, link)
48
+ blogged = "blogged:"
49
+ title = title[0,(length-((" "*2).length+blogged.length+link.length))]
50
+ [blogged, title, link].join(' ')
51
+ end
52
+
53
+ def update_config_file_with_latest_tweet_date(last_blog_post_tweeted, conf)
54
+ conf['last_blog_post_tweeted'] = last_blog_post_tweeted
55
+ File.open("#{ENV['HOME']}/.twog.yaml","w") { |out| out.write(conf.to_yaml) }
56
+ end
57
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: twog
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Jason Meridth
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-04-08 00:00:00 -05:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: twitter_oauth
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :runtime
31
+ version_requirements: *id001
32
+ - !ruby/object:Gem::Dependency
33
+ name: bitly
34
+ prerelease: false
35
+ requirement: &id002 !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ segments:
40
+ - 0
41
+ version: "0"
42
+ type: :runtime
43
+ version_requirements: *id002
44
+ description:
45
+ email: jmeridth@gmail.com
46
+ executables:
47
+ - twog
48
+ extensions: []
49
+
50
+ extra_rdoc_files: []
51
+
52
+ files:
53
+ - bin/twog
54
+ - lib/twog/blog_posts_handler.rb
55
+ - lib/twog/rss_parser.rb
56
+ - lib/twog/twitter_handler.rb
57
+ - lib/twog.rb
58
+ has_rdoc: true
59
+ homepage: http://github.com/armmer/twog
60
+ licenses: []
61
+
62
+ post_install_message:
63
+ rdoc_options: []
64
+
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ segments:
79
+ - 0
80
+ version: "0"
81
+ requirements: []
82
+
83
+ rubyforge_project:
84
+ rubygems_version: 1.3.6
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: Tool to tweet blog posts
88
+ test_files: []
89
+