2013-09-18 13:33:53 +04:00
|
|
|
# -*- coding: utf-8 -*-
|
2014-02-05 04:31:05 +04:00
|
|
|
extend Math
|
2013-09-18 13:33:53 +04:00
|
|
|
require 'rake'
|
|
|
|
|
2013-11-22 23:48:36 +04:00
|
|
|
# The year and title of tournament
|
2014-02-05 03:50:47 +04:00
|
|
|
@year = '2014'
|
|
|
|
@tournament ='1-tabiyas'
|
2013-11-22 23:48:36 +04:00
|
|
|
|
2013-11-23 12:01:00 +04:00
|
|
|
# Ask to choose the directory of PGN file
|
|
|
|
@ask_dir = true
|
|
|
|
|
2013-11-22 23:48:36 +04:00
|
|
|
require 'yaml'
|
|
|
|
file_dir = File.dirname(__FILE__)
|
|
|
|
yaml_file = File.expand_path("#{@year}/#{@tournament}/players.yml", file_dir)
|
|
|
|
@config = YAML.load_file yaml_file
|
|
|
|
|
|
|
|
# Sort players in numerical order
|
|
|
|
@config.sort! { |x,y| x['number'] <=> y['number'] }
|
|
|
|
|
|
|
|
# Associate a 'lichess' nickname with player's name
|
|
|
|
@player_lichess = {}
|
|
|
|
@config.each do |player|
|
|
|
|
lichess = player['lichess']
|
|
|
|
@player_lichess[lichess] = player['lor']
|
|
|
|
end
|
|
|
|
|
2014-02-05 04:31:05 +04:00
|
|
|
# Fix the player's name in PGN
|
2013-11-22 23:48:36 +04:00
|
|
|
def fix_player color, name
|
|
|
|
str = File.open('temp.pgn', 'r') { |f| f.read }
|
|
|
|
player_regex = Regexp.new "^\\[#{color.capitalize} \".*\"\\]$"
|
|
|
|
parts = str.partition player_regex
|
|
|
|
File.open('temp.pgn', 'w+') do |f|
|
|
|
|
f.write(parts[0] + "[#{color.capitalize} \"#{name}\"]" + parts[2])
|
|
|
|
end
|
2014-02-05 04:31:05 +04:00
|
|
|
puts "The 'lichess' name of #{color} player is changed to '#{name}'"
|
2013-11-22 23:48:36 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Fix the game date in PGN: YYYY-MM-DD -> YYYY.MM.DD
|
2013-09-18 13:33:53 +04:00
|
|
|
def fix_date
|
|
|
|
str = File.open('temp.pgn', 'r') { |f| f.read }
|
|
|
|
parts = str.partition /^\[Date \"\d{4}-\d{2}-\d{2}\"\]$/
|
|
|
|
if parts[1].empty?
|
|
|
|
puts "The game date is already correct"
|
|
|
|
else
|
|
|
|
parts[1].gsub! '-', '.'
|
|
|
|
File.open('temp.pgn', 'w+') do |f|
|
|
|
|
f.write(parts[0] + parts[1] + parts[2])
|
|
|
|
end
|
|
|
|
puts "The game date corrected"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-22 23:48:36 +04:00
|
|
|
# Returns the 'lichess' name of player to be corrected
|
2013-11-23 12:01:00 +04:00
|
|
|
def choose_player
|
2014-02-05 04:31:05 +04:00
|
|
|
print "Would you like to correct the player's name? (Y/N)> "
|
2013-11-22 23:48:36 +04:00
|
|
|
answer = $stdin.gets.chomp
|
|
|
|
if ['Yes', 'yes', 'Y', 'y'].include? answer
|
2014-02-05 04:31:05 +04:00
|
|
|
indention = (log(@config.size) / log(10)).floor + 1
|
2013-11-22 23:48:36 +04:00
|
|
|
@config.each do |player|
|
2014-02-05 04:31:05 +04:00
|
|
|
puts "%#{indention}.0f. %s" % [ player['number'], player['lor'] ]
|
2013-11-22 23:48:36 +04:00
|
|
|
end
|
|
|
|
|
2014-02-05 04:31:05 +04:00
|
|
|
print "Put the player's number> "
|
2013-11-22 23:48:36 +04:00
|
|
|
num = Integer $stdin.gets.chomp
|
|
|
|
@config[num-1]['lichess']
|
|
|
|
else
|
|
|
|
abort
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-23 12:01:00 +04:00
|
|
|
# Returns possible directories to put PGN file in
|
|
|
|
def pgn_dirs
|
2013-09-18 13:33:53 +04:00
|
|
|
str = File.open('temp.pgn', 'r') { |f| f.read }
|
|
|
|
|
|
|
|
date = str.scan(/^\[Date \"(.*)\"\]$/)[0][0]
|
2013-11-22 23:48:36 +04:00
|
|
|
date.gsub! '.', '-'
|
|
|
|
white_lichess = str.scan(/^\[White \"(.*)\"\]$/)[0][0]
|
|
|
|
white = @player_lichess[white_lichess]
|
|
|
|
black_lichess = str.scan(/^\[Black \"(.*)\"\]$/)[0][0]
|
|
|
|
black = @player_lichess[black_lichess]
|
2013-09-18 13:33:53 +04:00
|
|
|
|
2013-11-22 23:48:36 +04:00
|
|
|
unless white
|
2014-02-05 04:31:05 +04:00
|
|
|
puts "Could not find white player '#{white_lichess}'"
|
2013-11-23 12:01:00 +04:00
|
|
|
name = choose_player
|
2013-11-22 23:48:36 +04:00
|
|
|
fix_player 'white', name
|
|
|
|
white = @player_lichess[name]
|
|
|
|
end
|
|
|
|
unless black
|
|
|
|
puts "Could not recognize black player '#{black_lichess}'"
|
2013-11-23 12:01:00 +04:00
|
|
|
name = choose_player
|
2013-11-22 23:48:36 +04:00
|
|
|
fix_player 'black', name
|
|
|
|
black = @player_lichess[name]
|
|
|
|
end
|
|
|
|
|
2013-11-23 12:01:00 +04:00
|
|
|
subdir1 = date + '-' + white + '-vs-' + black
|
|
|
|
subdir2 = date + '-' + black + '-vs-' + white
|
|
|
|
# Change `1' -> `01' and so on
|
|
|
|
tour = "%02g" % ENV['tour']
|
|
|
|
|
|
|
|
[subdir1, subdir2].map do |subdir|
|
|
|
|
"#{@year}/#{@tournament}/tours/#{tour}/" + subdir
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Make the directory to move PGN file in
|
|
|
|
def mk_dir dir
|
|
|
|
puts "Directory '#{dir}' does not exist"
|
2014-02-05 04:31:05 +04:00
|
|
|
print "Create the directory? (Y/N)> "
|
2013-11-23 12:01:00 +04:00
|
|
|
answer = $stdin.gets.chomp
|
|
|
|
if ['Yes', 'yes', 'Y', 'y'].include? answer
|
|
|
|
FileUtils.mkdir_p dir
|
|
|
|
else
|
|
|
|
abort "PGN directory wasn't created"
|
|
|
|
end
|
2013-09-18 13:33:53 +04:00
|
|
|
end
|
|
|
|
|
2013-11-23 12:01:00 +04:00
|
|
|
# Choose and make the directory to move PGN file in
|
|
|
|
def choose_and_mk_dir dirs
|
|
|
|
puts "Choose a directory of PGN file from the list below:"
|
2014-02-05 04:31:05 +04:00
|
|
|
dirs.each_with_index { |dir, index| puts "%1.0f. %s" % [index+1, dir] }
|
2013-11-23 12:01:00 +04:00
|
|
|
|
2014-02-05 04:31:05 +04:00
|
|
|
print "Create a directory? (Number)> "
|
2013-11-23 12:01:00 +04:00
|
|
|
num = Integer $stdin.gets.chomp
|
|
|
|
FileUtils.mkdir_p dirs[num-1]
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns the file name to move PGN file to
|
2013-09-18 13:33:53 +04:00
|
|
|
def pgn_file dir
|
|
|
|
file = (Dir.entries(dir).length - 1).to_s + '.pgn'
|
|
|
|
if File.exists? (dir + '/' + file)
|
|
|
|
abort "Something wrong: PGN file already exists"
|
|
|
|
end
|
|
|
|
file
|
|
|
|
end
|
|
|
|
|
|
|
|
namespace :pgn do
|
|
|
|
desc "Parse a web page of lichess.org and save the PGN to temp file"
|
|
|
|
task :get do |t, args|
|
|
|
|
require 'nokogiri'
|
|
|
|
require 'open-uri'
|
|
|
|
|
|
|
|
doc = Nokogiri::HTML open ENV['url']
|
2014-02-05 04:34:47 +04:00
|
|
|
link = doc.css('.fen_pgn a').first
|
2013-09-18 13:33:53 +04:00
|
|
|
pgn_url = 'http://lichess.org' + link['href']
|
|
|
|
str = URI.parse(pgn_url).read
|
|
|
|
File.open('temp.pgn', 'w') { |f| f.write str }
|
|
|
|
fix_date # fix the game date
|
|
|
|
end
|
|
|
|
|
2014-02-05 16:10:34 +04:00
|
|
|
desc "Move PGN into the directory of destination"
|
2013-09-18 13:33:53 +04:00
|
|
|
task :mv do
|
|
|
|
require 'fileutils'
|
|
|
|
|
2013-11-23 12:01:00 +04:00
|
|
|
dirs = pgn_dirs
|
|
|
|
unless dirs.any? { |dir| Dir.exists? dir }
|
|
|
|
@ask_dir ? choose_and_mk_dir(dirs) : mk_dir(dirs.first)
|
2013-09-18 13:33:53 +04:00
|
|
|
else
|
2013-11-23 12:01:00 +04:00
|
|
|
puts "PGN directory exists"
|
|
|
|
end
|
|
|
|
|
|
|
|
dirs.each do |dir|
|
|
|
|
if Dir.exists? dir
|
|
|
|
dest = dir + '/' + pgn_file(dir)
|
|
|
|
puts "Moving PGN file to '#{dest}'"
|
|
|
|
FileUtils.mv('temp.pgn', dest)
|
|
|
|
break
|
2013-09-18 13:33:53 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
desc "Download and move PGN file at once"
|
|
|
|
task :create do
|
|
|
|
Rake::Task['pgn:get'].invoke
|
|
|
|
Rake::Task['pgn:mv'].invoke
|
|
|
|
end
|
|
|
|
end
|