robotwitter 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ gem "twitter"
5
+ gem "sqlite3"
6
+
7
+ # Add dependencies to develop your gem here.
8
+ # Include everything needed to run rake, tests, features, etc.
9
+ group :development do
10
+ gem "shoulda", ">= 0"
11
+ gem "bundler", "~> 1.0.0"
12
+ gem "jeweler", "~> 1.5.2"
13
+ gem "rcov", ">= 0"
14
+ end
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Krivich Ekaterina
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ = Robotwitter
2
+
3
+ Robotwitter makes it easer to robust some twitter functions, such as:
4
+ * creating tweets
5
+ * retweeting by keywords
6
+ * follow back who follows you
7
+ * unfollow who do not follows you
8
+
9
+ == Usage
10
+
11
+ * gem install robotwitter
12
+
13
+ client = Robotwitter.new "#{path_to_config_yaml}", "your_twitter_login", &GETTER
14
+ &GETTER could be nil (see below)
15
+
16
+ client.follow_all_back
17
+
18
+ == Config Example
19
+ your_twitter_login:
20
+ consumer_key: key
21
+ consumer_secret: secret
22
+ oauth_token: oauth_token
23
+ oauth_token_secret: oauth_token
24
+
25
+ == Copyright
26
+
27
+ Copyright (c) 2011 Krivich Ekaterina. See LICENSE.txt for
28
+ further details.
29
+
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "robotwitter"
16
+ gem.homepage = "http://github.com/kiote/robotwitter"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{automate some twitter tasks}
19
+ gem.description = %Q{automate twitter tasks, such as retweetting, following, unfollowing}
20
+ gem.email = "krivich.ekaterina@gmail.com"
21
+ gem.authors = ["Krivich Ekaterina"]
22
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
23
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
25
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rake/testtask'
30
+ Rake::TestTask.new(:test) do |test|
31
+ test.libs << 'lib' << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+
36
+ require 'rcov/rcovtask'
37
+ Rcov::RcovTask.new do |test|
38
+ test.libs << 'test'
39
+ test.pattern = 'test/**/test_*.rb'
40
+ test.verbose = true
41
+ end
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "robotwitter #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
@@ -0,0 +1,161 @@
1
+ require "rubygems"
2
+ require "twitter"
3
+ require "open-uri"
4
+ require 'pp'
5
+ require 'yaml'
6
+ require 'sqlite3'
7
+
8
+ module Robotwitter
9
+ class Robot
10
+ attr_accessor :stub
11
+ @db = nil
12
+
13
+ # getter should be a lambda - function
14
+ # which returns string
15
+ # example of getter
16
+ #
17
+ # SQLITE_GETTER = lambda do |s|
18
+ # db = SQLite3::Database.new("database.db")
19
+ # db.get_first_row( "select * from table" )
20
+ # end
21
+ def initialize config_path, section, &getter
22
+ @getter = getter
23
+ @followers_ids = nil
24
+ @following_ids = nil
25
+
26
+ @stub = false
27
+
28
+ yml = YAML.load_file config_path
29
+
30
+ Twitter.configure do |config|
31
+ config.consumer_key = yml[section]['consumer_key']
32
+ config.consumer_secret = yml[section]['consumer_secret']
33
+ config.oauth_token = yml[section]['oauth_token']
34
+ config.oauth_token_secret = yml[section]['oauth_token_secret']
35
+ end
36
+ @client = Twitter::Client.new
37
+ end
38
+
39
+ # follow who follows me
40
+ def follow_all_back
41
+ get_followers_ids
42
+ get_following_ids
43
+
44
+ follow_them = @followers_ids - @following_ids
45
+ pp 'follows:'
46
+ follow_them.each do |id|
47
+ begin
48
+ @client.follow id if not @stub
49
+ rescue
50
+ end
51
+ pp id
52
+ end
53
+ end
54
+
55
+ # string '_msg_ somth'
56
+ def send_message pattern
57
+ phrase = get_phrase
58
+ send = pattern.gsub('_msg_', phrase)
59
+ @client.update send if not @stub
60
+ pp send
61
+ end
62
+
63
+ def send_message_mention pattern
64
+
65
+ end
66
+
67
+ def retweet_about word
68
+ search = search_users_tweets_about(word, 2)
69
+ init_db
70
+ search.each do |result|
71
+ next if @db.retweeted?(result)
72
+ retweet result
73
+ @db.save_retweet result
74
+ pp result['id']
75
+ end
76
+ end
77
+
78
+ # follow who tweet about word
79
+ def follow_users_tweets_about word
80
+ users = search_users_tweets_about word
81
+
82
+ get_followers_ids
83
+ get_following_ids
84
+ pp users
85
+ users.each do |user|
86
+ id = user['from_user_id']
87
+ name = user['from_user']
88
+ begin
89
+ if (not @followers_ids.include?(id)) \
90
+ and (not @following_ids.include?(id) and not @stub)
91
+ @client.follow name
92
+ pp name
93
+ end
94
+ rescue Exception => msg
95
+ pp "Error #{msg}"
96
+ end
97
+ end
98
+ end
99
+
100
+ #unfollow who not following me
101
+ def unfollow_users
102
+ get_followers_ids
103
+ get_following_ids
104
+
105
+ unfollow_them = @following_ids - @followers_ids
106
+ unfollow_them.each do |id|
107
+ begin
108
+ @client.unfollow id if not @stub
109
+ rescue
110
+ end
111
+ pp id
112
+ end
113
+ end
114
+
115
+ protected
116
+
117
+ def init_db
118
+ @db = Db.new('tweets') if @db.nil?
119
+ @db
120
+ end
121
+
122
+ # get phrase
123
+ def get_phrase
124
+ @getter.call self
125
+ end
126
+
127
+ # ищем пользователей, которые пишут о
128
+ def search_users_tweets_about word, count = 5
129
+ Twitter::Search.new.containing(word).locale("ru").no_retweets.per_page(count).fetch
130
+ end
131
+
132
+ # get follower ids
133
+ def get_followers_ids
134
+ if @followers_ids.nil?
135
+ @followers_ids = @client.follower_ids['ids']
136
+ end
137
+ @followers_ids
138
+ end
139
+
140
+ # get following ids
141
+ def get_following_ids
142
+ if @following_ids.nil?
143
+ @following_ids = @client.friend_ids['ids']
144
+ end
145
+ end
146
+
147
+ # ретвитим
148
+ # result - hash от поиска
149
+ def retweet(result)
150
+ begin
151
+ @client.retweet(result['id']) unless @stub
152
+ rescue
153
+ puts 'error'
154
+ end
155
+ end
156
+
157
+ def rate_limit
158
+ puts @client.rate_limit_status.remaining_hits.to_s + " Twitter API request(s) remaining this hour"
159
+ end
160
+ end
161
+ end
@@ -0,0 +1,8 @@
1
+ require "spec_helper"
2
+
3
+ describe "Robotwitter" do
4
+
5
+ it "should do something" do
6
+ true.should == true
7
+ end
8
+ end
@@ -0,0 +1,4 @@
1
+ $:.unshift File.expand_path('../../lib', __FILE__)
2
+ $:.unshift File.dirname(__FILE__)
3
+
4
+ require "robotwitter"
metadata ADDED
@@ -0,0 +1,163 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: robotwitter
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Krivich Ekaterina
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-04-21 00:00:00 +04:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ prerelease: false
23
+ name: twitter
24
+ version_requirements: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ requirement: *id001
34
+ type: :runtime
35
+ - !ruby/object:Gem::Dependency
36
+ prerelease: false
37
+ name: sqlite3
38
+ version_requirements: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ requirement: *id002
48
+ type: :runtime
49
+ - !ruby/object:Gem::Dependency
50
+ prerelease: false
51
+ name: shoulda
52
+ version_requirements: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ requirement: *id003
62
+ type: :development
63
+ - !ruby/object:Gem::Dependency
64
+ prerelease: false
65
+ name: bundler
66
+ version_requirements: &id004 !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ~>
70
+ - !ruby/object:Gem::Version
71
+ hash: 23
72
+ segments:
73
+ - 1
74
+ - 0
75
+ - 0
76
+ version: 1.0.0
77
+ requirement: *id004
78
+ type: :development
79
+ - !ruby/object:Gem::Dependency
80
+ prerelease: false
81
+ name: jeweler
82
+ version_requirements: &id005 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ~>
86
+ - !ruby/object:Gem::Version
87
+ hash: 7
88
+ segments:
89
+ - 1
90
+ - 5
91
+ - 2
92
+ version: 1.5.2
93
+ requirement: *id005
94
+ type: :development
95
+ - !ruby/object:Gem::Dependency
96
+ prerelease: false
97
+ name: rcov
98
+ version_requirements: &id006 !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ hash: 3
104
+ segments:
105
+ - 0
106
+ version: "0"
107
+ requirement: *id006
108
+ type: :development
109
+ description: automate twitter tasks, such as retweetting, following, unfollowing
110
+ email: krivich.ekaterina@gmail.com
111
+ executables: []
112
+
113
+ extensions: []
114
+
115
+ extra_rdoc_files:
116
+ - LICENSE.txt
117
+ - README.rdoc
118
+ files:
119
+ - .document
120
+ - Gemfile
121
+ - LICENSE.txt
122
+ - README.rdoc
123
+ - Rakefile
124
+ - lib/robotwitter.rb
125
+ - spec/robotwitter/robotwitter_spec.rb
126
+ - spec/spec_helper.rb
127
+ has_rdoc: true
128
+ homepage: http://github.com/kiote/robotwitter
129
+ licenses:
130
+ - MIT
131
+ post_install_message:
132
+ rdoc_options: []
133
+
134
+ require_paths:
135
+ - lib
136
+ required_ruby_version: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ hash: 3
142
+ segments:
143
+ - 0
144
+ version: "0"
145
+ required_rubygems_version: !ruby/object:Gem::Requirement
146
+ none: false
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ hash: 3
151
+ segments:
152
+ - 0
153
+ version: "0"
154
+ requirements: []
155
+
156
+ rubyforge_project:
157
+ rubygems_version: 1.3.7
158
+ signing_key:
159
+ specification_version: 3
160
+ summary: automate some twitter tasks
161
+ test_files:
162
+ - spec/robotwitter/robotwitter_spec.rb
163
+ - spec/spec_helper.rb