Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion googlefinance/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,31 @@
u'pcls_fix': u'PreviousClosePrice'
}

DEFAULT_TIMEOUT = None

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please set a number here for a default value

@vadimg vadimg Apr 27, 2016

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure you want that? It will make this change non-backwards compatible.

Meaning that some clients (who are on bad networks perhaps?) may start timing out.

So, should I set this default value to something ridiculous, like 30 seconds?

_timeout = DEFAULT_TIMEOUT

def setTimeout(timeoutSeconds):
'''
Sets the timeout for google finance API calls (in seconds).
'''
global _timeout
_timeout = timeoutSeconds

def buildUrl(symbols):
symbol_list = ','.join([symbol for symbol in symbols])
# a deprecated but still active & correct api
return 'http://finance.google.com/finance/info?client=ig&q=' \
+ symbol_list

def request(symbols):
urlopenFlags = {}
if _timeout is not None:
urlopenFlags['timeout'] = _timeout

url = buildUrl(symbols)
req = Request(url)
resp = urlopen(req)
resp = urlopen(req, **urlopenFlags)

# remove special symbols such as the pound symbol
content = resp.read().decode('ascii', 'ignore').strip()
content = content[3:]
Expand Down
12 changes: 12 additions & 0 deletions tests/test_googlefinance.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import googlefinance
import unittest
from urllib2 import URLError

class TestQuotes(unittest.TestCase):

Expand All @@ -15,3 +16,14 @@ def test_symbols(self):
self.assertEqual(quotes[1]["Index"], "VIE")
self.assertEqual(quotes[1]["StockSymbol"], "BKS")

def test_timeout(self):
# ensure timeout will always happen
googlefinance.setTimeout(0.0001)
with self.assertRaisesRegexp(URLError, 'timed out'):
googlefinance.getQuotes(['GOOG'])

# reset timeout (for other tests)
googlefinance.setTimeout(googlefinance.DEFAULT_TIMEOUT)

if __name__ == '__main__':
unittest.main()