cinch-links-tumblr 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cinch-tumblr.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Brian Haberer
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,64 @@
1
+ # Cinch::Plugins::LinksTumblr
2
+
3
+ This plugin takes all links from the channel and posts them to a Tumblr.
4
+
5
+ You can optionally specify a password if you want to use a private Tumblr.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'cinch-tumblr'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install cinch-tumblr
20
+
21
+ ## Usage
22
+
23
+ You will need to add the Plugin and config to your list first;
24
+
25
+ @bot = Cinch::Bot.new do
26
+ configure do |c|
27
+ c.plugins.plugins = [Cinch::Plugins::LinksTumblr]
28
+ c.plugins.options[Cinch::Plugins::LinksTumblr] = { 'hostname' => 'whatever.tumblr.com',
29
+ 'password' => 'password, if applicable' }
30
+ end
31
+ end
32
+
33
+ However you need to acquire Tumblr oauth credentials in order to make the Plugin work.
34
+ This is easily done using the tumblr-rb gem which is required for this plugin.
35
+
36
+ 1. Run `gem install tumblr-rb`
37
+ 2. Head to http://www.tumblr.com/oauth/apps to register your app and get a key and secret.
38
+ 3. Run `tumblr authorize` it should pop open a window asking for the info you got from
39
+ registering an app.
40
+ 4. You will now have a file in ~/.tumblr with the info you need for the next step.
41
+
42
+ Once you have your Tumblr credentials, you need to add them to the configuration:
43
+
44
+ @bot = Cinch::Bot.new do
45
+ configure do |c|
46
+ c.plugins.plugins = [Cinch::Plugins::LinksTumblr]
47
+ c.plugins.options[Cinch::Plugins::LinksTumblr] = { 'hostname' => 'whatever.tumblr.com',
48
+ 'password' => 'password, if applicable',
49
+ 'consumer_key' => CONSUMER_KEY,
50
+ 'consumer_secret' => CONSUMER_SECRET,
51
+ 'token' => TOKEN,
52
+ 'token_secret' => TOKEN_SECRET }
53
+ end
54
+ end
55
+
56
+ That should be all you need to get the Plugin working!
57
+
58
+ ## Contributing
59
+
60
+ 1. Fork it
61
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
62
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
63
+ 4. Push to the branch (`git push origin my-new-feature`)
64
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cinch/plugins/links-tumblr/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "cinch-links-tumblr"
8
+ gem.version = Cinch::Plugins::LinksTumblr::VERSION
9
+ gem.authors = ["Brian Haberer"]
10
+ gem.email = ["bhaberer@gmail.com"]
11
+ gem.description = %q{Cinch gem that logs every link posted in the channel to a Tumblr}
12
+ gem.summary = %q{Cinch Tumblr Plugin}
13
+ gem.homepage = "https://github.com/bhaberer/cinch-links-tumblr"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency('cinch-storage', '>= 0.0.1')
21
+ gem.add_dependency('cinch-toolbox', '>= 0.0.5')
22
+ gem.add_dependency('cinch', '>= 2.0.0')
23
+ gem.add_dependency('tumblr-rb', '>= 2.0.0')
24
+ gem.add_dependency('weary', '>= 1.1.3')
25
+ end
@@ -0,0 +1,2 @@
1
+ require 'cinch/plugins/links-tumblr/version'
2
+ require 'cinch/plugins/links-tumblr/links-tumblr'
@@ -0,0 +1,132 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'nokogiri'
3
+ require 'open-uri'
4
+ require 'tumblr'
5
+ require 'cinch-storage'
6
+ require 'cinch-toolbox'
7
+
8
+ module Cinch::Plugins
9
+ class LinksTumblr
10
+ include Cinch::Plugin
11
+
12
+ listen_to :channel
13
+ match /tumblr/
14
+
15
+ self.help = 'Use .tumblr for the url and password (if any) for the channel\'s tumblr.'
16
+
17
+
18
+ def initialize(*args)
19
+ super
20
+ @storage = CinchStorage.new('yaml/tumblr.yaml')
21
+ @storage.data[:history] ||= Hash.new
22
+ @hostname = config['hostname'] || nil
23
+ @password = config['password'] || nil
24
+ @creds = { :consumer_key => config['consumer_key'],
25
+ :consumer_secret => config['consumer_secret'],
26
+ :token => config['token'],
27
+ :token_secret => config['token_secret'] }
28
+ end
29
+
30
+ def execute(m)
31
+ if m.channel.nil?
32
+ # Tumblr's are channel bound, so require the user to be in a channel
33
+ m.user.msg "You must use that command in the main channel."
34
+ return
35
+ else
36
+ if @hostname
37
+ msg = "Links are available @ http://#{@hostname}"
38
+ msg << " Password: #{@password}" unless @password.nil?
39
+ m.user.send msg
40
+ else
41
+ debug "ERROR: Tumblr hostname has not been specified, see docs for info."
42
+ end
43
+ end
44
+ end
45
+
46
+ def listen(m)
47
+ urls = URI.extract(m.message, ["http", "https"])
48
+ urls.each do |url|
49
+ @storage.data[:history][m.channel.name] ||= Hash.new
50
+
51
+ # Check to see if we've seen the link
52
+ unless @storage.data[:history][m.channel.name].key?(url)
53
+ short_url = Cinch::Toolbox.shorten(url)
54
+ title = Cinch::Toolbox.get_page_title(url)
55
+ tumble(url, title, m.user.nick)
56
+
57
+ # Store the links to try and cut down on popular urls getting tumbled 20 times
58
+ @storage.data[:history][m.channel.name][url] = { :time => Time.now }
59
+ end
60
+ end
61
+
62
+ if urls
63
+ synchronize(:save_links) do
64
+ @storage.save
65
+ end
66
+ end
67
+ end
68
+
69
+ private
70
+
71
+ def tumble(url, title, nick)
72
+ # Redit
73
+ if redit = url.match(/^https?:\/\/.*imgur\.com.*\/([A-Za-z0-9]+\.\S{3})/)
74
+ post_image("http://i.imgur.com/#{redit[1]}", title, nick)
75
+ # Images
76
+ elsif url.match(/\.jpg|jpeg|gif|png$/i)
77
+ post_image(url, title, nick)
78
+ # Youtube / Vimeo
79
+ elsif url.match(/https?:\/\/[^\/]*\.?(youtube|youtu|vimeo)\./)
80
+ post_video(url, nil, nick)
81
+ # Everything else
82
+ else
83
+ post_link(url, title, nick)
84
+ end
85
+ end
86
+
87
+ def post_link(url, title = nil, nick = nil)
88
+ document = tumblr_header('link', { 'name' => title, 'tags' => nick })
89
+ document << url
90
+ tublr_post(document)
91
+ end
92
+
93
+ def post_quote(quote, source, nick = nil)
94
+ document = tumblr_header('quote', { 'source' => source, 'tags' => [nick, 'twitter'] })
95
+ document << quote
96
+ tublr_post(document)
97
+ end
98
+
99
+ def post_image(url, title = nil, nick = nil)
100
+ document = tumblr_header('text', { 'title' => title, 'tags' => [nick, 'image'] })
101
+ document << "<p><a href='#{url}'><img src='#{url}' style='max-width: 650px;'/></a><br/><a href='#{url}'>#{url}</a></p>"
102
+ tublr_post(document)
103
+ end
104
+
105
+ def post_video(url, title, nick = nil)
106
+ document = tumblr_header('video', { 'caption' => title, 'tags' => [nick, 'video'] })
107
+ document << url
108
+ tublr_post(document)
109
+ end
110
+
111
+ def tumblr_header(type = 'text', options = {})
112
+ opts = { 'type' => type, 'hostname' => @hostname }.update(options)
113
+ doc = YAML::dump(opts)
114
+ doc << "---\n"
115
+ return doc
116
+ end
117
+
118
+ def tublr_post(doc)
119
+ client = Tumblr::Client.new(@hostname, @creds)
120
+ post = Tumblr::Post.load(doc)
121
+ request = post.post(client)
122
+
123
+ request.perform do |response|
124
+ if response.success?
125
+ debug "Successfully posted to Tumblr"
126
+ else
127
+ debug "Something went wrong posting to Tumblr #{response.code} #{response.message}"
128
+ end
129
+ end
130
+ end
131
+ end
132
+ end
@@ -0,0 +1,7 @@
1
+ module Cinch
2
+ module Plugins
3
+ class LinksTumblr
4
+ VERSION = "0.0.1"
5
+ end
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cinch-links-tumblr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Brian Haberer
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-19 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: cinch-storage
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 0.0.1
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 0.0.1
30
+ - !ruby/object:Gem::Dependency
31
+ name: cinch-toolbox
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 0.0.5
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 0.0.5
46
+ - !ruby/object:Gem::Dependency
47
+ name: cinch
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: 2.0.0
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 2.0.0
62
+ - !ruby/object:Gem::Dependency
63
+ name: tumblr-rb
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: 2.0.0
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: 2.0.0
78
+ - !ruby/object:Gem::Dependency
79
+ name: weary
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: 1.1.3
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: 1.1.3
94
+ description: Cinch gem that logs every link posted in the channel to a Tumblr
95
+ email:
96
+ - bhaberer@gmail.com
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files: []
100
+ files:
101
+ - .gitignore
102
+ - Gemfile
103
+ - LICENSE.txt
104
+ - README.md
105
+ - Rakefile
106
+ - cinch-links-tumblr.gemspec
107
+ - lib/cinch-links-tumblr.rb
108
+ - lib/cinch/plugins/links-tumblr/links-tumblr.rb
109
+ - lib/cinch/plugins/links-tumblr/version.rb
110
+ homepage: https://github.com/bhaberer/cinch-links-tumblr
111
+ licenses: []
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ none: false
118
+ requirements:
119
+ - - ! '>='
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ none: false
124
+ requirements:
125
+ - - ! '>='
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ requirements: []
129
+ rubyforge_project:
130
+ rubygems_version: 1.8.24
131
+ signing_key:
132
+ specification_version: 3
133
+ summary: Cinch Tumblr Plugin
134
+ test_files: []