HEX
Server: Microsoft-IIS/10.0
System: Windows NT ITPWINWEBSVR22 10.0 build 20348 (Windows Server 2022) AMD64
User: www.conferencesearch.co.uk (0)
PHP: 8.3.30
Disabled: NONE
Upload Files
File: D:/bin/scripts/python/prev/3/get_currency_exchange_rates - Copy.py
# (SS,07/04/14) Python script to lookup EUR and USD exchanges rates from Yahoo and save to HyperFlight currency tables
# (SS,28/04/14) Added comparison tables
# (SS,03/11/15) Modified to also save exchange rates to hypercomposites

import urllib2
import datetime
from BeautifulSoup import BeautifulSoup
import MySQLdb
import sys
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import email.utils

def fetch_page(AURL): 
  print_html(AURL)
  
  req = urllib2.Request(AURL, '',  {'User-Agent' : 'Mozilla/5.0 (Windows NT 6.3; WOW64)'})
  response = urllib2.urlopen(req)
  the_page = response.read()
  return the_page      
      
def get_currency_yahoo(AFromCurrency, AToCurrency):
    LToken = AFromCurrency.lower() + AToCurrency.lower() # e.g. gbpusd
    LURL = 'http://uk.finance.yahoo.com/q?s=' + LToken + '=x'
    html = fetch_page(LURL)
    
    soup = BeautifulSoup(html)
    
    # find the exchange rate which is in a span e.g. <span id="yfs_l10_gbpusd=x">1.6592</span>
    spans = soup.findAll('span', {"id" : "yfs_l10_" + LToken + "=x"})
    LExchangeRate = 0
    if len(spans) > 0:
        try:
            LExchangeRate = float(spans[0].text.strip())
        except ValueError, e:
            print_html('ValueError: %s' % (e))
    else:
        print_html('Exchange rate not found')
        
    return LExchangeRate
    
def get_currency(AFromCurrency, AToCurrency):
    print_html("")
    LExchangeRate = get_currency_yahoo(AFromCurrency, AToCurrency); 
    print_html('Exchange rate %s to %s: %s' % (AFromCurrency, AToCurrency, LExchangeRate))
    
    # save in history table
    LSQL = 'INSERT INTO currency_history SET \
        ExchangeRateDateTime = NOW(), Currency = "%s", \
        ExchangeRate = %0.4f' % (AToCurrency, LExchangeRate)
    cur.execute(LSQL)
    
    # update currencies table if <> 0
    if LExchangeRate != 0:
        LSQL = 'UPDATE currencies SET \
            ExchangeRate = %0.4f \
            WHERE Currency = "%s"' % (LExchangeRate, AToCurrency)
        cur.execute(LSQL)
        # (SS,3/11/15) do the same in hypercomposites
        cur_hypercomposites.execute(LSQL)
    else:
        set_alert(True) # signal alert if exchange is 0

def set_alert(AAlert):
    global FAlert # required to modify a global 
    FAlert = AAlert
      
def get_alert():
    return FAlert

