Last active
December 31, 2015 11:59
-
-
Save jesperes/7983623 to your computer and use it in GitHub Desktop.
Ruby script to check challenge compliance with XMAS Challenge 2011: http://coord.info/GC39Q62.
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
# | |
# Installation instructions for Nokogiri can be found at | |
# http://nokogiri.org/tutorials/installing_nokogiri.html | |
require 'nokogiri' | |
# | |
# Usage: ruby xmas-challenge.rb /path/to/MyFinds.gpx | |
if ARGV.size == 0 | |
puts "Usage: ruby #{$0} /path/to/MyFinds.gpx" | |
exit 1 | |
end | |
xml = Nokogiri::XML(File.open(ARGV[0])) | |
ns = {'groundspeak' => "http://www.groundspeak.com/cache/1/0/1"} | |
# Parses a date-string (on the format YYYY-MM-DD) and returns the | |
# december date in the range 1-24 as an integer. Returns nil if the | |
# date is not in the range. | |
def get_december_day(date) | |
if date =~ /\d\d\d\d-12-(.*)/ | |
d = $1.to_i | |
return d if d <= 24 | |
end | |
end | |
# Maps december dates (integers) to a hash with cache-info. | |
map = {} | |
# Stores a candidate match in the map. | |
def register_find(map, date, code, descr, type) | |
return unless d = get_december_day(date) | |
# Prefer "XMAS" caches. | |
return if map.has_key?(d) and map[d][:name]["XMAS"] and map[d][:type] == :published | |
map[d] = { :name => descr, :type => type, :code => code, :date => date } | |
end | |
# Scan gpx file for potential matches. | |
xml.xpath("//groundspeak:cache", ns).each do |cache| | |
wpt = cache.parent | |
code = wpt.css("name").text | |
descr = wpt.css("desc").text | |
register_find(map, wpt.css("time").text, code, descr, :placed) | |
if descr =~ /XMAS(20..)U[ -]*\b(\d+)\b/ | |
register_find(map, "#{$1}-12-#{$2}", code, descr, :published) | |
end | |
end | |
# Print out results | |
(1..24).each do |day| | |
m = map[day] | |
if not m | |
puts "Empty: #{day}" | |
else | |
code = m[:code] | |
puts "http://coord.info/#{m[:code]} #{m[:type]} on dec #{day}: #{m[:name]} (#{m[:date]}) " | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The script requires the Nokogiri XML parser, which you can get from here: http://nokogiri.org/.