Class: Maimailog::Crawler::History

Inherits:
Base
  • Object
show all
Includes:
Detail
Defined in:
lib/maimailog/crawler/history.rb

Overview

プレイ履歴を取得するクラス

Constant Summary collapse

DEFAULT_SLEEP_DURATION =

取得間隔の初期値

1

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#login

Constructor Details

#initializeHistory



65
66
67
68
# File 'lib/maimailog/crawler/history.rb', line 65

def initialize
  super
  @sleep_duration = DEFAULT_SLEEP_DURATION
end

Instance Attribute Details

#sleep_duration=(value) ⇒ Object (writeonly)

Sets the attribute sleep_duration



60
61
62
# File 'lib/maimailog/crawler/history.rb', line 60

def sleep_duration=(value)
  @sleep_duration = value
end

Instance Method Details

#fetch(page_num = 0) ⇒ Object

プレイ履歴ページからデータを取得して返す事前に login を済ませておく必要がある

Raises:



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/maimailog/crawler/history.rb', line 72

def fetch(page_num = 0)
  page = @agent.page
  raise 'require login.' if page.nil?

  if page.uri.to_s[/home\.html/]
    page = page.link_with(text: /[ ]+データ\z/).click
  end
  page = @agent.get("https://maimai-net.com/maimai-mobile/results.html?p=#{page_num}&#{page.uri.query[/sid=[\w]+/]}")

  links = page.search('//div[@id="accordion"]/ul/li//a')
  raise DataNotFoundError.new('maimai play data not found') if links.size == 0
  links.each do |elm|
    data = convert_data(@agent.get(elm[:href]))
    sleep(@sleep_duration)
    yield data
  end
end

#fetch_all(&block) ⇒ Object

プレイ履歴を最初から最後まで順番に取得していく



91
92
93
94
95
96
97
98
99
# File 'lib/maimailog/crawler/history.rb', line 91

def fetch_all(&block)
  0.upto(9999) do |page_num|
    begin
      fetch(page_num, &block)
    rescue DataNotFoundError
      break
    end
  end
end