# (SS,26/4/14)    
def get_comparisons():
    LSQL = """\
CREATE TEMPORARY TABLE tmp_exchange_rates

SELECT Currency, "0D" AS PastPeriod, ExchangeRate FROM currency_history
WHERE ExchangeRate <> 0 AND DATE(ExchangeRateDateTime) = DATE_SUB(CURDATE(), INTERVAL 0 DAY)

UNION ALL

SELECT Currency, "1D" AS PastPeriod, ExchangeRate FROM currency_history
WHERE ExchangeRate <> 0 AND DATE(ExchangeRateDateTime) = DATE_SUB(CURDATE(), INTERVAL 1 DAY)

UNION ALL

SELECT Currency, "1W" AS PastPeriod, ExchangeRate FROM currency_history
WHERE ExchangeRate <> 0 AND DATE(ExchangeRateDateTime) = DATE_SUB(CURDATE(), INTERVAL 1 WEEK)

UNION ALL

SELECT Currency, "2W" AS PastPeriod, ExchangeRate FROM currency_history
WHERE ExchangeRate <> 0 AND DATE(ExchangeRateDateTime) = DATE_SUB(CURDATE(), INTERVAL 2 WEEK)

UNION ALL

SELECT Currency, "3W" AS PastPeriod, ExchangeRate FROM currency_history
WHERE ExchangeRate <> 0 AND DATE(ExchangeRateDateTime) = DATE_SUB(CURDATE(), INTERVAL 3 WEEK)

UNION ALL

SELECT Currency, "1M" AS PastPeriod, ExchangeRate FROM currency_history
WHERE ExchangeRate <> 0 AND DATE(ExchangeRateDateTime) = DATE_SUB(CURDATE(), INTERVAL 1 MONTH)

UNION ALL

SELECT Currency, "2M" AS PastPeriod, ExchangeRate FROM currency_history
WHERE ExchangeRate <> 0 AND DATE(ExchangeRateDateTime) = DATE_SUB(CURDATE(), INTERVAL 2 MONTH)

UNION ALL

SELECT Currency, "3M" AS PastPeriod, ExchangeRate FROM currency_history
WHERE ExchangeRate <> 0 AND DATE(ExchangeRateDateTime) = DATE_SUB(CURDATE(), INTERVAL 3 MONTH)

UNION ALL

SELECT Currency, "6M" AS PastPeriod, ExchangeRate FROM currency_history
WHERE ExchangeRate <> 0 AND DATE(ExchangeRateDateTime) = DATE_SUB(CURDATE(), INTERVAL 6 MONTH)

UNION ALL

SELECT Currency, "1Y" AS PastPeriod, ExchangeRate FROM currency_history
WHERE ExchangeRate <> 0 AND DATE(ExchangeRateDateTime) = DATE_SUB(CURDATE(), INTERVAL 1 YEAR)

UNION ALL

SELECT Currency, "2Y" AS PastPeriod, ExchangeRate FROM currency_history
WHERE ExchangeRate <> 0 AND DATE(ExchangeRateDateTime) = DATE_SUB(CURDATE(), INTERVAL 2 YEAR)

UNION ALL

SELECT Currency, "3Y" AS PastPeriod, ExchangeRate FROM currency_history
WHERE ExchangeRate <> 0 AND DATE(ExchangeRateDateTime) = DATE_SUB(CURDATE(), INTERVAL 3 YEAR)

UNION ALL

SELECT Currency, "4Y" AS PastPeriod, ExchangeRate FROM currency_history
WHERE ExchangeRate <> 0 AND DATE(ExchangeRateDateTime) = DATE_SUB(CURDATE(), INTERVAL 4 YEAR)

UNION ALL

SELECT Currency, "5Y" AS PastPeriod, ExchangeRate FROM currency_history
WHERE ExchangeRate <> 0 AND DATE(ExchangeRateDateTime) = DATE_SUB(CURDATE(), INTERVAL 5 YEAR)

UNION ALL

SELECT Currency, "6Y" AS PastPeriod, ExchangeRate FROM currency_history
WHERE ExchangeRate <> 0 AND DATE(ExchangeRateDateTime) = DATE_SUB(CURDATE(), INTERVAL 6 YEAR)

UNION ALL

SELECT Currency, "7Y" AS PastPeriod, ExchangeRate FROM currency_history
WHERE ExchangeRate <> 0 AND DATE(ExchangeRateDateTime) = DATE_SUB(CURDATE(), INTERVAL 7 YEAR)

UNION ALL

SELECT Currency, "8Y" AS PastPeriod, ExchangeRate FROM currency_history
WHERE ExchangeRate <> 0 AND DATE(ExchangeRateDateTime) = DATE_SUB(CURDATE(), INTERVAL 8 YEAR)

UNION ALL

SELECT Currency, "9Y" AS PastPeriod, ExchangeRate FROM currency_history
WHERE ExchangeRate <> 0 AND DATE(ExchangeRateDateTime) = DATE_SUB(CURDATE(), INTERVAL 9 YEAR)

UNION ALL

SELECT Currency, "10Y" AS PastPeriod, ExchangeRate FROM currency_history
WHERE ExchangeRate <> 0 AND DATE(ExchangeRateDateTime) = DATE_SUB(CURDATE(), INTERVAL 10 YEAR)

UNION ALL

SELECT Currency, "11Y" AS PastPeriod, ExchangeRate FROM currency_history
WHERE ExchangeRate <> 0 AND DATE(ExchangeRateDateTime) = DATE_SUB(CURDATE(), INTERVAL 11 YEAR)

UNION ALL

SELECT Currency, "12Y" AS PastPeriod, ExchangeRate FROM currency_history
WHERE ExchangeRate <> 0 AND DATE(ExchangeRateDateTime) = DATE_SUB(CURDATE(), INTERVAL 12 YEAR)

UNION ALL

SELECT Currency, "13Y" AS PastPeriod, ExchangeRate FROM currency_history
WHERE ExchangeRate <> 0 AND DATE(ExchangeRateDateTime) = DATE_SUB(CURDATE(), INTERVAL 13 YEAR)

UNION ALL

SELECT Currency, "14Y" AS PastPeriod, ExchangeRate FROM currency_history
WHERE ExchangeRate <> 0 AND DATE(ExchangeRateDateTime) = DATE_SUB(CURDATE(), INTERVAL 14 YEAR)

UNION ALL

SELECT Currency, "15Y" AS PastPeriod, ExchangeRate FROM currency_history
WHERE ExchangeRate <> 0 AND DATE(ExchangeRateDateTime) = DATE_SUB(CURDATE(), INTERVAL 15 YEAR)

"""

    cur.execute(LSQL)
    
    LSQL = """\
SELECT Currency, ExchangeRate,
AVG(IF(PastPeriod = "1D", ExchangeRate, NULL)) AS OneDay,
AVG(IF(PastPeriod = "1W", ExchangeRate, NULL)) AS OneWeek,
AVG(IF(PastPeriod = "2W", ExchangeRate, NULL)) AS TwoWeeks,
AVG(IF(PastPeriod = "3W", ExchangeRate, NULL)) AS ThreeWeeks,
AVG(IF(PastPeriod = "1M", ExchangeRate, NULL)) AS OneMonth,
AVG(IF(PastPeriod = "2M", ExchangeRate, NULL)) AS TwoMonths,
AVG(IF(PastPeriod = "3M", ExchangeRate, NULL)) AS ThreeMonths,
AVG(IF(PastPeriod = "6M", ExchangeRate, NULL)) AS SixMonths,
AVG(IF(PastPeriod = "1Y", ExchangeRate, NULL)) AS OneYear,
AVG(IF(PastPeriod = "2Y", ExchangeRate, NULL)) AS TwoYears,
AVG(IF(PastPeriod = "3Y", ExchangeRate, NULL)) AS ThreeYears,
AVG(IF(PastPeriod = "4Y", ExchangeRate, NULL)) AS FourYears,
AVG(IF(PastPeriod = "5Y", ExchangeRate, NULL)) AS FiveYears,
AVG(IF(PastPeriod = "6Y", ExchangeRate, NULL)) AS SixYears,
AVG(IF(PastPeriod = "7Y", ExchangeRate, NULL)) AS SevenYears,
AVG(IF(PastPeriod = "8Y", ExchangeRate, NULL)) AS EightYears,
AVG(IF(PastPeriod = "9Y", ExchangeRate, NULL)) AS NineYears,
AVG(IF(PastPeriod = "10Y", ExchangeRate, NULL)) AS TenYears,
AVG(IF(PastPeriod = "11Y", ExchangeRate, NULL)) AS ElevenYears,
AVG(IF(PastPeriod = "12Y", ExchangeRate, NULL)) AS TwelveYears,
AVG(IF(PastPeriod = "13Y", ExchangeRate, NULL)) AS ThirteenYears,
AVG(IF(PastPeriod = "14Y", ExchangeRate, NULL)) AS FourteenYears,
AVG(IF(PastPeriod = "15Y", ExchangeRate, NULL)) AS FifteenYears
FROM tmp_exchange_rates
WHERE Currency <> "GBP"
GROUP BY Currency
ORDER BY (Currency = "EUR" OR Currency = "USD") DESC, Currency
"""
     
    cur.execute(LSQL)
    rows = cur.fetchall()     
    
    for type in range(0, 3):    
      print '<table>'
      print '<tr>'
      print '<th>Code</th><th>Rate</th><th>1D</th><th>1W</th><th>2W</th><th>3W</th><th>1M</th><th>2M</th><th>3M</th><th>6M</th><th>1Y</th><th>2Y</th><th>3Y</th><th>4Y</th><th>5Y</th><th>6Y</th><th>7Y</th><th>8Y</th><th>9Y</th><th>10Y</th><th>11Y</th><th>12Y</th><th>13Y</th><th>14Y</th><th>15Y</th>'
      print '</tr>'
      for row in rows:
        #    get_currency('GBP', row[0]) # e.g. get_currency('GBP', 'EUR')
        #get_currency('GBP', 'EUR')
        print '<tr style="text-align:right">'
        for f in range(0, 25):
          if f == 0:
            print '<td style="text-align:left"><a href="http://uk.finance.yahoo.com/q?s=gbp%s=x">%s</a></td>' % (row[f].lower(), row[f])
          else:
            if row[f] == None:
              print '<td></td>'
            else:
              if f >= 2:
                if type == 0:
                  print '<td>%.4f</td>' % (row[f])
                elif type == 1:
                  print '<td>%+.4f</td>' % (row[1] - row[f])
                else: # i.e. 2
                  print '<td>%+.2f%%</td>' % ((row[1] - row[f]) / row[1] * 100)
              else:
                print '<td>%s</td>' % row[f]
        print '</tr>'
      print '</table>'
      print '<br>'


