Created
October 23, 2013 14:43
-
-
Save steventlamb/7120127 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
#!/usr/bin/env python | |
import pgn | |
import sys | |
import csv | |
import datetime | |
def csv_filename_builder(filename_prefix): | |
filename_param = "filename = %s_%s.csv" % (filename_prefix, | |
formatted_datestring) | |
response = HttpResponse(content_type='text/csv') | |
response['Content-Disposition'] = 'attachment; %s;' % filename_param | |
return response | |
def main(): | |
if len(sys.argv) == 1: | |
print("please provide filename") | |
sys.exit(1) | |
pgn_file = sys.argv[1] | |
formatted_datestring = datetime.date.today().strftime("%Y%m%d") | |
location_csv_file = open('%s_LOCATIONS_%s.csv' % | |
(pgn_file[:-4], formatted_datestring), 'w') | |
game_csv_file = open('%s_GAMES_%s.csv' % | |
(pgn_file[:-4], formatted_datestring), 'w') | |
location_writer = csv.writer(location_csv_file) | |
game_writer = csv.writer(game_csv_file) | |
text = open(pgn_file).read() | |
games = pgn.loads(text) | |
location_writer.writerow(['loc_id', 'province', 'country']) | |
game_writer.writerow(['id', | |
'loc_id', | |
'game_date', | |
'tourney_date', | |
'tourney_country', | |
'tourney_site', | |
'tourney_round', | |
'white_player', | |
'white_country', | |
'black_player', | |
'black_country', | |
'result', | |
'eco', | |
]) | |
counter = 0 | |
for game in games: | |
counter += 1 | |
id = counter | |
try: | |
loc_id = game.site + game.eventcountry | |
except: | |
loc_id = "" | |
try: | |
game_date = game.date | |
except: | |
game_date = "" | |
try: | |
tourney_date = game.eventdate | |
except: | |
tourney_date = "" | |
try: | |
tourney_country = game.eventcountry | |
except: | |
tourney_country = "" | |
try: | |
tourney_site = game.site | |
except: | |
tourney_siten = "" | |
try: | |
tourney_round = game.round | |
except: | |
tourney_round = "" | |
try: | |
white_player = game.white | |
except: | |
white_player = "" | |
try: | |
white_country = game.whiteteamcountry | |
except: | |
white_country = "" | |
try: | |
black_player = game.black | |
except: | |
black_player = "" | |
try: | |
black_country = game.blackteamcountry | |
except: | |
black_country = "" | |
try: | |
result = game.result | |
except: | |
result = "" | |
try: | |
eco = game.eco | |
except: | |
eco = "" | |
game_writer.writerow([ | |
id, | |
loc_id, | |
game_date, | |
tourney_date, | |
tourney_country, | |
tourney_site, | |
tourney_round, | |
white_player, | |
white_country, | |
black_player, | |
black_country, | |
result, | |
eco, | |
]) | |
location_writer.writerow([loc_id, | |
tourney_site, | |
tourney_country]) | |
location_csv_file.close() | |
game_csv_file.close() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment