Ejemplo n.º 1
0
def ParseStockData(jwt):
    if (not jsonWebToken.isValid(str(jwt))):
        return "JWT invalid"
    symbol = jsonWebToken.getJsonData(jsonWebToken.decode(jwt))['symbol']
    user = jsonWebToken.getJsonData(jsonWebToken.decode(jwt))['user']
    daysToGet = jsonWebToken.getJsonData(jsonWebToken.decode(jwt))['daysToGet']
    StockObjs = []
    configSymbol = symbol.upper().strip(' \t\n\r')
    baseUrl = "https://query.yahooapis.com/v1/public/yql?q="
    suffixUrl = "&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback="
    print "Fetching Data, Please wait..."
    try:
        for x in stepper(2010, 2020, 1):
            try:
                query = "select Date,Close from yahoo.finance.historicaldata where symbol = \"" + symbol + "\""
                query += " and startDate=\""+str(x)+"-01-01\" and endDate=\""+str(x)+"-12-31\"|sort(field='Date', descending=\"false\")&format=json&diagnostics=true"
                url = baseUrl + query + suffixUrl
                response = urllib.urlopen(url)
                data = json.loads(response.read())
                for quote in data["query"]["results"]["quote"]:
                    StockObjs.append(StockObjectClass(float(quote["Close"]), str(quote["Date"])[5:]))
            except Exception as e:
                logHelper.LogError("An error was thrown during the API fetch.", "API")
    except Exception as e:
        logHelper.LogError("Error getting stock data", "API")
    dbCon.deleteData(user)
    stockCloseNums = GetPrices(StockObjs)
    avg = average(stockCloseNums)
    variance = map(lambda x: (x - avg)**2, stockCloseNums)
    sd = math.sqrt(average(variance))
    ln = len(stockCloseNums)
    high = stockCloseNums[(ln-1)]+sd
    low = stockCloseNums[(ln-1)]-sd
    monthJumper = 1
    dayCounter = 0
    for x in stepper(0, int(daysToGet), 1):
        output_format = '%d-%m-%Y'
        output = get_date(output_format, x)
        dateObj = datetime.strptime(output, output_format)
        #print str(dateObj.day)
        #print str(dateObj.month)
        prices = getPricesForDate(str(dateObj.day), str(dateObj.month), StockObjs)
        if(len(prices)>0):
            price = average(prices)
            dbCon.InsertData(str(output), user, str(price))
    return "Operation complete, now sit back and wait for the money to roll in!!"
 def test_isValid(self):#IS THE JWT VALID
     encoded = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmYWtlIjoicGF5bG9hZCJ9.aIMnxkP099xPuOMQhCnMUttdZN1bjENLFMlTevuVgh4'
     broken = "1"
     payload = {'fake':'payload'}
     self.assertTrue(jsonWebToken.isValid(encoded))
     self.assertFalse(jsonWebToken.isValid(broken))