From 29d362a8909289052744032518d259a661df44e0 Mon Sep 17 00:00:00 2001 From: Jonathan Harris Date: Wed, 3 Jun 2015 19:24:13 +0100 Subject: [PATCH] Add support for saving in CSV format. --- EDMarketConnector.py | 5 ++++- README.md | 2 +- bpc.py | 17 +++++++++++------ config.py | 1 + prefs.py | 7 ++++--- 5 files changed, 21 insertions(+), 11 deletions(-) diff --git a/EDMarketConnector.py b/EDMarketConnector.py index 53ad63ff..4e18ecbb 100755 --- a/EDMarketConnector.py +++ b/EDMarketConnector.py @@ -147,11 +147,14 @@ class AppWindow: elif not data.get('lastStarport') or not data['lastStarport'].get('commodities'): raise Exception("Station doesn't have a market!") + if config.read('output') & config.OUT_CSV: + bpc.export(data, True) + if config.read('output') & config.OUT_TD: td.export(data) if config.read('output') & config.OUT_BPC: - bpc.export(data) + bpc.export(data, False) if config.read('output') & config.OUT_EDDN: eddn.export(data, self.setstatus) diff --git a/README.md b/README.md index 4c78daaa..5c0f7be6 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ Elite: Dangerous Market Connector This app downloads commodity market data from the game [Elite: Dangerous](https://www.elitedangerous.com/) and, at your choice, either: * transmits the data to the Elite Dangerous Data Network ("EDDN") from where you and others can use it via online trading tools such as [eddb](http://eddb.io/). -* saves the data to files on your disk which you can load into trading tools such as [Slopey's BPC Market Tool](https://forums.frontier.co.uk/showthread.php?t=76081) and [Trade Dangerous](https://bitbucket.org/kfsone/tradedangerous/wiki/Home). +* saves the data to files on your disk which you can load into trading tools such as [Slopey's BPC Market Tool](https://forums.frontier.co.uk/showthread.php?t=76081), [Trade Dangerous](https://bitbucket.org/kfsone/tradedangerous/wiki/Home) and [Thrudd's Trading Tools](http://www.elitetradingtool.co.uk/). The user-interface is deliberately minimal - just press the "Update" button when you land at a station to automatically download and transmit or save the station's commodity market data: diff --git a/bpc.py b/bpc.py index b0e1fdc9..bf240f1a 100644 --- a/bpc.py +++ b/bpc.py @@ -8,21 +8,26 @@ import time from config import config from companion import commoditymap, bracketmap -def export(data): +def export(data, csv=False): querytime = config.read('querytime') or int(time.time()) - filename = join(config.read('outdir'), '%s.%s.%s.bpc' % (data['lastSystem']['name'].strip(), data['lastStarport']['name'].strip(), time.strftime('%Y-%m-%dT%H.%M.%S', time.localtime(querytime)))) + filename = join(config.read('outdir'), '%s.%s.%s.%s' % (data['lastSystem']['name'].strip(), data['lastStarport']['name'].strip(), time.strftime('%Y-%m-%dT%H.%M.%S', time.localtime(querytime)), csv and 'csv' or 'bpc')) timestamp = time.strftime('%Y-%m-%dT%H:%M:%S', time.gmtime(querytime)) - rowheader = '%s;%s;%s' % (data['commander']['name'].replace(';',':').strip(), data['lastSystem']['name'].strip(), data['lastStarport']['name'].strip()) + if csv: + header = 'System;Station;Commodity;Sell;Buy;Demand;;Supply;;Date;\n' + rowheader = '%s;%s' % (data['lastSystem']['name'].strip(), data['lastStarport']['name'].strip()) + else: # bpc + header = 'userID;System;Station;Commodity;Sell;Buy;Demand;;Supply;;Date;\n' + rowheader = '%s;%s;%s' % (data['commander']['name'].replace(';',':').strip(), data['lastSystem']['name'].strip(), data['lastStarport']['name'].strip()) - h = codecs.open(filename, 'w', 'utf-8') - h.write('userID;System;Station;Commodity;Sell;Buy;Demand;;Supply;;Date;\r\n') + h = open(filename, 'wt') # codecs can't automatically handle line endings, so encode manually where required + h.write(header.encode('utf-8')) for commodity in data['lastStarport']['commodities']: if commodity.get('categoryname') and commodity['categoryname'] != 'NonMarketable': - h.write('%s;%s;%s;%s;%s;%s;%s;%s;%s;\r\n' % ( + h.write('%s;%s;%s;%s;%s;%s;%s;%s;%s;\n' % ( rowheader, commoditymap.get(commodity['name'].strip(), commodity['name'].strip()), commodity.get('sellPrice') and int(commodity['sellPrice']) or '', diff --git a/config.py b/config.py index ccf7bf88..3cc8fb6e 100644 --- a/config.py +++ b/config.py @@ -18,6 +18,7 @@ class Config: OUT_EDDN = 1 OUT_BPC = 2 OUT_TD = 4 + OUT_CSV = 8 if platform=='darwin': diff --git a/prefs.py b/prefs.py index 832c706a..2df2d1d9 100644 --- a/prefs.py +++ b/prefs.py @@ -66,12 +66,13 @@ class PreferencesDialog(tk.Toplevel): ttk.Radiobutton(outframe, text="Online to the Elite Dangerous Data Network (EDDN)", variable=self.outvar, value=config.OUT_EDDN, command=self.outvarchanged).grid(row=1, columnspan=2, padx=5, sticky=tk.W) ttk.Radiobutton(outframe, text="Offline in Slopey's BPC format", variable=self.outvar, value=config.OUT_BPC, command=self.outvarchanged).grid(row=2, columnspan=2, padx=5, sticky=tk.W) ttk.Radiobutton(outframe, text="Offline in Trade Dangerous format", variable=self.outvar, value=config.OUT_TD, command=self.outvarchanged).grid(row=3, columnspan=2, padx=5, sticky=tk.W) - ttk.Label(outframe, text=(platform=='darwin' and 'Where:' or 'File location:')).grid(row=4, padx=5, pady=(5,0), sticky=tk.NSEW) + ttk.Radiobutton(outframe, text="Offline in CSV format", variable=self.outvar, value=config.OUT_CSV, command=self.outvarchanged).grid(row=4, columnspan=2, padx=5, sticky=tk.W) + ttk.Label(outframe, text=(platform=='darwin' and 'Where:' or 'File location:')).grid(row=5, padx=5, pady=(5,0), sticky=tk.NSEW) self.outbutton = ttk.Button(outframe, text=(platform=='darwin' and 'Browse...' or 'Choose...'), command=self.outbrowse) - self.outbutton.grid(row=4, column=1, padx=5, pady=(5,0), sticky=tk.NSEW) + self.outbutton.grid(row=5, column=1, padx=5, pady=(5,0), sticky=tk.NSEW) self.outdir = ttk.Entry(outframe) self.outdir.insert(0, config.read('outdir')) - self.outdir.grid(row=5, columnspan=2, padx=5, pady=5, sticky=tk.EW) + self.outdir.grid(row=6, columnspan=2, padx=5, pady=5, sticky=tk.EW) self.outvarchanged() if platform=='darwin':