forked from apiforfun/theforexapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
first.py
64 lines (48 loc) · 1.88 KB
/
first.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import csv
from pymongo import MongoClient
client = MongoClient('mongodb://localhost:27017/')
db = client['theforexapi']
collection = db['currency']
currencies = ['USD', 'JPY', 'BGN', 'CYP', 'CZK', 'DKK', 'EEK', 'GBP', 'HUF', 'LTL', 'LVL', 'MTL', 'PLN', 'ROL', 'RON', 'SEK', 'SIT', 'SKK', 'CHF', 'ISK', 'NOK', 'HRK', 'RUB', 'TRL', 'TRY', 'AUD', 'BRL', 'CAD', 'CNY', 'HKD', 'IDR', 'ILS', 'INR', 'KRW', 'MXN', 'MYR', 'NZD', 'PHP', 'SGD', 'THB', 'ZAR']
def calculate_new_base(new_base, new_row):
new_curr = {
'date': new_row['date'],
'base': new_base,
}
new_curr['rates']={}
new_curr['rates']['EUR']=1/float(new_row['rates'][new_base])
for x in currencies:
if x in new_row['rates'].keys() and x is not new_base:
new_curr['rates'][x] = float(new_row['rates'][x])/float(new_row['rates'][new_base])
collection.insert_one(new_curr)
print(new_curr)
print('baaaaaaaaaaaaaaaaaaaaaaaaaaaaaase', new_base)
with open('../eurofxref-hist.csv', newline='') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
# print(row)
# break
orginal_row = row
# print(orginal_row)
orginal_row.pop("")
# print(orginal_row)
new_row = {}
new_row['rates'] = {}
for key in orginal_row.keys():
if orginal_row[key] != 'N/A':
if key == 'Date':
new_row['date'] = orginal_row[key]
else:
new_row['rates'][key] = orginal_row[key]
new_row['base'] = 'EUR'
print(new_row)
for k in new_row['rates'].keys():
if k not in ('date', 'base'):
calculate_new_base(k, new_row)
collection.insert_one(new_row)
print(new_row)
# break
# new_base = 'USD'
# base = 'EUR'
# target_currency = 'JPY'
# target_currency_value = target/new_base