cinch-twitter 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +5 -0
- data/README.md +8 -9
- data/VERSION +1 -1
- data/cinch-twitter.gemspec +4 -2
- data/lib/cinch/plugins/twitter/error_handler.rb +1 -1
- data/lib/cinch/plugins/twitter/formatter.rb +12 -11
- data/lib/cinch/plugins/twitter/tweet_handler.rb +52 -50
- data/lib/cinch/plugins/twitter.rb +57 -60
- metadata +5 -3
data/CHANGELOG.md
ADDED
data/README.md
CHANGED
@@ -3,8 +3,6 @@ cinch-twitter
|
|
3
3
|
|
4
4
|
A Cinch plugin for accessing Twitter, using [John Nunemaker's](https://github.com/jnunemaker) wonderful [Twitter gem](https://github.com/jnunemaker/twitter).
|
5
5
|
|
6
|
-
Requiring the gem: `require 'cinch/plugins/twitter'`
|
7
|
-
|
8
6
|
Usage
|
9
7
|
-----
|
10
8
|
|
@@ -16,17 +14,18 @@ Usage
|
|
16
14
|
Shorthand commands are also available:
|
17
15
|
* `@[username]<+D>`, @#[id]
|
18
16
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
17
|
+
Requiring and including the plugin
|
18
|
+
----------------------------------
|
19
|
+
require 'cinch/plugins/twitter'
|
20
|
+
# ...
|
21
|
+
Bot.config {|c|
|
22
|
+
# ...
|
23
|
+
c.plugins.plugins << Cinch::Plugins::Twitter
|
25
24
|
|
26
25
|
Configuration
|
27
26
|
-------------
|
28
27
|
|
29
|
-
To configure the plugin, you must insert your access keys for the client.
|
28
|
+
To configure the plugin, you must insert your access keys for the client.
|
30
29
|
|
31
30
|
To retrieve your access keys and `oauth` tokens, if you already have an application set up, please visit https://dev.twitter.com/apps, otherwise visit https://dev.twitter.com/apps/new and follow the instructions.
|
32
31
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/cinch-twitter.gemspec
CHANGED
@@ -5,19 +5,21 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "cinch-twitter"
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.2.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Mark Seymour"]
|
12
|
-
s.date = "2012-06-
|
12
|
+
s.date = "2012-06-28"
|
13
13
|
s.description = "A Twitter plugin for Cinch."
|
14
14
|
s.email = "mark.seymour.ns@gmail.com"
|
15
15
|
s.extra_rdoc_files = [
|
16
|
+
"CHANGELOG.md",
|
16
17
|
"LICENSE.txt",
|
17
18
|
"README.md"
|
18
19
|
]
|
19
20
|
s.files = [
|
20
21
|
".document",
|
22
|
+
"CHANGELOG.md",
|
21
23
|
"Gemfile",
|
22
24
|
"Gemfile.lock",
|
23
25
|
"LICENSE.txt",
|
@@ -1,22 +1,23 @@
|
|
1
1
|
# coding: utf-8
|
2
2
|
require 'cgi'
|
3
|
+
require 'cinch/formatting'
|
3
4
|
|
4
5
|
module Cinch
|
5
6
|
module Plugins
|
6
|
-
|
7
|
+
class Twitter
|
7
8
|
module Formatter
|
8
9
|
|
9
10
|
def format_tweet(tweet)
|
10
11
|
tweet_text = expand_uris(tweet.text, tweet.attrs["entities"]["urls"])
|
11
12
|
parts, head, body, tail, urls = [], [], [], [], []
|
12
|
-
head =
|
13
|
+
head = Cinch::Formatting.format(:bold,"#{tweet.user.screen_name} »")
|
13
14
|
body << CGI::unescapeHTML(tweet_text.gsub("\n", " ").squeeze(" "))
|
14
|
-
body <<
|
15
|
+
body << Cinch::Formatting.format(:aqua,"*twoosh*") if tweet.text.length == 140
|
15
16
|
tail << "From #{tweet.place.full_name}" if !tweet.place.blank?
|
16
17
|
tail << "at #{tweet.created_at.strftime("%B %-d, %Y, %-I:%m%P")}"
|
17
18
|
tail << "via #{tweet.source.gsub( %r{</?[^>]+?>}, '' )}"
|
18
19
|
urls << "https://twitter.com/#{tweet.user.screen_name}"
|
19
|
-
urls <<
|
20
|
+
urls << Cinch::Formatting.format(:grey,"in reply to") if !tweet.in_reply_to_screen_name.blank?
|
20
21
|
urls << "http://twitter.com/#{tweet.in_reply_to_screen_name}#{"/statuses/#{tweet.in_reply_to_status_id.to_s}" if !tweet.in_reply_to_status_id.blank?}" if !tweet.in_reply_to_screen_name.blank?
|
21
22
|
parts = [head, body, ["(", tail.join(" "), ")"].join, urls].flatten
|
22
23
|
parts.join(" ")
|
@@ -25,9 +26,9 @@ module Cinch
|
|
25
26
|
def format_search(tweet)
|
26
27
|
tweet_text = expand_uris(tweet.text, tweet.attrs["entities"]["urls"])
|
27
28
|
parts, head, body, tail, urls = [], [], [], [], []
|
28
|
-
head =
|
29
|
+
head = Cinch::Formatting.format(:bold,"#{tweet.from_user} »")
|
29
30
|
body << CGI::unescapeHTML(tweet_text.gsub("\n", " ").squeeze(" "))
|
30
|
-
body <<
|
31
|
+
body << Cinch::Formatting.format(:aqua,"*twoosh*") if tweet.text.length == 140
|
31
32
|
tail << "at #{tweet.created_at.strftime("%B %-d, %Y, %-I:%m%P")}"
|
32
33
|
urls << "https://twitter.com/#{tweet.from_user}"
|
33
34
|
parts = [head, body, ["(", tail.join(" "), ")"].join, urls].flatten
|
@@ -36,11 +37,11 @@ module Cinch
|
|
36
37
|
|
37
38
|
def format_tweep_info(tweep)
|
38
39
|
tweep_status_text = expand_uris(tweep.status.text, tweep.status.attrs["entities"]["urls"])
|
39
|
-
head = "#{
|
40
|
+
head = "#{Cinch::Formatting.format(:aqua,tweep.name)}" + Cinch::Formatting.format(:silver," (#{tweep.screen_name})") + Cinch::Formatting.format(:grey," - #{tweep.url} https://twitter.com/#{tweep.screen_name}")
|
40
41
|
bio = ""
|
41
|
-
bio =
|
42
|
+
bio = Cinch::Formatting.format(:aqua,"\"#{tweep.description.strip}\"") if !tweep.description.blank?
|
42
43
|
location = ""
|
43
|
-
location = "They are from #{
|
44
|
+
location = "They are from #{Cinch::Formatting.format(:aqua,tweep.location.strip)}" if !tweep.location.blank?
|
44
45
|
desc = [] << "has made #{tweep.statuses_count} tweets since #{tweep.created_at.strftime("%B %-d, %Y")}"
|
45
46
|
desc << "follows #{tweep.friends_count} tweeps" if tweep.friends_count > 0
|
46
47
|
desc << "has #{tweep.followers_count} followers" if tweep.followers_count > 0
|
@@ -51,14 +52,14 @@ module Cinch
|
|
51
52
|
flags << "is a translator for Twitter" if tweep.is_translator?
|
52
53
|
flags << "is verified" if tweep.verified?
|
53
54
|
flags << "would rather keep their life secret" if tweep.protected?
|
54
|
-
tweet = [] <<
|
55
|
+
tweet = [] << Cinch::Formatting.format(:aqua,"Their latest tweet:")
|
55
56
|
tweet << CGI::unescapeHTML(tweep_status_text.gsub("\n", " ").squeeze(" "))
|
56
57
|
tweet_tail = []
|
57
58
|
tweet_tail << "from #{tweep.status.place.full_name}" if !tweep.status.place.blank?
|
58
59
|
tweet_tail << "at #{tweep.status.created_at.strftime("%B %-d, %Y, %-I:%m%P")}"
|
59
60
|
|
60
61
|
parts = [head, bio, location, desc, flags].reject(&:blank?).map {|e| e.is_a?(Array) ? "#{tweep.name} " + e.to_sentence + "." : e }
|
61
|
-
parts << [tweet,
|
62
|
+
parts << [tweet, Cinch::Formatting.format(:silver,["(", tweet_tail.join(" "), ")"].join)].join(" ")
|
62
63
|
parts.join("\n")
|
63
64
|
end
|
64
65
|
|
@@ -4,69 +4,71 @@ require 'cinch/plugins/twitter/error_handler'
|
|
4
4
|
|
5
5
|
module Cinch
|
6
6
|
module Plugins
|
7
|
-
|
7
|
+
class Twitter
|
8
8
|
module TweetHandler
|
9
|
-
include Formatter
|
10
|
-
include ErrorHandler
|
11
|
-
|
12
|
-
EXCEPTIONS = [::Twitter::Error, ErrorHandler::Warnings]
|
13
|
-
AMessage = Struct.new(:message,:type)
|
14
|
-
|
15
9
|
# Handler methods
|
10
|
+
class << self
|
11
|
+
include Formatter
|
12
|
+
include ErrorHandler
|
16
13
|
|
17
|
-
|
18
|
-
|
19
|
-
begin
|
20
|
-
raise Warnings::TooManyTweets if params[:nth_tweet].to_i > 20
|
21
|
-
timeline = ::Twitter.user_timeline params[:username], include_entities: true, include_rts: true, count: params[:nth_tweet].to_i.succ
|
22
|
-
raise Warnings::NoTweets if timeline.blank?
|
23
|
-
tweet = timeline.last
|
24
|
-
params[:username] = tweet.user.screen_name if !tweet.user.nil? # For proper case.
|
14
|
+
EXCEPTIONS = [::Twitter::Error, ErrorHandler::Warnings]
|
15
|
+
AMessage = Struct.new(:message,:type)
|
25
16
|
|
26
|
-
|
27
|
-
|
17
|
+
def tweet_by_username(params={})
|
18
|
+
params = { username: "Twitter", nth_tweet: 0 }.merge(params)
|
19
|
+
begin
|
20
|
+
raise Warnings::TooManyTweets if params[:nth_tweet].to_i > 20
|
21
|
+
timeline = ::Twitter.user_timeline params[:username], include_entities: true, include_rts: true, count: params[:nth_tweet].to_i.succ
|
22
|
+
raise Warnings::NoTweets if timeline.blank?
|
23
|
+
tweet = timeline.last
|
24
|
+
params[:username] = tweet.user.screen_name if !tweet.user.nil? # For proper case.
|
28
25
|
|
29
|
-
|
26
|
+
return "No tweets!" if timeline.blank?
|
27
|
+
return "Protected!" if tweet.user.protected?
|
30
28
|
|
31
|
-
|
32
|
-
|
29
|
+
AMessage.new format_tweet(tweet) # The fun starts here. If there is ever a problem, it'll bubble up here and be caught.
|
30
|
+
|
31
|
+
rescue *EXCEPTIONS => ex
|
32
|
+
AMessage.new handle_error(ex, params[:username], @bot.nick), :notice
|
33
|
+
end
|
33
34
|
end
|
34
|
-
end
|
35
35
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
36
|
+
def tweet_by_id(params={})
|
37
|
+
params = {id: 0 }.merge(params)
|
38
|
+
begin
|
39
|
+
tweet = ::Twitter.status params[:id], include_entities: true
|
40
|
+
params[:username] = tweet.user.screen_name if !tweet.user.nil? # For easy access.
|
41
|
+
AMessage.new format_tweet(tweet) # If there is ever a problem, it'll bubble up here and be caught.
|
42
|
+
rescue *EXCEPTIONS => ex
|
43
|
+
AMessage.new handle_error(ex, params[:username], @bot.nick), :notice
|
44
|
+
end
|
44
45
|
end
|
45
|
-
end
|
46
46
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
47
|
+
def tweep_info(params={})
|
48
|
+
params = {username: "Twitter"}.merge(params)
|
49
|
+
begin
|
50
|
+
tweep = ::Twitter.user params[:username], include_entities: true
|
51
|
+
AMessage.new format_tweep_info(tweep)
|
52
|
+
rescue *EXCEPTIONS => ex
|
53
|
+
AMessage.new handle_error(ex, params[:username], @bot.nick), :notice
|
54
|
+
end
|
54
55
|
end
|
55
|
-
end
|
56
56
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
57
|
+
def search_by_term(params={})
|
58
|
+
params = {term: "cat"}.merge(params)
|
59
|
+
begin
|
60
|
+
results = []
|
61
|
+
::Twitter.search(params[:term], include_entities: true, rpp: 3, result_type: "recent").each {|status|
|
62
|
+
params[:username] = status.from_user
|
63
|
+
results << format_search(status)
|
64
|
+
}
|
65
|
+
results << "There are no results for \"#{params[:term]}\"." if results.empty?
|
66
|
+
AMessage.new results.join("\n")
|
67
|
+
rescue *EXCEPTIONS => ex
|
68
|
+
AMessage.new handle_error(ex, params[:username], @bot.nick), :notice
|
69
|
+
end
|
69
70
|
end
|
71
|
+
|
70
72
|
end
|
71
73
|
|
72
74
|
end
|
@@ -7,80 +7,77 @@ require 'cinch/plugins/twitter/tweet_handler'
|
|
7
7
|
|
8
8
|
module Cinch
|
9
9
|
module Plugins
|
10
|
-
|
10
|
+
class Twitter
|
11
|
+
include Cinch::Plugin
|
12
|
+
#include Twitter::TweetHandler
|
11
13
|
|
12
|
-
|
13
|
-
|
14
|
-
|
14
|
+
set(
|
15
|
+
plugin_name: "Twitter",
|
16
|
+
help: "Access Twitter from the comfort of your IRC client! Usage:\n* `!tw <username><+D>` - Gets the latest tweet of the specified user, or the tweet 'D' tweets back, between 1 and 20.\n* `!tw #[id]` - Gets the tweet at the specified ID\n* `?tw [username]` - Gets the specified user's Twitter profile\n* `?ts [term]` - Searches for three of the most recent tweets regarding the specified query\n\nShorthand: `@[username]<+D>`, @#[id]",
|
17
|
+
required_options: [:access_keys])
|
15
18
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
::Twitter.configure do |c|
|
25
|
-
c.consumer_key = keys["consumer_key"]
|
26
|
-
c.consumer_secret = keys["consumer_secret"]
|
27
|
-
c.oauth_token = keys["oauth_token"]
|
28
|
-
c.oauth_token_secret = keys["oauth_token_secret"]
|
29
|
-
end
|
19
|
+
def initialize(*args)
|
20
|
+
super
|
21
|
+
keys = config[:access_keys]
|
22
|
+
::Twitter.configure do |c|
|
23
|
+
c.consumer_key = keys["consumer_key"]
|
24
|
+
c.consumer_secret = keys["consumer_secret"]
|
25
|
+
c.oauth_token = keys["oauth_token"]
|
26
|
+
c.oauth_token_secret = keys["oauth_token_secret"]
|
30
27
|
end
|
28
|
+
end
|
31
29
|
|
32
|
-
|
33
|
-
|
34
|
-
|
30
|
+
def is_notice?(m)
|
31
|
+
m.type == :notice ? true : false
|
32
|
+
end
|
35
33
|
|
36
34
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
end
|
35
|
+
match /tw$/, method: :execute_tweet
|
36
|
+
match /tw (\w+)(?:-(\d+))?$/, method: :execute_tweet
|
37
|
+
match /^@(\w+)(?:-(\d+))?$/, method: :execute_tweet, use_prefix: false
|
38
|
+
def execute_tweet(m, username = nil, nth_tweet = nil)
|
39
|
+
options = {}
|
40
|
+
options[:username] = username unless username.nil?
|
41
|
+
options[:nth_tweet] = nth_tweet unless nth_tweet.nil?
|
42
|
+
result = TweetHandler.tweet_by_username(options)
|
43
|
+
if is_notice?(result)
|
44
|
+
m.user.notice result.message
|
45
|
+
else
|
46
|
+
m.reply result.message
|
50
47
|
end
|
48
|
+
end
|
51
49
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
end
|
50
|
+
match /tw #(\d+)$/, method: :execute_id
|
51
|
+
match /^@#(\d+)$/, method: :execute_id, use_prefix: false
|
52
|
+
def execute_id(m, id)
|
53
|
+
result = TweetHandler.tweet_by_id(id: id)
|
54
|
+
if is_notice?(result)
|
55
|
+
m.user.notice result.message
|
56
|
+
else
|
57
|
+
m.reply result.message
|
61
58
|
end
|
59
|
+
end
|
62
60
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
end
|
61
|
+
match /\?tw (\w+)$/, method: :execute_info, use_prefix: false
|
62
|
+
def execute_info(m, username)
|
63
|
+
result = TweetHandler.tweep_info(username: username)
|
64
|
+
if is_notice?(result)
|
65
|
+
m.user.notice result.message
|
66
|
+
else
|
67
|
+
m.reply result.message
|
71
68
|
end
|
69
|
+
end
|
72
70
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
end
|
71
|
+
match /\?ts (.+)$/, method: :execute_search, use_prefix: false
|
72
|
+
def execute_search(m, term)
|
73
|
+
result = TweetHandler.search_by_term(term: term)
|
74
|
+
if is_notice?(result)
|
75
|
+
m.user.notice result.message
|
76
|
+
else
|
77
|
+
m.reply result.message
|
81
78
|
end
|
82
|
-
|
83
79
|
end
|
80
|
+
|
84
81
|
end
|
85
82
|
end
|
86
83
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cinch-twitter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-06-
|
12
|
+
date: 2012-06-28 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -128,10 +128,12 @@ email: mark.seymour.ns@gmail.com
|
|
128
128
|
executables: []
|
129
129
|
extensions: []
|
130
130
|
extra_rdoc_files:
|
131
|
+
- CHANGELOG.md
|
131
132
|
- LICENSE.txt
|
132
133
|
- README.md
|
133
134
|
files:
|
134
135
|
- .document
|
136
|
+
- CHANGELOG.md
|
135
137
|
- Gemfile
|
136
138
|
- Gemfile.lock
|
137
139
|
- LICENSE.txt
|
@@ -158,7 +160,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
158
160
|
version: '0'
|
159
161
|
segments:
|
160
162
|
- 0
|
161
|
-
hash: -
|
163
|
+
hash: -173112557
|
162
164
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
163
165
|
none: false
|
164
166
|
requirements:
|