-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcryptoDB.py
58 lines (46 loc) · 1.81 KB
/
cryptoDB.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
import boto3
import json
from boto3.dynamodb.conditions import Key, Attr
import datetime
class CryptoDB:
"""class that contains dynamo DB access options"""
def __init__(self, tableName="cryptoDB"):
with open('.keyaws.json') as data_file:
data = json.load(data_file)
self.client=boto3.client(
'dynamodb',
aws_access_key_id=data['accessKeyId'],
aws_secret_access_key=data['secretAccessKey'],
region_name=data['region']
)
self.tableName=tableName
def getDateRangeData(self, currencyPair, startDate, endDate):
"""Gets DynamoDB price data from date range from table"""
result=[]
query={
"TableName":self.tableName,
"ExpressionAttributeNames":{"#t": "time", "#cp":"currencyPair"},
"ExpressionAttributeValues": {
":startDate": {'S': startDate},
":endDate": {'S': endDate},
":cp" : {'S': currencyPair}
},
"KeyConditionExpression":"#cp = :cp and #t between :startDate and :endDate"
}
while True:
try:
response=self.client.query(**query)
for i in response[u'Items']:
result.append(i["time"]["S"]+","+i["value"]["N"])
if 'LastEvaluatedKey' not in response:
break
else:
query['ExclusiveStartKey']=response['LastEvaluatedKey']
except Exception as inst:
print("Error getting data based on time range")
print(inst)
return result
def insertKey(self, currencyPair, date, price):
pass
a=CryptoDB()
data=a.getDateRangeData("BTC-USD", "2017-08-02T23:10:38.486348", '2017-08-25T23:24:27.271083')