Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import urllib2
- import json
- from sparkpost import SparkPost
- sp = SparkPost('YOUR_API_KEY')
- def currencyConverter(currency_from,currency_to):
- yql_base_url = "https://query.yahooapis.com/v1/public/yql"
- yql_query = 'select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20("'+currency_from+currency_to+'")'
- yql_query_url = yql_base_url + "?q=" + yql_query + "&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys"
- try:
- yql_response = urllib2.urlopen(yql_query_url)
- try:
- yql_json = json.loads(yql_response.read())
- currency_output = 1 * float(yql_json['query']['results']['rate']['Rate'])
- return currency_output
- except (ValueError, KeyError, TypeError):
- return "JSON format error"
- except IOError, e:
- if hasattr(e, 'code'):
- return e.code
- elif hasattr(e, 'reason'):
- return e.reason
- currency_from = "EUR" # currency codes : http://en.wikipedia.org/wiki/ISO_4217
- currency_to = "PLN"
- direction_rate = 4.44
- rate = currencyConverter(currency_from,currency_to)
- print rate
- if rate > direction_rate:
- response = sp.transmissions.send(
- recipients=['YOUR_EMAIL@gmail.com'],
- html='<p>Rate of ' + currency_from + currency_to + ' equals ' + str(round(rate, 2)) + '</p>',
- from_email='test@sparkpostbox.com',
- subject='EuroMonitor'
- )
- print(response)
- else:
- print "Rate too low"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement