mihatter 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/Gemfile +4 -0
- data/README.md +53 -0
- data/Rakefile +2 -0
- data/bin/console +7 -0
- data/bin/setup +8 -0
- data/lib/mihatter.rb +12 -0
- data/lib/mihatter/configuration.rb +24 -0
- data/lib/mihatter/rest_watcher.rb +67 -0
- data/lib/mihatter/streaming_watcher.rb +30 -0
- data/lib/mihatter/version.rb +3 -0
- data/mihatter.gemspec +24 -0
- metadata +112 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 16370366bc95fe808cd74431832fdb9e996eeb62
|
4
|
+
data.tar.gz: 9fd5df7779f57f9c75dec597dcf86a2988aeb235
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5cb17d507e29e7f3b5679edd47aa9224eb99c10f500aad16f0974b0a694c5924824e4ace5bd63cf2da039beccb5f39228ef7e87842864b400d4ea48bdd822539
|
7
|
+
data.tar.gz: 4ea59e4ae3e23974056918f6cebc42baced72cfc57b25d3ea39de7980aadb6c0b61ae3592608c46a8b620f8ea88bddea533322dcf240e2a11de5468837e78f91
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# Mihatter
|
2
|
+
|
3
|
+
Mihatter is a simple Twitter crawler.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'mihatter'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install mihatter
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
require 'mihatter'
|
25
|
+
|
26
|
+
Mihatter.configuration do |config|
|
27
|
+
config.consumer_key = '.....'
|
28
|
+
config.consumer_secret = '.....'
|
29
|
+
config.access_token = '.....'
|
30
|
+
config.access_token_secret = '.....'
|
31
|
+
|
32
|
+
config.keyword = 'sushi'
|
33
|
+
end
|
34
|
+
|
35
|
+
# use REST API
|
36
|
+
watcher = Mihatter::RestWatcher.new
|
37
|
+
watcher.run! do |tweet|
|
38
|
+
puts tweet.text
|
39
|
+
end
|
40
|
+
|
41
|
+
# use Streaming API (only English, cannot use CJK)
|
42
|
+
watcher = Mihatter::StreamingWatcher.new
|
43
|
+
watcher.run! do |tweet|
|
44
|
+
puts tweet.text
|
45
|
+
end
|
46
|
+
```
|
47
|
+
|
48
|
+
## License
|
49
|
+
MIT
|
50
|
+
|
51
|
+
## Contributing
|
52
|
+
|
53
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/rutan/mihatter.
|
data/Rakefile
ADDED
data/bin/console
ADDED
data/bin/setup
ADDED
data/lib/mihatter.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'mihatter/version'
|
2
|
+
require 'mihatter/configuration'
|
3
|
+
require 'mihatter/rest_watcher'
|
4
|
+
require 'mihatter/streaming_watcher'
|
5
|
+
|
6
|
+
module Mihatter
|
7
|
+
def self.configuration
|
8
|
+
@configuration ||= Mihatter::Configuration.new
|
9
|
+
yield @configuration if block_given?
|
10
|
+
@configuration
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Mihatter
|
2
|
+
class Configuration
|
3
|
+
def initialize
|
4
|
+
self.keyword = 'Twitter'
|
5
|
+
self.wait_time = 30
|
6
|
+
end
|
7
|
+
|
8
|
+
attr_accessor :consumer_key
|
9
|
+
attr_accessor :consumer_secret
|
10
|
+
attr_accessor :access_token
|
11
|
+
attr_accessor :access_token_secret
|
12
|
+
|
13
|
+
attr_accessor :keyword
|
14
|
+
attr_accessor :lang
|
15
|
+
attr_accessor :since_id
|
16
|
+
|
17
|
+
attr_accessor :wait_time
|
18
|
+
|
19
|
+
def merge(config = {})
|
20
|
+
config.each { |k, v| self.public_send("#{k}=", v) }
|
21
|
+
self
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'twitter'
|
2
|
+
|
3
|
+
module Mihatter
|
4
|
+
class RestWatcher
|
5
|
+
def initialize(config = {})
|
6
|
+
@config = Mihatter.configuration.dup.merge(config)
|
7
|
+
end
|
8
|
+
|
9
|
+
attr_reader :config
|
10
|
+
|
11
|
+
def run!
|
12
|
+
raise ArgumentError, '`Mihatter::RestWatcher#run!` require block' unless block_given?
|
13
|
+
|
14
|
+
begin
|
15
|
+
connect
|
16
|
+
assign_since_id unless @config.since_id
|
17
|
+
loop do
|
18
|
+
search.each do |tweet|
|
19
|
+
yield tweet
|
20
|
+
@config.since_id = tweet.id
|
21
|
+
end
|
22
|
+
sleep @config.wait_time
|
23
|
+
end
|
24
|
+
rescue Twitter::Error::TooManyRequests => e
|
25
|
+
sleep e.rate_limit.reset_in
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def since_id
|
30
|
+
@config.since_id
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def connect
|
36
|
+
@client = Twitter::REST::Client.new do |config|
|
37
|
+
config.consumer_key = @config.consumer_key
|
38
|
+
config.consumer_secret = @config.consumer_secret
|
39
|
+
config.access_token = @config.access_token
|
40
|
+
config.access_token_secret = @config.access_token_secret
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def assign_since_id
|
45
|
+
result = @client.search(@config.keyword, {
|
46
|
+
result_type: 'recent',
|
47
|
+
count: 1,
|
48
|
+
lang: @config.lang,
|
49
|
+
}.delete_if { |_, v| v.nil? }).first
|
50
|
+
@config.since_id = (result ? result.id : 0)
|
51
|
+
end
|
52
|
+
|
53
|
+
def search(max_id: nil)
|
54
|
+
results = @client.search(@config.keyword, {
|
55
|
+
result_type: 'recent',
|
56
|
+
count: 100,
|
57
|
+
lang: @config.lang,
|
58
|
+
max_id: max_id,
|
59
|
+
since_id: @config.since_id,
|
60
|
+
}.delete_if {|_, v| v.nil?} ).take(100).sort do |a, b|
|
61
|
+
a.id <=> b.id
|
62
|
+
end
|
63
|
+
results = search(max_id: results.first.id - 1) + results if results.size == 100
|
64
|
+
results
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'twitter'
|
2
|
+
|
3
|
+
module Mihatter
|
4
|
+
class StreamingWatcher
|
5
|
+
def initialize(config = {})
|
6
|
+
@config = Mihatter.configuration.dup.merge(config)
|
7
|
+
end
|
8
|
+
|
9
|
+
attr_reader :config
|
10
|
+
|
11
|
+
def run!
|
12
|
+
raise ArgumentError, '`Mihatter::StreamingWatcher#run!` require block' unless block_given?
|
13
|
+
connect
|
14
|
+
@client.filter(track: @config.keyword) do |obj|
|
15
|
+
yield obj if obj.is_a?(Twitter::Tweet)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def connect
|
22
|
+
@client = Twitter::Streaming::Client.new do |config|
|
23
|
+
config.consumer_key = @config.consumer_key
|
24
|
+
config.consumer_secret = @config.consumer_secret
|
25
|
+
config.access_token = @config.access_token
|
26
|
+
config.access_token_secret = @config.access_token_secret
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
data/mihatter.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'mihatter/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'mihatter'
|
8
|
+
spec.version = Mihatter::VERSION
|
9
|
+
spec.authors = ['ru_shalm']
|
10
|
+
spec.email = ['ru_shalm@hazimu.com']
|
11
|
+
spec.summary = %q{Simple Twitter crawler}
|
12
|
+
spec.homepage = 'https://github.com/rutan/mihatter'
|
13
|
+
spec.licenses = ['MIT']
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
16
|
+
spec.bindir = 'exe'
|
17
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
18
|
+
spec.require_paths = ['lib']
|
19
|
+
|
20
|
+
spec.add_dependency 'twitter', '~> 5'
|
21
|
+
spec.add_development_dependency 'bundler', '~> 1.12'
|
22
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
23
|
+
spec.add_development_dependency 'pry'
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mihatter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- ru_shalm
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-05-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: twitter
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '5'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.12'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.12'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- ru_shalm@hazimu.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- Gemfile
|
78
|
+
- README.md
|
79
|
+
- Rakefile
|
80
|
+
- bin/console
|
81
|
+
- bin/setup
|
82
|
+
- lib/mihatter.rb
|
83
|
+
- lib/mihatter/configuration.rb
|
84
|
+
- lib/mihatter/rest_watcher.rb
|
85
|
+
- lib/mihatter/streaming_watcher.rb
|
86
|
+
- lib/mihatter/version.rb
|
87
|
+
- mihatter.gemspec
|
88
|
+
homepage: https://github.com/rutan/mihatter
|
89
|
+
licenses:
|
90
|
+
- MIT
|
91
|
+
metadata: {}
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options: []
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
requirements: []
|
107
|
+
rubyforge_project:
|
108
|
+
rubygems_version: 2.5.1
|
109
|
+
signing_key:
|
110
|
+
specification_version: 4
|
111
|
+
summary: Simple Twitter crawler
|
112
|
+
test_files: []
|