Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 174 lines (140 sloc) 5.109 kb
7db08a6 @gleuch first commit of retweet-fucker.
gleuch authored
1 require 'rubygems'
2 require 'sinatra'
3 require 'twitter_oauth'
4 require 'configatron'
5
6 configure do
7 %w(dm-core dm-aggregates dm-timestamps user).each { |lib| require lib }
8
9 ROOT = File.expand_path(File.dirname(__FILE__))
10 configatron.configure_from_yaml("#{ROOT}/settings.yml", :hash => Sinatra::Application.environment.to_s)
11
12 DataMapper.setup(:default, configatron.db_connection.gsub(/ROOT/, ROOT))
13 DataMapper.auto_upgrade!
14
15 set :sessions, true
16 end
17
18 helpers do
19 def twitter_connect(user={})
ff1ac34 @gleuch some more touch-ups, prepping for some styling.
gleuch authored
20 begin
21 unless user.blank?
22 @twitter_client = TwitterOAuth::Client.new(:consumer_key => configatron.twitter_oauth_token, :consumer_secret => configatron.twitter_oauth_secret, :token => user.oauth_token, :secret => user.oauth_secret) rescue nil
23 else
24 @twitter_client = TwitterOAuth::Client.new(:consumer_key => configatron.twitter_oauth_token, :consumer_secret => configatron.twitter_oauth_secret) rescue nil
25 end
26 rescue
27 twitter_fail
7db08a6 @gleuch first commit of retweet-fucker.
gleuch authored
28 end
29
30 # Do some error here if connection fails!
31 end
32
ff1ac34 @gleuch some more touch-ups, prepping for some styling.
gleuch authored
33 def twitter_fail(msg=false)
34 msg = 'An error has occured while trying to talk to Twitter. Please try again.' if msg.blank?
35 @error = msg
36 haml :fail and return
37 end
38
7db08a6 @gleuch first commit of retweet-fucker.
gleuch authored
39 def get_user
40 @user = User.first(:id => session[:user])
41 end
42
43 def launch_retweet_hell
44 # 1. Get random tweet
45 # 2. Get list of random users (no more than 20% or 500, whichever is less)
46 # 3. Tweet away (and remove failed users -- assume they deleted access)
47
48 @base_user, @tweet, user_ct, fail_ct = nil, 'NO TWEET', User.count, 0
49
50 while (@base_user.blank? || fail_ct < 10)
51 @base_user = User.get(1+rand(user_ct)) rescue nil
52
53 unless @base_user.blank?
54 twitter_connect(@base_user)
55
56 unless @twitter_client.blank?
ff1ac34 @gleuch some more touch-ups, prepping for some styling.
gleuch authored
57 info = @twitter_client.info rescue nil
58
7db08a6 @gleuch first commit of retweet-fucker.
gleuch authored
59 @tweet = "RT: @#{info['screen_name']}: %s #{configatron.twitter_hashtag}"
60
61 x = 142-@tweet.length
62
63 @tweet = @tweet.gsub(/\%s/, (info['status']['text'])[0,x])
64
65 # Don't tweet blank stuff
66 @base_user = nil if @tweet.blank?
67 else
68 # Remove from database -- fuck them.
69 @base_user.destroy
70 @base_user = nil
71 end
72 end
73
74 fail_ct += 1
75 end
76
77 unless @tweet.blank?
78 total = (user_ct * (configatron.twitter_retweet_percent/100.to_f)).round
79 total = configatron.twitter_retweet_max if total > configatron.twitter_retweet_max
80
81 @users = User.find_by_sql("SELECT id, account_id, screen_name, oauth_token, oauth_secret FROM users WHERE id != #{@base_user.id} ORDER BY RANDOM() LIMIT #{total}")#, :property => [ :id, :account_id, :screen_name, :oauth_token, :oauth_secret ])
82 @users.each do |user|
ff1ac34 @gleuch some more touch-ups, prepping for some styling.
gleuch authored
83 begin
84 twitter_connect(user)
85 @twitter_client.update(@tweet)
86 rescue
87 twitter_fail('An error has occured while trying to post a retweet to Twitter. Please try again.')
88 end
7db08a6 @gleuch first commit of retweet-fucker.
gleuch authored
89 end
ff1ac34 @gleuch some more touch-ups, prepping for some styling.
gleuch authored
90
91 haml :run
7db08a6 @gleuch first commit of retweet-fucker.
gleuch authored
92 else
ff1ac34 @gleuch some more touch-ups, prepping for some styling.
gleuch authored
93 @error = 'Could not load a tweet for this launch.'
94 haml :fail
7db08a6 @gleuch first commit of retweet-fucker.
gleuch authored
95 end
96 end
97 end
98
99
100 get '/' do
101 get_user unless session[:user].blank?
102 unless @user.blank?
ff1ac34 @gleuch some more touch-ups, prepping for some styling.
gleuch authored
103 haml :thanks
7db08a6 @gleuch first commit of retweet-fucker.
gleuch authored
104 else
ff1ac34 @gleuch some more touch-ups, prepping for some styling.
gleuch authored
105 haml :home
7db08a6 @gleuch first commit of retweet-fucker.
gleuch authored
106 end
107 end
108
109
110 # Initiate the conversation with Twitter
111 get '/connect' do
ff1ac34 @gleuch some more touch-ups, prepping for some styling.
gleuch authored
112 @title = 'Connect to Twitter'
113
7db08a6 @gleuch first commit of retweet-fucker.
gleuch authored
114 twitter_connect
ff1ac34 @gleuch some more touch-ups, prepping for some styling.
gleuch authored
115 begin
116 request_token = @twitter_client.request_token(:oauth_callback => 'http://localhost:4567/auth')
117 session[:request_token] = request_token.token
118 session[:request_token_secret] = request_token.secret
119 redirect request_token.authorize_url.gsub('authorize', 'authenticate')
120 rescue
121 twitter_fail('An error has occured while trying to authenticate with Twitter. Please try again.')
122 end
7db08a6 @gleuch first commit of retweet-fucker.
gleuch authored
123 end
124
125 # Callback URL to return to after talking with Twitter
126 get '/auth' do
ff1ac34 @gleuch some more touch-ups, prepping for some styling.
gleuch authored
127 @title = 'Authenticate with Twitter'
128
7db08a6 @gleuch first commit of retweet-fucker.
gleuch authored
129 twitter_connect
130 @access_token = @twitter_client.authorize(session[:request_token], session[:request_token_secret], :oauth_verifier => params[:oauth_verifier])
131
132 if @twitter_client.authorized?
ff1ac34 @gleuch some more touch-ups, prepping for some styling.
gleuch authored
133 begin
134 info = @twitter_client.info
135 rescue
136 twitter_fail and return
137 end
7db08a6 @gleuch first commit of retweet-fucker.
gleuch authored
138
139 @user = User.first_or_create(:account_id => info['id'])
140 @user.update_attributes(
141 :account_id => info['id'],
142 :screen_name => info['screen_name'],
143 :oauth_token => @access_token.token,
144 :oauth_secret => @access_token.secret
145 )
146
147 # Set and clear session data
148 session[:user] = @user.id
149 session[:account] = @user.account_id
150 session[:request_token] = nil
151 session[:request_token_secret] = nil
152
ff1ac34 @gleuch some more touch-ups, prepping for some styling.
gleuch authored
153 begin
154 twitter_connect(@user)
155 @twitter_client.update("#{twitter_sync_tweet} #{twiter_hashtag}")
156 rescue
157 twitter_fail('An error has occured while trying to post a tweet to Twitter. Please try again.')
158 end
7db08a6 @gleuch first commit of retweet-fucker.
gleuch authored
159 redirect '/'
160 else
161 redirect '/'
162 end
163 end
164
165 get '/run/*' do
ff1ac34 @gleuch some more touch-ups, prepping for some styling.
gleuch authored
166 @title = 'Launch Retweet Hell'
167
7db08a6 @gleuch first commit of retweet-fucker.
gleuch authored
168 if params[:splat].to_s == configatron.secret_launch_code.to_s
169 launch_retweet_hell
170 else
ff1ac34 @gleuch some more touch-ups, prepping for some styling.
gleuch authored
171 @error = 'WTF!? You ain\'t got access to this. Fuck off.'
172 haml :fail
7db08a6 @gleuch first commit of retweet-fucker.
gleuch authored
173 end
174 end
Something went wrong with that request. Please try again.