From 84f00ae3d63f28ea58a93d49337e9e642f3f6cb7 Mon Sep 17 00:00:00 2001 From: Kevin DuBois Date: Wed, 3 Sep 2008 14:38:52 -0400 Subject: [PATCH] Check for invalid requests --- yahoo.py | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/yahoo.py b/yahoo.py index b7b34b4..5aefd96 100755 --- a/yahoo.py +++ b/yahoo.py @@ -1,6 +1,7 @@ #!/usr/bin/python import urllib +import re # #These constants are specific to the Yahoo Finance @@ -31,8 +32,8 @@ EPS_NEXT_Q = 'e9' FLOAT_SHARES = 'f6' DAY_LOW = 'g' DAY_HIGH = 'h' -52W_LOW = 'j' -52W_HIGH = 'k' +LOW_52W = 'j' +HIGH_52W = 'k' HOLDINGS_GAIN_PERCENT = 'g1' ANNUALIZED_GAIN = 'g3' HOLDINGS_GAIN = 'g4' @@ -91,8 +92,26 @@ DAY_VALUE_CHANGE_RT = 'w4' STOCK_EXCHANGE = 'x' +valid_requests = ['n' , 'a', 'a2', 'a5', 'b', 'b2', 'b3', 'b4', 'b6', 'c', 'c1', 'c3', 'c6', 'c8', 'd', 'd1', 'd2', 'e', 'e7', 'e9', 'f6', 'g', 'h', 'j', 'k', 'g1', 'g3', 'g4', 'g5', 'g6', 'i5', 'j1', 'j3', 'j4', 'j5', 'j6', 'k1', 'k2', 'k3', 'k4', 'k5', 'l', 'l1', 'l2', 'l3', 'm', 'm2', 'm3', 'm4', 'm5', 'm6', 'm7', 'm8', 'n4', 'o', 'p', 'p1', 'p2', 'p5', 'p6', 'q', 'r', 'r1', 'r2', 'r5', 'r6', 'r7', 's', 's1', 's7', 't1', 't6', 't7', 't8', 'v', 'v1', 'v7', 'w', 'w1', 'w4', 'x'] + +def check_request(field): + p = re.compile('[a-z][0-9]|[a-z]') + list = p.findall(field) + for user_entry in list: + valid = 0 + for word in valid_requests: + if word == user_entry: + valid = 1 + if valid == 0: + print "Error, malfomed request: " + user_entry + print "Please ensure that the symbols you are requesting are part of the Yahoo API" + print "fatal: Exiting program" + quit() def yahoo_request(sym, field): + + check_request(field) + sym = sym.upper() prefix = "http://finance.yahoo.com/d/quotes.csv?s=" @@ -129,7 +148,9 @@ def yahoo_hist_request(sym, start, end): print data -yahoo_hist_request("XOM", "2007-1-1", "2008-1-1") -#print yahoo_request("XOm", NAME + AVERAGE_DAILY_VOL) + +#yahoo_hist_request("XOM", "2007-1-1", "2008-1-1") + +print yahoo_request("XOm", NAME + AVERAGE_DAILY_VOL) -- 2.11.4.GIT