Last active
February 27, 2016 15:22
-
-
Save mabako/173c433325a14bb2b66f to your computer and use it in GitHub Desktop.
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 'httparty' | |
require 'net/http' | |
player_id = 115095 | |
folder_name = 'sgf-files' | |
Dir.mkdir(folder_name) unless Dir.exist?(folder_name) | |
Dir.chdir(folder_name) | |
class Player | |
include HTTParty | |
base_uri 'https://online-go.com/api/v1/players' | |
def initialize player_id | |
@player_id = player_id | |
end | |
def games page | |
self.class.get("/#{@player_id}/games", query: {page: page, page_size: 200}) | |
end | |
def game_ids page | |
response = games(page) | |
# relative links to all pages | |
links = response['results'].map { |o| o['id'] } | |
# next page number | |
next_page = response['next'].nil? ? nil : (page+1) | |
return links, next_page | |
end | |
end | |
def save_game game_id, http | |
file_name = "#{game_id}.sgf" | |
if not File.exist? file_name | |
File.open(file_name, 'wb') do |file| | |
http.get("/api/v1/games/#{game_id}/sgf") do |str| | |
file.write str | |
end | |
end | |
# somewhat of a progress indicator | |
print '.' | |
else | |
print '_' | |
end | |
end | |
page = 1 | |
player = Player.new(player_id) | |
Net::HTTP.start('online-go.com', 443, use_ssl: true) do |http| | |
begin | |
games, page = player.game_ids page | |
# download SGF files | |
games.each do |game_id| | |
save_game game_id, http | |
end | |
end while page != nil | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment