Advertisement
Guest User

GarciaPL

a guest
May 9th, 2016
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.44 KB | None | 0 0
  1. import urllib2
  2. import json
  3. from sparkpost import SparkPost
  4.  
  5. sp = SparkPost('YOUR_API_KEY')
  6.  
  7. def currencyConverter(currency_from,currency_to):
  8.     yql_base_url = "https://query.yahooapis.com/v1/public/yql"
  9.     yql_query = 'select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20("'+currency_from+currency_to+'")'
  10.     yql_query_url = yql_base_url + "?q=" + yql_query + "&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys"
  11.     try:
  12.         yql_response = urllib2.urlopen(yql_query_url)
  13.         try:
  14.             yql_json = json.loads(yql_response.read())
  15.             currency_output = 1 * float(yql_json['query']['results']['rate']['Rate'])
  16.             return currency_output
  17.         except (ValueError, KeyError, TypeError):
  18.             return "JSON format error"
  19.  
  20.     except IOError, e:
  21.         if hasattr(e, 'code'):
  22.             return e.code
  23.         elif hasattr(e, 'reason'):
  24.             return e.reason
  25.  
  26. currency_from = "EUR" # currency codes : http://en.wikipedia.org/wiki/ISO_4217
  27. currency_to = "PLN"
  28. direction_rate = 4.44
  29.  
  30. rate = currencyConverter(currency_from,currency_to)
  31. print rate
  32.  
  33. if rate > direction_rate:
  34.     response = sp.transmissions.send(
  35.         recipients=['YOUR_EMAIL@gmail.com'],
  36.         html='<p>Rate of ' + currency_from + currency_to + ' equals ' + str(round(rate, 2)) + '</p>',
  37.         from_email='test@sparkpostbox.com',
  38.         subject='EuroMonitor'
  39.     )
  40.  
  41.     print(response)
  42. else:
  43.    print "Rate too low"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement