<commit_before>#!/usr/bin/env ruby
# encoding : utf-8

# data/parser.rb
# https://github.com/bcylin/receipt-number-checker
#
# Created by Ben (c) 2012
# Released under the MIT License
# http://opensource.org/licenses/MIT


begin
    gem "nokogiri"
rescue Gem::LoadError
    system("gem install nokogiri")
    puts "nokogiri is now installed. Run the script again."
    exit
end

require "nokogiri"
require "open-uri"
require "json"

# Fetch data

url = "http://invoice.etax.nat.gov.tw/"
path = "./cache/invoice.html"

begin
    html = Nokogiri::HTML(open(url))
    puts "Fetched from URL"
rescue
    if not File.exists?(path)
        puts "Error loading HTML file"
        exit
    end

    file = File.expand_path(path)
    f = File.open(file)
    html = Nokogiri::HTML(f)
    f.close
    puts "Fetched from cache"
end


# Parse winning numbers in certain div with given id
# @param  {Nokogiri::HTML::Document} html
# @param  {String} div id
# @return {String} month name
# @return {Hash}   "prize name" => [numbers...]
def parse(html, id)
    nodes = html.xpath("//div[@id='#{id}']")
    month = nodes.xpath(".//h2")[0].content.match(/\d+-\d+.{1}/)[0]
    titles = nodes.xpath(".//span[@class='t18Red']/../../td[@class='title']")
    numbers = nodes.xpath(".//span[@class='t18Red']")
    draw = {}
    titles.each_with_index do |node, index|
        prize_name = (node.content == "頭獎") ? "頭獎至六獎" : node.content
        draw[prize_name] = numbers[index].content.split(/[^\d]/)
    end
    return month, draw
end

this_month, this_draw = parse(html, "area1")
last_month, last_draw = parse(html, "area2")<commit_msg>Fix NoMethodError due to DOM change<commit_after>#!/usr/bin/env ruby
# encoding : utf-8

# data/parser.rb
# https://github.com/bcylin/receipt-number-checker
#
# Created by Ben (c) 2012
# Released under the MIT License
# http://opensource.org/licenses/MIT


begin
    gem "nokogiri"
rescue Gem::LoadError
    system("gem install nokogiri")
    puts "nokogiri is now installed. Run the script again."
    exit
end

require "nokogiri"
require "open-uri"
require "json"

# Fetch data

url = "http://invoice.etax.nat.gov.tw/"
path = "./cache/invoice.html"

begin
    html = Nokogiri::HTML(open(url))
    puts "Fetched from URL"
rescue
    if not File.exists?(path)
        puts "Error loading HTML file"
        exit
    end

    file = File.expand_path(path)
    f = File.open(file)
    html = Nokogiri::HTML(f)
    f.close
    puts "Fetched from cache"
end


# Parse winning numbers in certain div with given id
# @param  {Nokogiri::HTML::Document} html
# @param  {String} div id
# @return {String} month name
# @return {Hash}   "prize name" => [numbers...]
def parse(html, id)
    nodes = html.xpath("//div[@id='#{id}']")
    month = nodes.xpath(".//h2")[1].content.match(/\d+-\d+.{1}/)[0]
    titles = nodes.xpath(".//span[@class='t18Red']/../../td[@class='title']")
    numbers = nodes.xpath(".//span[@class='t18Red']")
    draw = {}
    titles.each_with_index do |node, index|
        prize_name = (node.content == "頭獎") ? "頭獎至六獎" : node.content
        draw[prize_name] = numbers[index].content.split(/[^\d]/)
    end
    return month, draw
end

this_month, this_draw = parse(html, "area1")
last_month, last_draw = parse(html, "area2")
{'fraction_non_alphanumeric': 0.16851168511685116, 'fraction_numerical': 0.00922509225092251, 'mean_word_length': 3.2467362924281984, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '6452486', 'n_tokens_mistral': 1220, 'n_tokens_neox': 1211, 'n_words': 282}