twitter_topic_bot 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9b82cc1435918f3e1b0cb1ef84c332f14fa2ba17
4
+ data.tar.gz: f8a4e6862aa5cd85da511df4d2554dc0e2b1a7df
5
+ SHA512:
6
+ metadata.gz: fa5e6ab98c56c1841a1d5cb5698514bfcdf7931da176f8f19be2888538fe461d380db8d991456bff66dca3fd14207b62f17ca6108a9e2ada22a9d304f8397102
7
+ data.tar.gz: 1d66820a52febe7a771e3d20fc4f9f9b6742768712ff44aa87d1e1d5d4f06fd9cd8539ff92b9bbe55a1102f0646055ef53bf421ffd7d3f8414e5d34baef9037b
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.12.5
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at annekjohnson23@yahoo.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in twitter_topic_bot.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,74 @@
1
+ # TwitterTopicBot
2
+
3
+ Create a Twitter bot in 5 minutes that tweets and engages with the community on topics you're interested in!
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'twitter_topic_bot'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install twitter_topic_bot
20
+
21
+ ## Usage
22
+
23
+ To let your bot know what to tweet about, you need to make an object that responds to the three methods in the code sample below. The topic below is art, but your topic can be about anything.
24
+
25
+ ```ruby
26
+ class ArtContentPreparer
27
+ def topic_string
28
+ ['#painting', '#watercolor'].sample
29
+ end
30
+
31
+ def prepare_tweet
32
+ %q{If you hear a voice within you say 'you cannot paint,' then by all means paint, and that voice will be silenced. -Vincent Van Gogh}
33
+ end
34
+
35
+ def prepare_reply(tweet_to_reply_to, user_to_reply_to)
36
+ "@#{user_to_reply_to}, thank you for tweeting about art!"
37
+ end
38
+ end
39
+
40
+ content_preparer = ArtContentPreparer.new
41
+ ```
42
+
43
+ Next, get your Twitter API credentials ready. Register an app on [Twitter](https://apps.twitter.com/) with read & write permissions. Once you have your API keys, prepare the following pieces of information:
44
+
45
+ ```ruby
46
+ credentials = {
47
+ username: '<Your bot\'s username (without the @ sign)>',
48
+ consumer_key: '<Your consumer key>',
49
+ consumer_secret: '<Your consumer secret>',
50
+ access_token: '<Your access token>',
51
+ access_token_secret: '<Your access token secret>'
52
+ }
53
+ ```
54
+
55
+ Next, instantiate a `TwitterTopicBot`, and make it do things!
56
+
57
+ ```ruby
58
+ bot = TwitterTopicBot.new(content_preparer, credentials)
59
+
60
+ bot.tweet
61
+ bot.retweet_someone
62
+ bot.follow_someone
63
+ bot.retweet_mentions
64
+ bot.reply_to_someone
65
+ bot.follow_followers
66
+ ```
67
+
68
+ ## Development
69
+
70
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
71
+
72
+ ## Contributing
73
+
74
+ Bug reports and pull requests are welcome on GitHub at https://github.com/annejohnson/twitter_topic_bot. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'twitter_topic_bot'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,95 @@
1
+ require 'twitter'
2
+ require 'forwardable'
3
+
4
+ class TwitterTopicBot
5
+ class ApiClient
6
+ extend Forwardable
7
+
8
+ MAX_TWEET_LENGTH = 140
9
+
10
+ def_delegators :twitter_client,
11
+ :favorite,
12
+ :following,
13
+ :followers
14
+
15
+ def initialize(credentials)
16
+ @credentials = Hash[credentials.map { |k, v| [k.to_s, v] }]
17
+ @tweet_filterer = TwitterTopicBot::TweetFilterer.new
18
+ @max_num_tweets_per_query = 60
19
+ end
20
+
21
+ def max_tweet_length
22
+ MAX_TWEET_LENGTH
23
+ end
24
+
25
+ def tweet(str, options = {})
26
+ twitter_client.update(str, options)
27
+ end
28
+
29
+ def retweet(*tweets)
30
+ twitter_client.retweet(*tweets.map(&:id))
31
+ rescue Twitter::Error::Forbidden
32
+ false
33
+ end
34
+
35
+ def follow(*users)
36
+ twitter_client.follow(*users.map(&:id))
37
+ end
38
+
39
+ def search_recent_tweets(query_str)
40
+ filter_tweets(
41
+ twitter_client.search(
42
+ query_str,
43
+ result_type: 'recent'
44
+ ).take(max_num_tweets_per_query)
45
+ )
46
+ end
47
+
48
+ def mentions
49
+ filter_tweets(
50
+ twitter_client.mentions_timeline,
51
+ allow_links: false
52
+ )
53
+ end
54
+
55
+ def tweets
56
+ twitter_client.user_timeline(
57
+ username,
58
+ count: max_num_tweets_per_query
59
+ )
60
+ end
61
+
62
+ def replies
63
+ tweets.select do |tweet|
64
+ tweet.in_reply_to_status_id.to_s.match(/\d+/)
65
+ end
66
+ end
67
+
68
+ def username
69
+ credentials['username']
70
+ end
71
+
72
+ private
73
+
74
+ attr_reader :tweet_filterer,
75
+ :credentials,
76
+ :max_num_tweets_per_query
77
+
78
+ def filter_tweets(tweets, allow_links: true)
79
+ allowed_languages = ['en']
80
+ tweets.select do |tweet|
81
+ allowed_languages.include?(tweet.lang) &&
82
+ tweet_filterer.acceptable_tweet?(tweet.text, allow_links: allow_links)
83
+ end
84
+ end
85
+
86
+ def twitter_client
87
+ @twitter_client ||= Twitter::REST::Client.new do |config|
88
+ config.consumer_key = credentials['consumer_key']
89
+ config.consumer_secret = credentials['consumer_secret']
90
+ config.access_token = credentials['access_token']
91
+ config.access_token_secret = credentials['access_token_secret']
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,49 @@
1
+ class TwitterTopicBot
2
+ class TweetFilterer
3
+ def initialize(options = {})
4
+ @max_num_mentions = options.fetch(:max_num_mentions, 1)
5
+ @max_num_hashtags = options.fetch(:max_num_hashtags, 3)
6
+ end
7
+
8
+ def acceptable_tweet?(tweet_str, allow_links: true)
9
+ return false if is_spammy_tweet?(tweet_str) ||
10
+ is_sketchy_tweet?(tweet_str) ||
11
+ has_too_many_mentions?(tweet_str) ||
12
+ has_too_many_hashtags?(tweet_str)
13
+
14
+ allow_links || !contains_link?(tweet_str)
15
+ end
16
+
17
+ private
18
+
19
+ attr_reader :max_num_mentions,
20
+ :max_num_hashtags
21
+
22
+ def is_spammy_tweet?(tweet_str)
23
+ contains_link?(tweet_str) &&
24
+ tweet_str.match(/(buy)|(e-?book)|(order)|(press)/i)
25
+ end
26
+
27
+ def is_sketchy_tweet?(tweet_str)
28
+ tweet_str.match(/(fuck)|(fetish)|(ass)|(gamergate)|(shit)|(bitch)|(cunt)|(rape)/i)
29
+ end
30
+
31
+ def has_too_many_mentions?(tweet_str)
32
+ num_mentions = tweet_str.split.count do |word|
33
+ word.start_with?('@')
34
+ end
35
+ num_mentions > max_num_mentions
36
+ end
37
+
38
+ def has_too_many_hashtags?(tweet_str)
39
+ num_hashtags = tweet_str.split.count do |word|
40
+ word.start_with?('#')
41
+ end
42
+ num_hashtags > max_num_hashtags
43
+ end
44
+
45
+ def contains_link?(tweet_str)
46
+ tweet_str.match(/http:\/\//i)
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,3 @@
1
+ class TwitterTopicBot
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,75 @@
1
+ require 'twitter'
2
+ require 'twitter_topic_bot/version'
3
+ require 'twitter_topic_bot/api_client'
4
+ require 'twitter_topic_bot/tweet_filterer'
5
+
6
+ class TwitterTopicBot
7
+ def initialize(content_preparer, credentials)
8
+ @content_preparer = content_preparer
9
+ @api_client = TwitterTopicBot::ApiClient.new(credentials)
10
+ end
11
+
12
+ def tweet
13
+ api_client.tweet(
14
+ content_preparer.prepare_tweet
15
+ )
16
+ end
17
+
18
+ def retweet_someone
19
+ api_client.retweet(tweet_to_retweet)
20
+ end
21
+
22
+ def follow_someone
23
+ api_client.follow(user_to_follow)
24
+ end
25
+
26
+ def retweet_mentions
27
+ # TODO: filter out already retweeted mentions
28
+ api_client.retweet *api_client.mentions
29
+ end
30
+
31
+ def reply_to_someone
32
+ tweet = tweet_to_reply_to
33
+ reply = content_preparer.prepare_reply(
34
+ tweet.text,
35
+ tweet.user.screen_name
36
+ )
37
+
38
+ api_client.tweet(
39
+ reply,
40
+ in_reply_to_status_id: tweet.id
41
+ )
42
+ end
43
+
44
+ def follow_followers
45
+ # TODO: it is too easy to hit the rate limit on this one
46
+ api_client.follow *api_client.followers
47
+ end
48
+
49
+ private
50
+
51
+ attr_reader :content_preparer,
52
+ :api_client
53
+
54
+ def tweet_to_retweet
55
+ get_topic_tweets.max_by(&:favorite_count)
56
+ end
57
+
58
+ def user_to_follow
59
+ get_topic_tweets.sample.user
60
+ end
61
+
62
+ def tweet_to_reply_to
63
+ tweet_ids_already_replied_to = api_client.replies.map(&:in_reply_to_status_id)
64
+
65
+ get_topic_tweets.reject do |tweet|
66
+ tweet.retweet? || tweet_ids_already_replied_to.include?(tweet.id)
67
+ end.sample
68
+ end
69
+
70
+ def get_topic_tweets
71
+ api_client.search_recent_tweets(
72
+ content_preparer.topic_string
73
+ )
74
+ end
75
+ end
@@ -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 'twitter_topic_bot/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'twitter_topic_bot'
8
+ spec.version = TwitterTopicBot::VERSION
9
+ spec.authors = ['Anne Johnson']
10
+ spec.email = ['annecodes@gmail.com']
11
+
12
+ spec.summary = %q{A gem to create Twitter bots that tweet about topics of interest}
13
+ spec.homepage = 'https://github.com/annejohnson/twitter_topic_bot'
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_runtime_dependency 'twitter', '~> 5.16.0'
21
+ spec.add_development_dependency 'bundler', '~> 1.12'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+ spec.add_development_dependency 'rspec', '~> 3.0'
24
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: twitter_topic_bot
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Anne Johnson
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-05-29 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.16.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 5.16.0
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: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description:
70
+ email:
71
+ - annecodes@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - CODE_OF_CONDUCT.md
80
+ - Gemfile
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - lib/twitter_topic_bot.rb
86
+ - lib/twitter_topic_bot/api_client.rb
87
+ - lib/twitter_topic_bot/tweet_filterer.rb
88
+ - lib/twitter_topic_bot/version.rb
89
+ - twitter_topic_bot.gemspec
90
+ homepage: https://github.com/annejohnson/twitter_topic_bot
91
+ licenses: []
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.6.4
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: A gem to create Twitter bots that tweet about topics of interest
113
+ test_files: []