Last active
August 29, 2015 14:15
-
-
Save mschuett/f416301a24a10cdbd1a4 to your computer and use it in GitHub Desktop.
convert price data from http://www.davek.com.au/td/ to RegulatedNoise CSV
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
#!/bin/env python | |
# | |
# Simple proof of concept how to convert TradeDangerous price data | |
# to RegulatedNoise CSV data. | |
# | |
# For use on the command line using stdin/stdout, e.g.: | |
# python conv_prices.py _Gen123.prices > _Gen123.csv | |
# python < conv_prices.py _Gen123.prices > _Gen123.csv | |
# | |
# Written by Martin Schuette <info@mschuette.name>, 2015 | |
# Published under the terms of the MIT License (http://opensource.org/licenses/MIT) | |
import fileinput | |
import re | |
# keep state across lines | |
station = {} | |
# parse one line of data for an item price | |
item_re = re.compile(r"""^ | |
(?P<item>.{24})\s* | |
(?P<sell>\d+)\s* | |
(?P<buy>\d+)\s* | |
(?P<demand>\d*) # no space | |
(?P<demandlvl>.)\s* | |
(?P<stock>\d*) # no space | |
(?P<stocklvl>.)\s* | |
(?P<date>\d\d\d\d-\d\d-\d\d)\s | |
(?P<time>\d\d:\d\d:\d\d)\s* | |
(?P<comment>.*)$ | |
""", re.X) | |
def process(line): | |
if len(line) == 0 or line.startswith('#'): | |
# ignore empty lines and comments | |
return | |
elif line.startswith('@'): | |
# read the system and station name, to be used for all following items | |
(system_name, station_name) = line.split('/') | |
station['sysname'] = system_name[2:] | |
station['name'] = station_name | |
elif line.startswith(' + '): | |
# ignore the item category lines | |
return | |
else: | |
# item data line, parse with regex | |
# note: the demand levels high/med/low are ignored, because they are not present in my .prices samples | |
match = item_re.match(line) | |
if match: | |
m = match.groupdict() | |
print("{};{};{};{};{};{};{};{};{};{};{}".format( | |
station['sysname'], station['name'], | |
m['item'].strip(), m['sell'], m['buy'], | |
m['demand'], '', | |
m['stock'], '', | |
m['date']+'T'+m['time'], | |
"imported with conv_prices, comment: " + | |
m['comment'].strip(" #"))) | |
print('System;Station;Commodity;Sell;Buy;Demand;;Supply;;Date;') | |
for line in fileinput.input(): | |
process(line.strip()) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
cheers ,have past it on