def print_html_header():
      print """<html>
<title>HyperFlight Order</title>
<style>
html, body, h1, h2, p {
  font-family: Arial, Helvetica, sans-serif;
}
img {
  border: 0;
}
table {
  border-collapse: collapse;
}
table, th, td {
  border: 1px solid black;
}
td, th {
  padding: 5px;
  white-space: nowrap;
}
</style>
<body>
"""

def print_html_footer():
    print """</body>
</html>"""

def print_html(AText):
    print AText + "<br>"
      
def send_email_file(AFileName, ASubject, AFrom, ATo, ABcc):
    # Open a plain text file for reading.  For this example, assume that
    # the text file contains only ASCII characters.
    fp = open(AFileName, 'rb')
    # Create a text/plain message
    #msg = MIMEText(fp.read())
    html = fp.read()
    fp.close()
    
    msg = MIMEMultipart('alternative')

    # header    
    if get_alert():
        msg['Subject'] = 'ALERT!!! ' + ASubject
    else:
        msg['Subject'] = ASubject
    msg['From'] = AFrom
    msg['To'] = ATo
    msg['Date'] = email.utils.formatdate(localtime=True)
    if get_alert():       
        msg['X-Priority'] = '2'
    
    #* html = html.replace('\r\n', '<br>')
    part1 = MIMEText(html, 'html')
    msg.attach(part1)
            
    # send the messsage using localhost
    s = smtplib.SMTP('localhost')
    s.sendmail(AFrom, [ATo, ABcc], msg.as_string())
    s.quit()
     
LOG_FILE = 'get_currency_exchange_rates.log'
 
FAlert = False  # global used to set alert
 
# output all prints to file
f = open(LOG_FILE, 'w')
sys.stdout = f 
        
start = datetime.datetime.now()
print_html_header()
print_html('Started: ' + start.strftime('%Y-%m-%d %H:%M:%S'))
filename_date_suffix = start.strftime('%Y-%m-%d')

con = MySQLdb.connect(host='localhost', user='hyperflight', passwd='b747', db='hyperflight')
cur = con.cursor()

# (SS,3/11/15) added following to update the exchange rates in hypercomposites
con_hypercomposites = MySQLdb.connect(host='localhost', user='hypercomposites', passwd='b787', db='hypercomposites')
cur_hypercomposites = con_hypercomposites.cursor()

# fetch all (non GBP) currencies from currencies table
cur.execute("SELECT Currency FROM currencies WHERE Currency <> 'GBP' ORDER BY Currency")
rows = cur.fetchall()
for row in rows:
    get_currency('GBP', row[0]) # e.g. get_currency('GBP', 'EUR')
#get_currency('GBP', 'EUR')

finish = datetime.datetime.now()
time_taken = finish - start
print_html('')
print_html('Finished: ' + finish.strftime('%Y-%m-%d %H:%M:%S'))
print_html('Time taken: %0.3f seconds' % (time_taken.seconds + time_taken.microseconds / 1E6))

print_html('')

# (SS,26/4/14)
get_comparisons()

# (SS,3/11/15)
cur_hypercomposites.close()
con_hypercomposites.close()

cur.close()
con.close()

finish = datetime.datetime.now()
time_taken = finish - start
#print_html('')
print_html('Finished: ' + finish.strftime('%Y-%m-%d %H:%M:%S'))
print_html('Time taken: %0.3f seconds' % (time_taken.seconds + time_taken.microseconds / 1E6))
print_html_footer()

# set back to default out and close the opened log file
sys.stdout = sys.__stdout__
f.close()

# send the file as an email
#send_email_file(LOG_FILE, 'HyperFlight Exchange Rate Result', 'exchange_rate@hyperflight.co.uk', 'surinder@itpartnership.com', 'surinder@ssbsoft.co.uk')
send_email_file(LOG_FILE, 'HyperFlight Exchange Rate Result', 'exchange_rate@hyperflight.co.uk', 'sales@hyperflight.co.uk', 'contactforms@itpartnership.com')