<commit_before>module Scrape
  class Official
    def initialize(user_id, id, pass)
      @user = User.find_by(id: user_id)
      @base = %(http://p.eagate.573.jp/game/2dx/22/p/djdata/music.html)

      @agent = Mechanize.new
      url = %(https://p.eagate.573.jp/gate/p/login.html)
      @agent.get(url)
      @agent.page.encoding = %(UTF-8)
      @count = 0

      # ログイン処理
      form = @agent.page.form_with(method: 'POST')
      form.KID = id
      form.pass = pass
      @agent.submit(form)
    end

    def score_get
      return false unless incorrect_user
      recent = Abilitysheet::Application.config.iidx_version - 1
      recent.downto(4) do |version|
        # version管理
        url = @base + %(?list=#{version}&play_style=0&s=1&page=1#musiclist)
        @agent.get(url)

        # スコア取得処理
        next if normal_get(version)
        return false
      end
      true
    end

    private

    def incorrect_user
      iidxid = @user.iidxid
      check = @agent.get('http://p.eagate.573.jp/game/2dx/22/p/djdata/status.html')
      data = check.body.kconv(Kconv::UTF8, Kconv::SJIS)
      html = Nokogiri::HTML.parse(data, nil, 'UTF-8')
      remote_iidxid = html.xpath('//div[@class="dj_info_box"]/table/tbody/tr/td')[2].text
      return true if iidxid == remote_iidxid
      false
    end

    def last_page_check(url)
      check = @agent.get(url)
      data = check.body.kconv(Kconv::UTF8, Kconv::SJIS)
      html = Nokogiri::HTML.parse(data, nil, 'UTF-8')

      return true if html.to_s.index('データがみつかりません')
    end

    def page_get
      @agent.page.links.each do |link|
        title = error_title_check(link.text.to_s)
        element_get('http://p.eagate.573.jp' + link.href.to_s) if Sheet.exists?(title: title) || title.index('gigadelic') || title.index('Innocent Walls')
      end
    end

    def normal_get(version)
      page = 1
      while 1 > 0
        url = @base + %(?list=#{version}&play_style=0&s=1&page=#{page}#musiclist)
        html = Nokogiri::HTML.parse(
          @agent.get(url).body.kconv(Kconv::UTF8, Kconv::SJIS),
          nil,
          'UTF-8'
        )
        # 最後尾のページになったら次のバージョンへ
        return true if last_page_check(url)

        # メンテナンスかIDPASSエラーなら何もせずに終了
        return false unless error_check(html)

        page_get

        page += 1
      end
    end

    # no_value周り要修正
    def data_register(title, score, miss, l_v)
      sheet = Sheet.find_by(title: title)
      return unless sheet

      pg = pg_s(score)
      g  = g_s(score)

      miss = no_value(miss)
      pg   = no_value(pg)
      g    = no_value(g)

      score = pg.to_i * 2 + g.to_i
      @user.update_with_logs({ 'sheet_id' => sheet.id, 'state' => l_v }, score, miss)
      @count += 1
    end

    def data_parse(title, tmp_score, tmp_miss, lamp)
      hash = Hash.new(2)
      hash.store('gigadelic', 1)
      hash.store('Innocent Walls', 1)

      t = hash[title]
      score = tmp_score[t + 3].text
      miss  = tmp_miss[t].text

      l_v = lamp_return(lamp[t].attribute('src').value)<commit_msg>refs #150 公式同期の不具合を修正<commit_after>module Scrape
  class Official
    def initialize(user_id, id, pass)
      @user = User.find_by(id: user_id)
      @base = %(http://p.eagate.573.jp/game/2dx/22/p/djdata/music.html)

      @agent = Mechanize.new
      url = %(https://p.eagate.573.jp/gate/p/login.html)
      @agent.get(url)
      @agent.page.encoding = %(UTF-8)
      @count = 0

      # ログイン処理
      form = @agent.page.form_with(method: 'POST')
      form.KID = id
      form.pass = <PASSWORD>
      @agent.submit(form)
    end

    def score_get
      return false unless incorrect_user
      recent = Abilitysheet::Application.config.iidx_version - 1
      recent.downto(4) do |version|
        # version管理
        url = @base + %(?list=#{version}&play_style=0&s=1&page=1#musiclist)
        @agent.get(url)

        # スコア取得処理
        next if normal_get(version)
        return false
      end
      true
    end

    private

    def incorrect_user
      iidxid = @user.iidxid
      check = @agent.get('http://p.eagate.573.jp/game/2dx/22/p/djdata/status.html')
      data = check.body.kconv(Kconv::UTF8, Kconv::SJIS)
      html = Nokogiri::HTML.parse(data, nil, 'UTF-8')
      remote_iidxid = html.xpath('//div[@class="dj_info_box"]/table/tbody/tr/td')[2].text
      return true if iidxid == remote_iidxid
      false
    end

    def last_page_check(url)
      check = @agent.get(url)
      data = check.body.kconv(Kconv::UTF8, Kconv::SJIS)
      html = Nokogiri::HTML.parse(data, nil, 'UTF-8')

      return true if html.to_s.index('データがみつかりません')
    end

    def page_get
      @agent.page.links.each do |link|
        title = error_title_check(link.text.to_s)
        element_get('http://p.eagate.573.jp' + link.href.to_s) if Sheet.exists?(title: title) || title.index('gigadelic') || title.index('Innocent Walls')
      end
    end

    def normal_get(version)
      page = 1
      while 1 > 0
        url = @base + %(?list=#{version}&play_style=0&s=1&page=#{page}#musiclist)
        html = Nokogiri::HTML.parse(
          @agent.get(url).body.kconv(Kconv::UTF8, Kconv::SJIS),
          nil,
          'UTF-8'
        )
        # 最後尾のページになったら次のバージョンへ
        return true if last_page_check(url)

        # メンテナンスかIDPASSエラーなら何もせずに終了
        return false unless error_check(html)

        page_get

        page += 1
      end
    end

    # no_value周り要修正
    def data_register(title, score, miss, l_v)
      sheet = Sheet.find_by(title: title)
      return unless sheet

      pg = pg_s(score)
      g  = g_s(score)

      miss = no_value(miss)
      pg   = no_value(pg)
      g    = no_value(g)

      score = pg.to_i * 2 + g.to_i
      user_score = @user.scores.find_by(sheet_id: sheet.id)
      user_score.update_with_logs({ 'sheet_id' => sheet.id, 'state' => l_v }, score, miss)
      @count += 1
    end

    def data_parse(title, tmp_score, tmp_miss, lamp)
      hash = Hash.new(2)
      hash.store('gigadelic', 1)
      hash.store('Innocent Walls', 1)

      t = hash[title]
      score = tmp_score[t + 3].text
      miss  = tmp_miss[t].text

      l_v = lamp_return(lamp[t].attribute('src').value)
{'fraction_non_alphanumeric': 0.13632666341304703, 'fraction_numerical': 0.014803969415975272, 'mean_word_length': 2.2982832618025753, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 10, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '79233', 'n_tokens_mistral': 2458, 'n_tokens_neox': 2344, 'n_words': 408}