Created
October 22, 2016 16:32
-
-
Save emorima/fbf52f38067a835e65e0554a0508fe8e to your computer and use it in GitHub Desktop.
RailsGirlsTools
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 'yaml' | |
PAGE_PATH = "./railsgirls/events.html" | |
class Event | |
def initialize(new_line) | |
@lines = [ new_line ] | |
@country = nil | |
@city = nil | |
@date = nil | |
end | |
attr_reader :country, :city, :date | |
def add(line) | |
@lines << line | |
end | |
def parse | |
@lines.each do |l| | |
if l =~ /<h3>([\S\s]+)<\/h3>/ | |
info = $1 | |
if info.include?(", ") && info =~ /^([\S\s,]+), ([\S\s]+)+$/ | |
@city = $1 | |
@country = $2 | |
else | |
@city, @country = fix_city(info) | |
end | |
elsif l =~ /<p>([\s\S]+)<\/p>/ | |
@date = $1 | |
end | |
end | |
STDERR.puts @lines unless @country | |
end | |
private | |
def fix_city(line) | |
case line | |
when "Los Angeles,CA USA" | |
return ["Los Angeles, CA", "USA"] | |
when "Hong Kong" | |
return [line, "China"] | |
when "Bogotá" | |
return [line, "Colombia"] | |
when "Belgrade" | |
return [line, "Republika Srbija"] | |
when "Luxembourg" | |
return ["Luxembourg City", "Luxembourg"] | |
else | |
STDOUT.puts "warning: #{info}" | |
end | |
end | |
end | |
def get_events(events) | |
File.open(PAGE_PATH) do |fd| | |
f_on_event = false | |
s = nil | |
event = nil | |
while s = fd.gets | |
s.strip! | |
if f_on_event | |
event.add s | |
if s =~ /^<\/a>/ | |
event.parse | |
events << event | |
f_on_event = false | |
end | |
elsif s =~ / class=\"event\"/ || s =~ / class=\'event\'/ | |
next if is_commented_line?(s) | |
f_on_event = true | |
event = Event.new(s) | |
end | |
end | |
end | |
rescue | |
STDERR.puts $! | |
end | |
def is_commented_line?(line) | |
return true if line =~ /^<!-- / | |
false | |
end | |
events = [] | |
get_events(events) | |
sum = {} | |
events.each do |event| | |
sum[event.country] ||= {} | |
sum[event.country][event.city] ||= [] | |
sum[event.country][event.city] << event.date | |
end | |
#File.open("events.dump", "w") do |fd| | |
# fd.puts YAML.dump sum | |
#end | |
File.open("events.csv", "w") do |fd| | |
sum.keys.sort.each do |country| | |
cities = sum[country] | |
sum[country].keys.sort.each do |city| | |
sum[country][city].each do |d| | |
fd.puts sprintf("%-20s\t%-30s\t#{d}", country, city, d) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment