twitter_meme 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
+ SHA256:
3
+ metadata.gz: 00e03fb5e3b68b4157fae830d6087382bb2ca596c9730f2155f067a82c3cb528
4
+ data.tar.gz: 8254e341d46b042e92df4dd2d6266ab8e525ffed381f717db5e78d940765cae4
5
+ SHA512:
6
+ metadata.gz: 61f5eb3d2f8c63f87aedacc5a4ece6f611ea7e7e6a0d6183eb38164a5b0cf1176d2c5069b295abf3e85f8a4eb28c150fe45733e5015e4b8206b627ad2983d917
7
+ data.tar.gz: 48aed9ffb4f84e1381b9b41d38d37b32cf432fa58f4773c96564d7ac0caf42ebd3296099ee385474f49f9f7c89d661beb1d2f7f27f13d1695a37726062a3a45e
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.0
5
+ before_install: gem install bundler -v 1.16.1
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in twitter_meme.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,82 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ twitter_meme (0.1.0)
5
+ rest-client (~> 2.0.0)
6
+ twitter (~> 6.0.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ addressable (2.5.2)
12
+ public_suffix (>= 2.0.2, < 4.0)
13
+ buftok (0.2.0)
14
+ diff-lcs (1.3)
15
+ domain_name (0.5.20170404)
16
+ unf (>= 0.0.5, < 1.0.0)
17
+ equalizer (0.0.11)
18
+ faraday (0.10.1)
19
+ multipart-post (>= 1.2, < 3)
20
+ http (2.2.2)
21
+ addressable (~> 2.3)
22
+ http-cookie (~> 1.0)
23
+ http-form_data (~> 1.0.1)
24
+ http_parser.rb (~> 0.6.0)
25
+ http-cookie (1.0.3)
26
+ domain_name (~> 0.5)
27
+ http-form_data (1.0.3)
28
+ http_parser.rb (0.6.0)
29
+ memoizable (0.4.2)
30
+ thread_safe (~> 0.3, >= 0.3.1)
31
+ mime-types (3.1)
32
+ mime-types-data (~> 3.2015)
33
+ mime-types-data (3.2016.0521)
34
+ multipart-post (2.0.0)
35
+ naught (1.1.0)
36
+ netrc (0.11.0)
37
+ public_suffix (3.0.1)
38
+ rake (10.5.0)
39
+ rest-client (2.0.2)
40
+ http-cookie (>= 1.0.2, < 2.0)
41
+ mime-types (>= 1.16, < 4.0)
42
+ netrc (~> 0.8)
43
+ rspec (3.7.0)
44
+ rspec-core (~> 3.7.0)
45
+ rspec-expectations (~> 3.7.0)
46
+ rspec-mocks (~> 3.7.0)
47
+ rspec-core (3.7.1)
48
+ rspec-support (~> 3.7.0)
49
+ rspec-expectations (3.7.0)
50
+ diff-lcs (>= 1.2.0, < 2.0)
51
+ rspec-support (~> 3.7.0)
52
+ rspec-mocks (3.7.0)
53
+ diff-lcs (>= 1.2.0, < 2.0)
54
+ rspec-support (~> 3.7.0)
55
+ rspec-support (3.7.0)
56
+ simple_oauth (0.3.1)
57
+ thread_safe (0.3.6)
58
+ twitter (6.0.0)
59
+ addressable (~> 2.5)
60
+ buftok (~> 0.2.0)
61
+ equalizer (= 0.0.11)
62
+ faraday (~> 0.10.0)
63
+ http (~> 2.1)
64
+ http_parser.rb (~> 0.6.0)
65
+ memoizable (~> 0.4.2)
66
+ naught (~> 1.1)
67
+ simple_oauth (~> 0.3.1)
68
+ unf (0.1.4)
69
+ unf_ext
70
+ unf_ext (0.0.7.4)
71
+
72
+ PLATFORMS
73
+ ruby
74
+
75
+ DEPENDENCIES
76
+ bundler (~> 1.16)
77
+ rake (~> 10.0)
78
+ rspec (~> 3.0)
79
+ twitter_meme!
80
+
81
+ BUNDLED WITH
82
+ 1.16.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Josh Frye
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,8 @@
1
+ # TwitterMeme
2
+
3
+ ## TODO:
4
+ - specs
5
+ - make config location configurable
6
+ - make logging configurable
7
+ - make tmp dir configurable
8
+ - make LAST_ID configurable
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_meme"
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(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ mkdir -p ~/.twitter_meme/tmp
9
+ touch ~/.twitter_meme/LAST_ID
10
+
11
+ cp config.yml.example ~/.twitter_meme/config.yml
data/bin/twitter_meme ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "twitter_meme"
5
+
6
+ trap('INT') do
7
+ puts "\nShutting down... done"
8
+ exit
9
+ end
10
+
11
+ TwitterMeme::Main.new.process!
@@ -0,0 +1,9 @@
1
+ twitter_username: '@username'
2
+ reply_to_orginal_tweet: false
3
+ imgflip_template_id: 74603695
4
+ imgflip_username: ''
5
+ imgflip_password: ''
6
+ twitter_consumer_key: ''
7
+ twitter_consumer_secret: ''
8
+ twitter_access_token: ''
9
+ twitter_access_token_secret: ''
@@ -0,0 +1,125 @@
1
+ require 'twitter_meme/version'
2
+ require 'twitter_meme/meme'
3
+ require 'twitter_meme/util'
4
+ require 'twitter'
5
+ require 'cgi'
6
+ require 'net/http'
7
+ require 'yaml'
8
+ require 'fileutils'
9
+
10
+ module TwitterMeme
11
+ class Main
12
+ attr_accessor :options
13
+
14
+ def initialize(options = {})
15
+ @options = options.merge!(
16
+ config_file: "#{ENV['HOME']}/.twitter_meme/config.yml"
17
+ )
18
+ puts "Loading config from #{options[:config_file]}"
19
+ load_config
20
+
21
+ puts "Going to process tweets from #{options[:twitter_username]}"
22
+
23
+ begin
24
+ Dir.mkdir("#{ENV['HOME']}/.twitter_meme/tmp")
25
+ rescue Errno::EEXIST
26
+ end
27
+
28
+ FileUtils.touch("#{ENV['HOME']}/.twitter_meme/LAST_ID")
29
+
30
+ self.last_id = 1 if last_id.zero?
31
+ end
32
+
33
+ def load_config
34
+ options.merge!(Util.symbolize_keys(YAML.load_file(options[:config_file])))
35
+ end
36
+
37
+ def process!
38
+ loop do
39
+ # hot reload config between sleeps
40
+ load_config
41
+
42
+ tweets.reverse.each do |tweet|
43
+ puts 'Going to tweet!'
44
+
45
+ self.last_id = tweet.id
46
+
47
+ # use full_text here so we don't get a truncated twitter link
48
+ text0, text1 = Util.split_text(CGI.unescapeHTML(tweet.full_text))
49
+ next unless text0
50
+ text1 ||= ''
51
+
52
+ meme_file = generate_meme(text0, text1, tweet)
53
+
54
+ print 'Tweeting to main account... '
55
+ # my_tweet = client.update_with_media(nil, File.new(meme_file))
56
+ my_tweet = client.update_with_media(CGI.unescapeHTML(tweet.text), File.new(meme_file))
57
+ print "done (#{my_tweet.url})\n"
58
+
59
+ if options[:reply_to_orginal_tweet]
60
+ print "Tweeting reply to #{options[:twitter_username]}... "
61
+ reply_tweet = client.update(
62
+ "#{options[:twitter_username]} #{my_tweet.url}",
63
+ in_reply_to_status_id: last_id
64
+ )
65
+ print "done (#{reply_tweet.url})\n"
66
+ end
67
+ end
68
+
69
+ puts "last_id: #{last_id}"
70
+
71
+ # api rate limits
72
+ sleep 60
73
+ end
74
+ end
75
+
76
+ def client
77
+ Twitter::REST::Client.new do |config|
78
+ config.consumer_key = options[:twitter_consumer_key]
79
+ config.consumer_secret = options[:twitter_consumer_secret]
80
+ config.access_token = options[:twitter_access_token]
81
+ config.access_token_secret = options[:twitter_access_token_secret]
82
+ end
83
+ end
84
+
85
+ def tweets
86
+ print 'Checking for new tweets... '
87
+ tweets = client.user_timeline(
88
+ options[:twitter_username],
89
+ since_id: last_id,
90
+ count: 200,
91
+ trim_user: true,
92
+ exclude_replies: true
93
+ )
94
+ print "done (found #{tweets.length})\n"
95
+
96
+ tweets
97
+ end
98
+
99
+ def generate_meme(text0, text1, tweet)
100
+ puts "Generating meme with text: '#{text0}, #{text1}'"
101
+ meme_image_url = Meme.new(
102
+ options[:imgflip_username],
103
+ options[:imgflip_password],
104
+ options[:imgflip_template_id]
105
+ ).generate_meme(text0, text1)
106
+
107
+ print "Downloading meme image #{meme_image_url}... "
108
+ tmp_file_name = "#{ENV['HOME']}/.twitter_meme/tmp/meme_#{tweet.id}.jpg"
109
+ File.write(tmp_file_name, Net::HTTP.get(URI.parse(meme_image_url)))
110
+ print "done\n"
111
+
112
+ tmp_file_name
113
+ end
114
+
115
+ def last_id
116
+ File.read("#{ENV['HOME']}/.twitter_meme/LAST_ID").chomp.to_i
117
+ end
118
+
119
+ def last_id=(last_id)
120
+ file = File.open("#{ENV['HOME']}/.twitter_meme/LAST_ID", 'w')
121
+ file.write(last_id)
122
+ file.close
123
+ end
124
+ end
125
+ end
@@ -0,0 +1,32 @@
1
+ require 'rest-client'
2
+
3
+ class Meme
4
+ attr_reader :username, :password, :template_id
5
+
6
+ def initialize(username, password, template_id)
7
+ @username = username
8
+ @password = password
9
+ @template_id = template_id
10
+ end
11
+
12
+ def generate_meme(text0, text1)
13
+ http_resp = make_api_call(text0, text1)
14
+
15
+ return unless http_resp.code == 200
16
+ result = JSON.parse(http_resp.body)
17
+ return unless result['success']
18
+
19
+ result['data']['url']
20
+ end
21
+
22
+ def make_api_call(text0, text1)
23
+ RestClient.post(
24
+ 'http://api.imgflip.com/caption_image',
25
+ username: username,
26
+ password: password,
27
+ template_id: template_id,
28
+ text0: text0,
29
+ text1: text1
30
+ )
31
+ end
32
+ end
@@ -0,0 +1,48 @@
1
+ class Util
2
+ def self.split_text(text)
3
+ i = text[0..(text.length / 2)].rindex(' ')
4
+ if i.nil?
5
+ if include?(' ')
6
+ split(' ', 1)
7
+ else
8
+ [text, '']
9
+ end
10
+ else
11
+ [text[0..i].strip, text[i..-1].strip]
12
+ end
13
+ end
14
+
15
+ def self.symbolize_keys(obj)
16
+ case obj
17
+ when Array
18
+ obj.inject([]){|res, val|
19
+ res << case val
20
+ when Hash, Array
21
+ symbolize_keys(val)
22
+ else
23
+ val
24
+ end
25
+ res
26
+ }
27
+ when Hash
28
+ obj.inject({}){|res, (key, val)|
29
+ nkey = case key
30
+ when String
31
+ key.to_sym
32
+ else
33
+ key
34
+ end
35
+ nval = case val
36
+ when Hash, Array
37
+ symbolize_keys(val)
38
+ else
39
+ val
40
+ end
41
+ res[nkey] = nval
42
+ res
43
+ }
44
+ else
45
+ obj
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,3 @@
1
+ module TwitterMeme
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,29 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "twitter_meme/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "twitter_meme"
8
+ spec.version = TwitterMeme::VERSION
9
+ spec.authors = ["Josh Frye"]
10
+ spec.email = ["josh@joshfrye.com"]
11
+
12
+ spec.summary = "Mirror twitter accounts to parody meme accounts"
13
+ spec.description = "twitter_meme allows you to follow a twitter account and repost tweets as meme images"
14
+ spec.homepage = "https://github.com/joshfng/twitter_meme"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "bin"
21
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.16"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ spec.add_dependency "twitter", "~> 6.0.0"
28
+ spec.add_dependency "rest-client", "~> 2.0.0"
29
+ end
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: twitter_meme
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Josh Frye
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-01-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: twitter
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 6.0.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 6.0.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: rest-client
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 2.0.0
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 2.0.0
83
+ description: twitter_meme allows you to follow a twitter account and repost tweets
84
+ as meme images
85
+ email:
86
+ - josh@joshfrye.com
87
+ executables:
88
+ - console
89
+ - setup
90
+ - twitter_meme
91
+ extensions: []
92
+ extra_rdoc_files: []
93
+ files:
94
+ - ".gitignore"
95
+ - ".rspec"
96
+ - ".travis.yml"
97
+ - Gemfile
98
+ - Gemfile.lock
99
+ - LICENSE.txt
100
+ - README.md
101
+ - Rakefile
102
+ - bin/console
103
+ - bin/setup
104
+ - bin/twitter_meme
105
+ - config.yml.example
106
+ - lib/twitter_meme.rb
107
+ - lib/twitter_meme/meme.rb
108
+ - lib/twitter_meme/util.rb
109
+ - lib/twitter_meme/version.rb
110
+ - twitter_meme.gemspec
111
+ homepage: https://github.com/joshfng/twitter_meme
112
+ licenses:
113
+ - MIT
114
+ metadata: {}
115
+ post_install_message:
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ requirements: []
130
+ rubyforge_project:
131
+ rubygems_version: 2.7.4
132
+ signing_key:
133
+ specification_version: 4
134
+ summary: Mirror twitter accounts to parody meme accounts
135
+ test_files: []