# 151 currencies, 22650 currency pairs, 364 days (period 1) 134 days (period 2) => 3,035,100(20,100/from_c) - 8,221,950 entries(8361.157)


print('importing packages')
import time
import sqlite3
import json
import requests
import datetime
import math
import pytz
from datetime import date
from multiprocessing.pool import Pool

print('connecting to db')
conn = sqlite3.connect('mastercard_1.sqlite')
cur = conn.cursor()

print('defining functions')
def day_calculator(date):
    return (date - date_1).days + 1
def date_calculator(day):
    return date_1+datetime.timedelta(day-1)
def date_stringer(date):
    return date.strftime('%Y-%m-%d')

print('defining constants')
start_from_id = int(input('from_id initial value: '))
start_to_id = int(input('to_id initial value: '))
base_url = 'https://www.mastercard.us/settlement/currencyrate/fxDate={date_};transCurr={from_};crdhldBillCurr={to_};bankFee=0.00;transAmt=1/conversion-rate'

first_date=date(2016,2,29)
now = datetime.datetime.now(pytz.timezone('US/Eastern'))
if now.hour < 14:
    today=now.date() - datetime.timedelta(days=1)
else:
    today=now.date()
    
date_1=today - datetime.timedelta(days=364)
if date_1.weekday()==6:
    date_1=date_1+datetime.timedelta(days=1)
if date_1.weekday()==5:
    date_1=date_1+datetime.timedelta(days=2)

date_string = date_stringer(date_1)
print('first date in period', date_1, 'today:',today) 
late_day=day_calculator(date(2016,10,14))

print('grabbing codes from db')
cur.execute('SELECT code FROM Currency_Codes')
code_tuples=cur.fetchall()
codes = [ x[0] for x in code_tuples ]
number_of_codes = len(codes)




print('initiating')
for code in codes[(start_from_id-1):]:
    start_time_f = datetime.datetime.now()
    to_id = start_to_id
    from_c = code
    cur.execute('SELECT id FROM Currency_Codes WHERE code=?', (from_c,))
    from_id = cur.fetchone()[0]
    while to_id <= number_of_codes:
        start_time_t = datetime.datetime.now()
        to_c = codes[to_id-1]
        print(from_c,to_c)
        if from_c is to_c:
            to_id +=1
            continue