Skip to content

Instantly share code, notes, and snippets.

@maliqq
Created December 26, 2012 16:02
Show Gist options
  • Save maliqq/4381083 to your computer and use it in GitHub Desktop.
Save maliqq/4381083 to your computer and use it in GitHub Desktop.
# encoding: utf-8
require 'open-uri'
url = "http://ru.wikipedia.org/w/index.php?title=%D0%A1%D0%BF%D0%B8%D1%81%D0%BE%D0%BA_%D0%BC%D0%B5%D1%82%D1%80%D0%BE%D0%BF%D0%BE%D0%BB%D0%B8%D1%82%D0%B5%D0%BD%D0%BE%D0%B2&action=raw"
wiki = open(url).read
result = wiki.lines.map { |line|
next unless line.start_with?('*')
#* [[Лондон]] ([[1863]]) — [[Лондонский метрополитен]] — 11 линий, 408 км, 260 станций
parts = line.sub("''", '').sub("): ", ") —").sub("]]: ", "]] —").split('—')
data = {}
if parts[0] =~ /\[\[(.*)\]\].*\((.*)\)/
data.update(city: $1, year: $2)
else
parts[0] =~ /\[\[(.*)\]\]/
data[:city] = $1
end
data[:name] = parts[1]
metrics = parts[2]
if data[:year] =~ /(\d{4})/
data[:year] = $1.to_i
end
if metrics =~ /([^\s]+)\s*км/
data[:length] = $1.index(',') ? $1.sub(',', '.').to_f : $1.to_i
end
if metrics =~ /(\d+) лин/
data[:lines] = $1.to_i
end
if metrics =~ /(\d+) стан/
data[:stations] = $1.to_i
end
#data[:name] = data[:name].split(']]', 2)[0]
data[:length].nil? ? nil : data
}.compact
puts result.sort_by { |row| row[:km] }.reverse.map { |data|
"| [[#{data[:city]}]] || #{data[:name]} || #{data[:year]} || #{data[:lines]} || #{data[:stations]} || #{data[:length]}"
}.join("\n|-\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment