Skip to content

Instantly share code, notes, and snippets.

@m-atthieu
Created June 1, 2020 00:24
Show Gist options
  • Select an option

  • Save m-atthieu/7e3993c11d9acebe4a348146ab3e1574 to your computer and use it in GitHub Desktop.

Select an option

Save m-atthieu/7e3993c11d9acebe4a348146ab3e1574 to your computer and use it in GitHub Desktop.
# coding: utf-8
require 'net/http'
require 'uri'
require 'nokogiri'
require 'open-uri'
require 'json'
OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE
now = Time.now
event_start = Time.new(2020, 5, 31, 5, 00, 00, "+02:00")
event_end = Time.new(2020, 6, 6, 12, 00, 00, "+02:00")
$debug = false
if now < event_start or now > event_end then
#return unless $debug
end
###############################################################################
# Total Point
###############################################################################
def page_get stage, rank
user_id = "123456789"
page = (rank.to_f / 100).ceil.to_i
url_glb = "https://optc-ww.channel.or.jp/wa_mission/en/ranking.php?stage=#{stage}&page=#{page}&user_id=#{user_id}"
url = url_glb
# puts " [page_get] opening #{url}" if $debug
return Nokogiri::HTML(open(url).read)
end
def point_get stage, rank
#puts "point_get stage=#{stage} rank=#{rank} id='stage#{stage}_rank_#{rank}'" if $debug
doc = page_get stage, rank
#list = doc.xpath('//li[div[@class="rankNo"]]')
#list = doc.xpath("//ol[@class='totalRankList']/li[starts-with(@id, 'stage#{stage}-')]")
list = doc.xpath("//li[@id='stage#{stage}_rank_#{rank}']")
#puts list if $debug
if true and rank <= 3 then
list.each do |li|
#puts "## #{li}" if $debug
#alt = li.xpath('./div[@class="rankNo"]/h2/img/@alt').first.content
#alt = alt.gsub(/[^\d]/, '').to_i
#puts " alt #{alt.inspect}" if $debug
#if alt == rank.to_i
pts = li.xpath('./div/p[@class="score"]/span[@class="count"]/text()')
#puts " pts #{pts.inspect}" if $debug
return pts.first.content.to_i
#end
end
else
list.each do |li|
div = li.xpath('./div[@class="rankNo"]/h2/text()')
puts div if $debug
#next if div.length == 0
#_rank = div.first.content.gsub(/[^\d]/, '').to_i
#if _rank == rank then
pts = li.xpath('./div/p[@class="score"]/span[@class="count"]/text()')
#puts li if $debug
return pts.first.content.gsub(/[^\d]/, '').to_i
#end
end
end
-1
end
def ranking_get stage
doc = page_get stage, 1
# on JPN, it was section, not div for first element
# also last div was class=rankingYourScore
#ranking = doc.xpath("//div[contains(@class, 'stage#{stage}')]/div[contains(@class, 'current-position')]/div[@class='your-score']/dl[1]/dd[1]/text()").first.content.to_i
ranking = doc.xpath("//div[@class='your-score']/dl[1]/dd[1]/text()").first.content.to_i
#points = doc.xpath("//div[contains(@class, 'stage#{stage}')]/div[contains(@class, 'current-position')]/div[@class='your-score']/dl[1]/dd[2]/text()").first.content.to_i
points = doc.xpath("//div[@class='your-score']/dl[1]/dd[2]/text()").first.content.to_i
return ranking, points
end
ranks = [ 1, 2, 3, 10, 50, 100, 500, 1000, 3000, 5000, 10000 ]
#ranks = [ 1, 2, 3, 10 ]
points = {
:kaido => [],
:speed => [],
:holdem => [],
:hawkins => []
}
stages = { :kaido => 1, :speed => 2, :holdem => 3, :hawkins => 4 }
#stages = { :kaido => 1 }
stages.each do | name, num |
ranks.each do |rank|
point = point_get num, rank
puts "#{name} #{num} #{rank} #{point}" if $debug
points[name] << point
end
end
stages.each do |name, num|
filename = "data-cutoff-#{name}.csv"
open("#{__dir__}/#{filename}", 'a') do |file|
file.puts "#{now.strftime('%s')},#{points[name].join(',')}"
end
system("echo put #{__dir__}/#{filename} /optc/events/wa_mission/#{filename} | ftp ftp.host.com") unless $debug
end
rankings = {
:kaido => 15000,
:speed => 15000,
:holdem => 15000,
:hawkins => 15000
}
stages.each do |name, num|
ranking = ranking_get num
rankings[name] = ranking
end
puts rankings if $debug
filename = "data-part-1-ranking.csv"
open("#{__dir__}/#{filename}", 'a') do |file|
file.puts "#{now.strftime('%s')},#{rankings[:kaido].join(',')},#{rankings[:speed].join(',')},#{rankings[:holdem].join(',')},#{rankings[:hawkins].join(',')}"
end
system("echo put #{__dir__}/#{filename} /optc/events/wa_mission/#{filename} | ftp ftp.host.com") unless $debug
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment