Class: Elephrame::Bots::EbooksBot
- Inherits:
-
GenerativeBot
- Object
- BaseBot
- GenerativeBot
- Elephrame::Bots::EbooksBot
- Defined in:
- lib/elephrame/mix/bots.rb
Overview
A basic Ebooks bot template
Constant Summary collapse
- PrivacyLevels =
['public', 'unlisted', 'private', 'direct']
- APILimit =
280
- RetryTime =
'6m'
Constants inherited from GenerativeBot
GenerativeBot::SavedFileName, GenerativeBot::SavedFilterFileName
Constants inherited from BaseBot
BaseBot::FNGabLink, BaseBot::NoBotRegex
Instance Attribute Summary collapse
-
#old_id ⇒ Object
readonly
Returns the value of attribute old_id.
-
#scrape_filter ⇒ Object
readonly
Returns the value of attribute scrape_filter.
-
#update_interval ⇒ Object
readonly
Returns the value of attribute update_interval.
Attributes inherited from GenerativeBot
#char_limit, #cw, #filter, #filter_by, #filter_filename, #filter_words, #following, #model, #model_filename, #model_hash, #retry_limit, #visibility
Attributes included from Command
#cmd_hash, #cmd_regex, #commands, #not_found, #prefix
Attributes included from Reply
Attributes included from Scheduler
Attributes included from Streaming
Attributes inherited from BaseBot
#client, #failed, #max_retries, #strip_html, #username
Instance Method Summary collapse
-
#default_help ⇒ Object
overloads the super’s help method.
-
#fetch_new_posts ⇒ Object
Fetch posts from the accounts the bot follows.
-
#fetch_old_posts ⇒ Object
Method to go and fetch all posts should be ran first.
-
#initialize(interval, opts = {}) ⇒ EbooksBot
constructor
Creates a new Ebooks bot.
-
#run ⇒ Object
Run the Ebooks bot.
Methods inherited from GenerativeBot
#add_filter_word, #add_privileged_command, #filter_and_post, #load_file, #save_file
Methods included from Command
#add_command, #if_not_found, #run_commands, #set_help, #set_prefix, #setup_command
Methods included from Reply
#reply, #reply_with_mentions, #run_reply
Methods included from Scheduler
#run_scheduled, #setup_scheduler
Methods included from Streaming
Methods inherited from BaseBot
backup_method, #fetch_account_id, #fetch_list_id, #find_ancestor, #no_bot?, #post
Constructor Details
#initialize(interval, opts = {}) ⇒ EbooksBot
Creates a new Ebooks bot
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/elephrame/mix/bots.rb', line 124 def initialize(interval, opts = {}) super # add our manual update command add_privileged_command 'update' do fetch_new_posts end # set some defaults for our internal vars level = PrivacyLevels.index(opts[:scrape_privacy]) || 0 @scrape_filter = /(#{PrivacyLevels[0..level].join('|')})/ @update_interval = opts[:update_interval] || '2d' # if we don't have what a newest post id then we fetch them # for each account if @model_hash[:last_id].empty? @old_id = {} @following.each do |account| # get the newest post from this account and save the id newest_id = @client.statuses(account, exclude_reblogs: true, limit: 1).first.id @model_hash[:last_id][account] = newest_id @old_id[account] = newest_id end end # if our model's token are empty that means we have an empty model fetch_old_posts if @model_hash[:model].tokens.empty? end |
Instance Attribute Details
#old_id ⇒ Object (readonly)
Returns the value of attribute old_id.
98 99 100 |
# File 'lib/elephrame/mix/bots.rb', line 98 def old_id @old_id end |
#scrape_filter ⇒ Object (readonly)
Returns the value of attribute scrape_filter.
98 99 100 |
# File 'lib/elephrame/mix/bots.rb', line 98 def scrape_filter @scrape_filter end |
#update_interval ⇒ Object (readonly)
Returns the value of attribute update_interval.
98 99 100 |
# File 'lib/elephrame/mix/bots.rb', line 98 def update_interval @update_interval end |
Instance Method Details
#default_help ⇒ Object
overloads the super’s help method
303 304 305 306 307 308 |
# File 'lib/elephrame/mix/bots.rb', line 303 def default_help txt = [] txt << '!update -- tells the bot to manually fetch new posts' txt << super txt.join '\n' end |
#fetch_new_posts ⇒ Object
Fetch posts from the accounts the bot follows
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/elephrame/mix/bots.rb', line 224 def fetch_new_posts begin # set up some vars for tracking our progress added_posts = { statuses: [], mentions: [] } api_calls = 1 errored = false # for each account we're following @following.each do |account| # get 40 posts at a time, where we left off posts = @client.statuses(account, exclude_reblogs: true, limit: 40, since_id: @model_hash[:last_id][account]) # while we have posts to process and we haven't # gotten near the api limit while not posts.size.zero? and api_calls < APILimit posts.reverse_each do |post| # save our post id for next loop @model_hash[:last_id][account] = post.id # if the post matches our set visibility we add it to our hash if post.visibility =~ @scrape_filter added_posts = add_post_to_hash post, added_posts end end # fetch more posts posts = @client.statuses(account, exclude_reblogs: true, limit: 40, since_id: @model_hash[:last_id][account]) api_calls += 1 end # in case we hit our api limit between calls break if api_calls >= APILimit end rescue # if we've hit here then we've errored out errored = true ensure # consume our new posts, and add them to our original hash @model_hash[:model].consume! added_posts if api_calls >= APILimit or errored @scheduler.in RetryTime do fetch_new_posts end end # then we save save_file(@model_filename, @model_hash.collect {|key, value| value.to_hash }.to_yaml) end end |
#fetch_old_posts ⇒ Object
Method to go and fetch all posts
should be ran first
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/elephrame/mix/bots.rb', line 160 def fetch_old_posts begin # init some vars to keep track of where we are api_calls = 1 errored = false new_posts = { statuses: [], mentions: [] } # for each account we're following @following.each do |account| # okay so # we keep track of how many get requests we're doing and before # the limit (300) we schedule for 5min and go on, saving what we got posts = @client.statuses(account, exclude_reblogs: true, limit: 40, max_id: @old_id[account]) # while we still have posts and haven't gotten near the api limit while not posts.size.zero? and api_calls < APILimit posts.each do |post| # add the new post to our hash if post.visibility =~ @scrape_filter new_posts = add_post_to_hash post, new_posts end # set our cached id to the latest post id @old_id[account] = post.id end # fetch more posts posts = @client.statuses(account, exclude_reblogs: true, limit: 40, max_id: @old_id[account]) api_calls += 1 end break if api_calls >= APILimit end rescue errored = true ensure # consume our posts, and then save our model @model_hash[:model].consume! new_posts save_file(@model_filename, @model_hash.collect {|key, value| value.to_hash }.to_yaml) # if we have more than our limit of api calls # or we errored out that means we need to check again if api_calls >= APILimit or errored @scheduler.in RetryTime do fetch_old_posts end end end end |
#run ⇒ Object
Run the Ebooks bot
288 289 290 291 292 293 294 295 296 |
# File 'lib/elephrame/mix/bots.rb', line 288 def run # set up our scheduler to scrape posts @scheduler.repeat @update_interval do fetch_new_posts end # call generativebot's run method super end |