mcdallas / wallstreet

Real time stock and option data.
MIT License
1.24k stars 197 forks source link

Mutual funds not reporting properly #15

Open bryanlan opened 6 years ago

bryanlan commented 6 years ago

Google's mechanism to report mutual funds is a bit different, and the way it returns data sometimes has a ',}' that causes the json parser to hit an exception. Also, the keys are different from non-mutual funds. I used the hackery below to get past it, but you may want to do something more elegant:

try: jayson = r.text.replace('\n', '')

mutual funds have a ,} that messes up the json parser

        jayson = jayson.replace(',}','}')
        jayson = json.loads(jayson[2:])[0]
        self.ticker = jayson['t']
    except:
        self.ticker = None

    if r.status_code == 400 or self.ticker != query.split(':')[-1]:
        raise LookupError('Ticker symbol not found. Try adding the exchange parameter')
    else:
        r.raise_for_status()

    self.id = jayson["id"]
    self.exchange = jayson['e']
    self._last_trade = None
    self.name = jayson['name']
    if self.exchange != 'MUTF':
        self._price = parse(jayson['l'])
        try:
            self.change = parse(jayson['c'])
            self.cp = parse(jayson['cp'])
        except ValueError:
            self.change = jayson['c']
            self.cp = jayson['cp']

        self.dy = parse(jayson.get('dy') or 0)/100
    else:
        self._price = parse(jayson['nav_prior'])
        try:
            self.change = parse(jayson['nav_c'])
            self.cp = parse(jayson['nav_cp'])
        except ValueError:
            self.change = jayson['nav_c']
            self.cp = jayson['nav_cp']
        self.dy = parse(jayson.get('yield_percent') or 0) / 100