Skip to content

Commit

Permalink
Allow import to retrieve files from the web.
Browse files Browse the repository at this point in the history
Also added a --maddavo option to import which fetches daves' prices.
  • Loading branch information
kfsone committed Nov 29, 2014
1 parent 88b8a61 commit 8e1bd3e
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 4 deletions.
11 changes: 11 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@
TradeDangerous, Copyright (C) Oliver "kfsone" Smith, July 2014
==============================================================================

v6.1.2 Nov 28 2014
. (kfsone) Adding support for stations with the same name in diff systems
. (kfsone) Removed import-from-maddavo.py
. (kfsone) Added support for URLs in "import" sub-command
. (kfsone) Added "--maddavo" option to "import" to import maddavo's data
e.g.
trade.py import -i http://kfs.org/td/prices
or
trade.py import -i --maddavo


v6.1.1 Nov 27 2014
Optimization:
. (kfsone) Removed support for old pre v5 .prices files
Expand Down
30 changes: 30 additions & 0 deletions README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,36 @@ UPDATE sub-command:
aka:
trade.py update --sub -ST0 aulin

IMPORT sub-command:

Provides a way to import prices data from a file or a web site. You can use this
to import data for a few stations or an entire .prices file from a friend.

For instance, if you 'update'd a station and there was an error importing it,
the data is usually saved as "prices.last". You can open this file and correct
the error and then import it, rather than having to re-enter all of the data.

trade.py import [-q | -v] [filename | url | --maddavo] [--ignore-unknown]

filename
Specifies the name of the file to load
e.g.
import.prices

url
Specifies web adress to retrieve the data from
e.g.
http://kfs.org/td/prices

--maddavo
Like 'url' but specifies the URL for maddavo's .prices file

--ignore-unknown
-i
Any systems, stations, categories or items that aren't recognized
by this version of TD will be reported but import will continue.



BUY sub-command:

Expand Down
50 changes: 46 additions & 4 deletions commands/import_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
from commands.parsing import MutuallyExclusiveGroup, ParseArgument
from commands.exceptions import *
import math
import re
from urllib.parse import urlencode
from urllib.request import Request, urlopen

import cache
from pathlib import Path
Expand All @@ -20,10 +23,20 @@
arguments = [
]
switches = [
ParseArgument('filename',
help='Name of the file to read.',
type=str,
default=None,
MutuallyExclusiveGroup(
ParseArgument('filename',
help='Name of the file to read.',
type=str,
default=None,
),
MutuallyExclusiveGroup(
ParseArgument('--maddavo',
help='Import prices from Maddavo\'s site.',
dest='url',
action='store_const',
const="http://www.davek.com.au/td/prices.asp",
),
),
),
ParseArgument(
'--ignore-unknown', '-i',
Expand All @@ -36,12 +49,38 @@
),
]

######################################################################
# Helpers

def download(cmdenv, url):
"""
Download a prices file from the web.
"""

req = Request(url)

cmdenv.DEBUG0("Fetching: {}", url)
f = urlopen(req)
cmdenv.DEBUG0(str(f.info()))
data = f.read().decode()

dstFile = "import.prices"
with open(dstFile, "w") as fh:
print(data, file=fh)

return dstFile


######################################################################
# Perform query and populate result set

def run(results, cmdenv, tdb):
# If the filename specified was "-" or None, then go ahead
# and present the user with an open file dialog.

if cmdenv.url:
cmdenv.filename = download(cmdenv, cmdenv.url)

if not cmdenv.filename:
import tkinter
from tkinter.filedialog import askopenfilename
Expand All @@ -61,6 +100,9 @@ def run(results, cmdenv, tdb):
raise SystemExit("Aborted")
cmdenv.filename = filename

if re.match("^https?://", cmdenv.filename, re.IGNORECASE):
cmdenv.filename = download(cmdenv.filename)

# check the file exists.
filePath = Path(cmdenv.filename)
if not filePath.is_file():
Expand Down

0 comments on commit 8e1bd3e

Please sign in to comment.