Created
February 5, 2017 22:18
-
-
Save missingno15/bde766b12f80e79b8227d19e94d3c8d6 to your computer and use it in GitHub Desktop.
showroom bot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "active_support/core_ext/numeric/time" | |
require "capybara" | |
require "capybara/poltergeist" | |
require "dotenv" | |
require "logger" | |
require "json" | |
require "pry" | |
Capybara.register_driver :poltergeist do |app| | |
Capybara::Poltergeist::Driver.new(app, {js_errors: false}) | |
end | |
Capybara.default_driver = :poltergeist | |
Dotenv.load | |
class ShowroomBot | |
def initialize | |
@browser = Capybara.current_session | |
@logger = Logger.new("showroom_bot.log", datetime_format: "%Y-%m-%d %p%I:%M:%S") | |
@last_started = nil | |
@accessed_onlives = [] | |
@current_free_gift_count = Array.new(5, 0) | |
logger.info "ShowroomBot initialized" | |
browser.visit SHOWROOM_BASE_URL | |
login | |
logger.info "Logged in at #{Time.now}" | |
@onlives = get_onlives() | |
end | |
def run | |
last_started = Time.now | |
loop do | |
login if logged_out? | |
if (Time.now - last_started) < 1.hour && !free_gift_gauge_full? | |
if onlives.any? | |
live = onlives.pop | |
browser.visit "#{SHOWROOM_BASE_URL}/#{live['room_url_key']}" | |
# wait 40 seconds for the free gifts to increase | |
sleep 40 | |
current_free_gift_count = get_current_free_gift_count() | |
if free_gift_gauge_full? | |
logger.info "Free star gifts have been fully replenished" | |
end | |
accessed_onlives << live | |
else | |
# we want to refresh the onlives but if we | |
# access all the available streams under an | |
# hour, we don't want to keep hammering SHOWROOM | |
# with refresh requests | |
sleep(rand(30..60)) | |
onlives = get_onlives() | |
end # if onlives.any? | |
end # if Time.now ... | |
# update last_started if over an hour | |
# clear out accessed_onlives | |
if (Time.now - last_started) > 1.hour | |
last_started = Time.now | |
accessed_onlives = [] | |
logger.info "SHOWROOM time limit has been reset" | |
end | |
end # loop | |
end | |
private | |
SHOWROOM_BASE_URL = "https://www.showroom-live.com".freeze | |
ONLIVE_ENDPOINT = "#{SHOWROOM_BASE_URL}/api/live/onlives".freeze | |
GENRES = { | |
0 => "Popularity", | |
101 => "Music", | |
102 => "Idol", | |
103 => "Talent Model" | |
} | |
attr_reader :browser, :logger | |
attr_accessor :accessed_onlives, | |
:current_free_gift_count, | |
:last_started, | |
:onlives | |
def login | |
login_button = browser.all("ul.side-nonuser-menu li").last | |
login_button.click | |
# fill in credentials | |
browser.fill_in "account_id", with: ENV["SHOWROOM_USERNAME"] | |
browser.fill_in "password", with: ENV["SHOWROOM_PASSWORD"] | |
# click login button | |
browser.all("a#js-login-submit").first.click | |
end | |
def logged_out? | |
browser.all("div.side-nonuser-menu").any? | |
end | |
def logged_in? | |
!logged_out? | |
end | |
def get_onlives | |
json = JSON.parse(Net::HTTP.get(URI(ONLIVE_ENDPOINT))) | |
lives = json.fetch("onlives").reduce([]) do |acc, genre| | |
if GENRES[genre["genre_id"]] | |
acc += genre["lives"] | |
else | |
acc | |
end | |
end | |
lives.reject do |live| | |
accessed_onlives.any? do |accessed_onlive| | |
accessed_onlive["room_url_key"] == live["room_url_key"] | |
end | |
end | |
end | |
def get_current_free_gift_count | |
browser.all("div.gift-free-num-label").map do |node| | |
node.text[/(\d{1,})/,1].to_i | |
end | |
end | |
def free_gift_gauge_full? | |
current_free_gift_count.all? { |gift_count| gift_count == 99 } | |
end | |
end | |
showroom_bot = ShowroomBot.new | |
showroom_bot.run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment