| 1 |
#!/usr/bin/python |
| 2 |
""" |
| 3 |
An example of scheduling time-based events in the background. |
| 4 |
|
| 5 |
Download the latest EUR/USD exchange rate from Yahoo every 30 seconds in the |
| 6 |
background; the rendered Flask web page can use the latest value without |
| 7 |
having to do the request itself. |
| 8 |
|
| 9 |
Note this is example is for demonstration purposes only, and is not actually |
| 10 |
used in the real world. You should not do this in a real application without |
| 11 |
reading Yahoo's terms-of-service and following them. |
| 12 |
""" |
| 13 |
|
| 14 |
from __future__ import print_function |
| 15 |
|
| 16 |
from flask import Flask |
| 17 |
|
| 18 |
from twisted.internet.task import LoopingCall |
| 19 |
from twisted.web.client import getPage |
| 20 |
from twisted.python import log |
| 21 |
|
| 22 |
from crochet import wait_for, run_in_reactor, setup |
| 23 |
setup() |
| 24 |
|
| 25 |
|
| 26 |
# Twisted code: |
| 27 |
class _ExchangeRate(object): |
| 28 |
"""Download an exchange rate from Yahoo Finance using Twisted.""" |
| 29 |
|
| 30 |
def __init__(self, name): |
| 31 |
self._value = None |
| 32 |
self._name = name |
| 33 |
|
| 34 |
# External API: |
| 35 |
def latest_value(self): |
| 36 |
"""Return the latest exchange rate value. |
| 37 |
|
| 38 |
May be None if no value is available. |
| 39 |
""" |
| 40 |
return self._value |
| 41 |
|
| 42 |
def start(self): |
| 43 |
"""Start the background process.""" |
| 44 |
self._lc = LoopingCall(self._download) |
| 45 |
# Run immediately, and then every 30 seconds: |
| 46 |
self._lc.start(30, now=True) |
| 47 |
|
| 48 |
def _download(self): |
| 49 |
"""Download the page.""" |
| 50 |
print("Downloading!") |
| 51 |
def parse(result): |
| 52 |
print("Got %r back from Yahoo." % (result,)) |
| 53 |
values = result.strip().split(",") |
| 54 |
self._value = float(values[1]) |
| 55 |
d = getPage( |
| 56 |
"http://download.finance.yahoo.com/d/quotes.csv?e=.csv&f=c4l1&s=%s=X" |
| 57 |
% (self._name,)) |
| 58 |
d.addCallback(parse) |
| 59 |
d.addErrback(log.err) |
| 60 |
return d |
| 61 |
|
| 62 |
|
| 63 |
# Blocking wrapper: |
| 64 |
class ExchangeRate(object): |
| 65 |
"""Blocking API for downloading exchange rate.""" |
| 66 |
|
| 67 |
def __init__(self, name): |
| 68 |
self._exchange = _ExchangeRate(name) |
| 69 |
|
| 70 |
@run_in_reactor |
| 71 |
def start(self): |
| 72 |
self._exchange.start() |
| 73 |
|
| 74 |
@wait_for(timeout=1) |
| 75 |
def latest_value(self): |
| 76 |
"""Return the latest exchange rate value. |
| 77 |
|
| 78 |
May be None if no value is available. |
| 79 |
""" |
| 80 |
return self._exchange.latest_value() |
| 81 |
|
| 82 |
|
| 83 |
EURUSD = ExchangeRate("EURUSD") |
| 84 |
app = Flask(__name__) |
| 85 |
|
| 86 |
@app.route('/') |
| 87 |
def index(): |
| 88 |
rate = EURUSD.latest_value() |
| 89 |
if rate is None: |
| 90 |
rate = "unavailable, please refresh the page" |
| 91 |
return "Current EUR/USD exchange rate is %s." % (rate,) |
| 92 |
|
| 93 |
|
| 94 |
if __name__ == '__main__': |
| 95 |
import sys, logging |
| 96 |
logging.basicConfig(stream=sys.stderr, level=logging.DEBUG) |
| 97 |
EURUSD.start() |
| 98 |
app.run() |