Sha256: e896ea6193af0fa682a902ef6a4921795c8835f9e5315fab20d447e7b1095270

Contents?: true

Size: 1.65 KB

Versions: 7

Compression:

Stored size: 1.65 KB

Contents

require 'yaml'
require 'NeonRAW'

# Creates and authenticates the client.
# @!method login(config)
# @param config [Hash] The data loaded from settings.yaml.
# @return [NeonRAW::Clients::Script] Returns the client.
def login(config)
  reddit_exception_handling do
    client = NeonRAW.script(config['username'], config['password'],
                            config['client_id'], config['secret'],
                            user_agent: 'Flairbot by /u/SirNeon')
    return client
  end
end

# Flairs submissions based on keywords in their title.
# @!method flair_shit(client, subreddit)
# @param client [NeonRAW::Clients::Script] The client.
# @param subreddit [String] The subreddit to scan.
def flair_shit(client, subreddit)
  reddit_exception_handling do
    subreddit = client.subreddit subreddit
    submissions = subreddit.new limit: 100
    submissions.each do |submission|
      next if submission.flair?
      case submission.title
      when /\[meta\]/i then subreddit.set_flair submission, 'Meta',
                                                css_class: 'meta'
      when /test/i then subreddit.set_flair submission, 'Test'
      end
    end
    break
  end
end

# Handles Reddit exceptions.
# @!method reddit_exception_handling
# @param block [&block] The block to execute.
def reddit_exception_handling
  include NeonRAW::Errors
  loop do
    begin
      yield
    rescue InvalidCredentials, InvalidOAuth2Credentials => error
      abort(error.message)
    rescue CouldntReachServer, ServiceUnavailable
      sleep(5)
      redo
    end
  end
end

def main
  config = YAML.load_file('settings.yaml')
  client = login(config)
  flair_shit client, client.me.name
end

main

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
NeonRAW-0.1.7 examples/flairbot.rb
NeonRAW-0.1.6 examples/flairbot.rb
NeonRAW-0.1.5 examples/flairbot.rb
NeonRAW-0.1.4 examples/flairbot.rb
NeonRAW-0.1.3 examples/flairbot.rb
NeonRAW-0.1.2 examples/flairbot.rb
NeonRAW-0.1.1 examples/flairbot.rb