twitterstream 0.0.4 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +9 -3
- data/Manifest.txt +3 -3
- data/{README.txt → README.mkd} +28 -24
- data/README.rdoc +2 -54
- data/Rakefile +2 -2
- data/lib/twitter_stream.rb +107 -0
- data/test/test_helper.rb +1 -1
- data/test/{test_twitterstream.rb → test_twitter_stream.rb} +7 -6
- metadata +6 -7
- data/lib/twitterstream.rb +0 -77
data/History.txt
CHANGED
@@ -1,4 +1,10 @@
|
|
1
|
-
=== 0.0
|
1
|
+
=== 0.1.0 2010-01-01
|
2
2
|
|
3
|
-
*
|
4
|
-
*
|
3
|
+
* Fixed some from Sora Harakami
|
4
|
+
* TwitterStream::Client => TwitterStream
|
5
|
+
* require 'twitterStream' => require 'twitter_stream'
|
6
|
+
* TwitterStream#start_stream is to be a private method
|
7
|
+
|
8
|
+
=== 0.0.1 2009-12-31
|
9
|
+
|
10
|
+
* Initial release from yayugu
|
data/Manifest.txt
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
History.txt
|
2
2
|
Manifest.txt
|
3
3
|
README.rdoc
|
4
|
-
README.
|
4
|
+
README.mkd
|
5
5
|
Rakefile
|
6
|
-
lib/
|
6
|
+
lib/twitter_stream.rb
|
7
7
|
script/console
|
8
8
|
script/destroy
|
9
9
|
script/generate
|
10
10
|
test/test_helper.rb
|
11
|
-
test/
|
11
|
+
test/test_twitter_stream.rb
|
data/{README.txt → README.mkd}
RENAMED
@@ -1,44 +1,48 @@
|
|
1
|
-
|
1
|
+
# twitterstream
|
2
2
|
|
3
|
-
* http://rubyforge.org/projects/twitterstream/
|
3
|
+
* [rubyforge - twitterstream](http://rubyforge.org/projects/twitterstream/)
|
4
4
|
|
5
|
-
|
5
|
+
## DESCRIPTION
|
6
6
|
|
7
|
-
It is the simple library to access the Twitter Streaming API(
|
7
|
+
It is the simple library to access the [Twitter Streaming API](http://apiwiki.twitter.com/Streaming-API-Documentation). It works with pure-ruby(don't need C compiler) and rubygems.
|
8
8
|
|
9
|
-
|
9
|
+
## FEATURES/PROBLEMS
|
10
10
|
|
11
|
-
|
11
|
+
## SYNOPSIS
|
12
12
|
|
13
|
-
require "
|
14
|
-
TwitterStream::Client.new("username", "password").sample do |status|
|
15
|
-
next unless status['text']
|
16
|
-
user = status['user']
|
17
|
-
puts "#{user['screen_name']}: #{status['text']}"
|
18
|
-
end
|
13
|
+
require "twitter_stream"
|
19
14
|
|
20
|
-
|
15
|
+
ts = TwitterStream.new(USERNAME,PASSWORD)
|
21
16
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
puts "#{user['screen_name']}: #{status['text']}"
|
27
|
-
end
|
17
|
+
ts.sample do |status|
|
18
|
+
user = status['user']
|
19
|
+
puts "#{user['screen_name']}: #{status['text']}"
|
20
|
+
end
|
28
21
|
|
29
|
-
|
22
|
+
ts.track(['bit','ly']) do |status| # or ts.track("bit,ly")
|
23
|
+
user = status['user']
|
24
|
+
puts "#{user['screen_name']}: #{status['text']}"
|
25
|
+
end
|
26
|
+
|
27
|
+
ts.follow([5161091,66137185]) do |status| # or ts.track("5161091,66137185") or ts.track(["5161091",66137185])
|
28
|
+
user = status['user']
|
29
|
+
puts "#{user['screen_name']}: #{status['text']}"
|
30
|
+
end
|
31
|
+
|
32
|
+
## REQUIREMENTS
|
30
33
|
|
31
34
|
* json
|
32
35
|
|
33
|
-
|
36
|
+
## INSTALL:
|
34
37
|
|
35
|
-
sudo gem install twitterstream
|
38
|
+
sudo gem install twitterstream
|
36
39
|
|
37
|
-
|
40
|
+
## LICENSE:
|
38
41
|
|
39
42
|
(The MIT License)
|
40
43
|
|
41
|
-
Copyright (c)
|
44
|
+
Copyright (c) 2010 Sora Harakami (Modified)
|
45
|
+
Copyright (c) 2009 yayugu
|
42
46
|
|
43
47
|
Permission is hereby granted, free of charge, to any person obtaining
|
44
48
|
a copy of this software and associated documentation files (the
|
data/README.rdoc
CHANGED
@@ -1,62 +1,10 @@
|
|
1
1
|
= twitterstream
|
2
2
|
|
3
|
-
* http://rubyforge.org/projects/twitterstream/
|
4
3
|
|
5
4
|
== DESCRIPTION:
|
6
5
|
|
7
6
|
It is the simple library to access the Twitter Streaming API( http://apiwiki.twitter.com/Streaming-API-Documentation ). It works with pure-ruby(don't need C compiler) and rubygems.
|
8
7
|
|
9
|
-
==
|
8
|
+
== DETAIL:
|
10
9
|
|
11
|
-
|
12
|
-
|
13
|
-
== SYNOPSIS:
|
14
|
-
|
15
|
-
require "twitterStream"
|
16
|
-
TwitterStream::Client.new("username", "password").sample do |status|
|
17
|
-
next unless status['text']
|
18
|
-
user = status['user']
|
19
|
-
puts "#{user['screen_name']}: #{status['text']}"
|
20
|
-
end
|
21
|
-
|
22
|
-
or
|
23
|
-
|
24
|
-
require "twitterStream"
|
25
|
-
TwitterStream::Client.new("username", "password").track("bit,ly") do |status|
|
26
|
-
next unless status['text']
|
27
|
-
user = status['user']
|
28
|
-
puts "#{user['screen_name']}: #{status['text']}"
|
29
|
-
end
|
30
|
-
|
31
|
-
== REQUIREMENTS:
|
32
|
-
|
33
|
-
* json
|
34
|
-
|
35
|
-
== INSTALL:
|
36
|
-
|
37
|
-
sudo gem install twitterstream
|
38
|
-
|
39
|
-
== LICENSE:
|
40
|
-
|
41
|
-
(The MIT License)
|
42
|
-
|
43
|
-
Copyright (c) 2009 FIXME full name
|
44
|
-
|
45
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
46
|
-
a copy of this software and associated documentation files (the
|
47
|
-
'Software'), to deal in the Software without restriction, including
|
48
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
49
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
50
|
-
permit persons to whom the Software is furnished to do so, subject to
|
51
|
-
the following conditions:
|
52
|
-
|
53
|
-
The above copyright notice and this permission notice shall be
|
54
|
-
included in all copies or substantial portions of the Software.
|
55
|
-
|
56
|
-
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
57
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
58
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
59
|
-
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
60
|
-
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
61
|
-
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
62
|
-
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
10
|
+
see README.mkd
|
data/Rakefile
CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
|
|
2
2
|
gem 'hoe', '>= 2.1.0'
|
3
3
|
require 'hoe'
|
4
4
|
require 'fileutils'
|
5
|
-
require './lib/
|
5
|
+
require './lib/twitter_stream'
|
6
6
|
|
7
7
|
Hoe.plugin :newgem
|
8
8
|
# Hoe.plugin :website
|
@@ -13,7 +13,7 @@ Hoe.plugin :newgem
|
|
13
13
|
$hoe = Hoe.spec 'twitterstream' do
|
14
14
|
self.developer 'yayugu', 'yayugu@gmail.com'
|
15
15
|
|
16
|
-
self.rubyforge_name =
|
16
|
+
self.rubyforge_name = "twitterstream" # TODO this is default value
|
17
17
|
self.extra_deps = [['json','>= 1.2.0']]
|
18
18
|
|
19
19
|
end
|
@@ -0,0 +1,107 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__)) unless
|
2
|
+
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
3
|
+
|
4
|
+
require 'net/http'
|
5
|
+
require 'uri'
|
6
|
+
require 'rubygems'
|
7
|
+
require 'json'
|
8
|
+
|
9
|
+
module Net
|
10
|
+
class HTTPResponse
|
11
|
+
def each_line(rs = "\n")
|
12
|
+
stream_check
|
13
|
+
while line = @socket.readuntil(rs)
|
14
|
+
yield line
|
15
|
+
end
|
16
|
+
self
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class TwitterStream
|
22
|
+
VERSION = '0.1.0'
|
23
|
+
@@urls = {
|
24
|
+
'sample' => URI.parse("http://stream.twitter.com/1/statuses/sample.json"),
|
25
|
+
'filter' => URI.parse("http://stream.twitter.com/1/statuses/filter.json")
|
26
|
+
}
|
27
|
+
|
28
|
+
def initialize(username, password)
|
29
|
+
@username = username
|
30
|
+
@password = password
|
31
|
+
self
|
32
|
+
end
|
33
|
+
|
34
|
+
def sample(params=nil)
|
35
|
+
raise ArgumentError, "params is not hash" unless params.nil? || params.kind_of?(Hash)
|
36
|
+
start_stream('sample', params) do |status|
|
37
|
+
yield status
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def filter(params=nil)
|
42
|
+
raise ArgumentError, "params is not hash" unless params.nil? || params.kind_of?(Hash)
|
43
|
+
start_stream('filter', params) do |status|
|
44
|
+
yield status
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def track(track, params=nil)
|
49
|
+
raise ArgumentError, "track is not array or string" unless track.kind_of?(Array) || track.kind_of?(String)
|
50
|
+
raise ArgumentError, "params is not hash" unless params.nil? || params.kind_of?(Hash)
|
51
|
+
|
52
|
+
p = { 'track' => track.kind_of?(Array) ? track.map{|x| raise ArgumentError, "track item is not string or integer!" unless x.kind_of?(String) || x.kind_of?(Integer); x.kind_of?(Integer) ? x.to_s : x }.join(",") : track }
|
53
|
+
|
54
|
+
p.merge!('filter',params) if params
|
55
|
+
start_stream('filter', p) do |status|
|
56
|
+
yield status
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def follow(follow, params=nil)
|
61
|
+
raise ArgumentError, "follow is not array or string" unless follow.kind_of?(Array) || follow.kind_of?(String)
|
62
|
+
raise ArgumentError, "params is not hash" unless params.nil? || params.kind_of?(Hash)
|
63
|
+
|
64
|
+
p = { 'follow' => follow.kind_of?(Array) ? follow.join(",") : follow }
|
65
|
+
p.merge!(params) if params
|
66
|
+
|
67
|
+
start_stream('filter', p) do |status|
|
68
|
+
yield status
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
private
|
73
|
+
|
74
|
+
def start_stream(url, params=nil)
|
75
|
+
raise ArgumentError, "params is not hash" unless params.nil? || params.kind_of?(Hash)
|
76
|
+
raise ArgumentError, "url is not String or URI!" unless url.kind_of?(URI) || url.kind_of?(String)
|
77
|
+
|
78
|
+
if url.kind_of?(URI)
|
79
|
+
uri = url
|
80
|
+
elsif url.kind_of?(String)
|
81
|
+
if @@urls[url]
|
82
|
+
uri = @@urls[url]
|
83
|
+
elsif /^https?:/ =~ url
|
84
|
+
uri = URI.parse(url)
|
85
|
+
else
|
86
|
+
raise ArgumentError, "@@urls['#{url}'] not found"
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
http = Net::HTTP.start(uri.host, uri.port)
|
91
|
+
request = Net::HTTP::Post.new(uri.request_uri)
|
92
|
+
request.set_form_data(params) if params
|
93
|
+
request.basic_auth(@username, @password)
|
94
|
+
|
95
|
+
begin
|
96
|
+
http.request(request) do |response|
|
97
|
+
response.each_line("\r\n") do |line|
|
98
|
+
j = JSON.parse(line) rescue next
|
99
|
+
next unless j["text"]
|
100
|
+
yield j
|
101
|
+
end
|
102
|
+
end
|
103
|
+
ensure
|
104
|
+
http.finish
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
data/test/test_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: twitterstream
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- yayugu
|
@@ -42,21 +42,20 @@ extensions: []
|
|
42
42
|
extra_rdoc_files:
|
43
43
|
- History.txt
|
44
44
|
- Manifest.txt
|
45
|
-
- README.txt
|
46
45
|
files:
|
47
46
|
- History.txt
|
48
47
|
- Manifest.txt
|
49
48
|
- README.rdoc
|
50
|
-
- README.
|
49
|
+
- README.mkd
|
51
50
|
- Rakefile
|
52
|
-
- lib/
|
51
|
+
- lib/twitter_stream.rb
|
53
52
|
- script/console
|
54
53
|
- script/destroy
|
55
54
|
- script/generate
|
56
55
|
- test/test_helper.rb
|
57
|
-
- test/
|
56
|
+
- test/test_twitter_stream.rb
|
58
57
|
has_rdoc: true
|
59
|
-
homepage:
|
58
|
+
homepage:
|
60
59
|
licenses: []
|
61
60
|
|
62
61
|
post_install_message:
|
@@ -85,5 +84,5 @@ signing_key:
|
|
85
84
|
specification_version: 3
|
86
85
|
summary: It is the simple library to access the Twitter Streaming API( http://apiwiki.twitter.com/Streaming-API-Documentation )
|
87
86
|
test_files:
|
88
|
-
- test/
|
87
|
+
- test/test_twitter_stream.rb
|
89
88
|
- test/test_helper.rb
|
data/lib/twitterstream.rb
DELETED
@@ -1,77 +0,0 @@
|
|
1
|
-
$:.unshift(File.dirname(__FILE__)) unless
|
2
|
-
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
3
|
-
|
4
|
-
require 'net/http'
|
5
|
-
require 'uri'
|
6
|
-
require 'rubygems'
|
7
|
-
require 'json'
|
8
|
-
|
9
|
-
module Net
|
10
|
-
class HTTPResponse
|
11
|
-
def each_line(rs = "\n")
|
12
|
-
stream_check
|
13
|
-
while line = @socket.readuntil(rs)
|
14
|
-
yield line
|
15
|
-
end
|
16
|
-
self
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
module TwitterStream
|
22
|
-
VERSION = '0.0.4'
|
23
|
-
|
24
|
-
class Client
|
25
|
-
def initialize(username, password)
|
26
|
-
@username = username
|
27
|
-
@password = password
|
28
|
-
self
|
29
|
-
end
|
30
|
-
|
31
|
-
def start_stream(uri, params=nil)
|
32
|
-
Net::HTTP.start(uri.host, uri.port) do |http|
|
33
|
-
request = Net::HTTP::Post.new(uri.request_uri)
|
34
|
-
request.set_form_data(params) if params
|
35
|
-
request.basic_auth(@username, @password)
|
36
|
-
http.request(request) do |response|
|
37
|
-
response.each_line("\r\n") do |line|
|
38
|
-
status = JSON.parse(line) rescue next
|
39
|
-
yield status
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
def sample(params=nil)
|
46
|
-
uri = URI.parse("http://stream.twitter.com/1/statuses/sample.json")
|
47
|
-
start_stream(uri, params) do |status|
|
48
|
-
yield status
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
def filter(params=nil)
|
53
|
-
uri = URI.parse("http://stream.twitter.com/1/statuses/filter.json")
|
54
|
-
start_stream(uri, params) do |status|
|
55
|
-
yield status
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
def track(track, params=nil)
|
60
|
-
uri = URI.parse("http://stream.twitter.com/1/statuses/filter.json")
|
61
|
-
p = {'track'=>track}
|
62
|
-
p.merge!(params) if params
|
63
|
-
start_stream(uri, p) do |status|
|
64
|
-
yield status
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
def follow(follow, params=nil)
|
69
|
-
uri = URI.parse("http://stream.twitter.com/1/statuses/filter.json")
|
70
|
-
p = {'follow'=>follow}
|
71
|
-
p.merge!(params) if params
|
72
|
-
start_stream(uri, p) do |status|
|
73
|
-
yield status
|
74
|
-
end
|
75
|
-
end
|
76
|
-
end
|
77
|
-
end
|