-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_extraction.py
92 lines (61 loc) · 2.51 KB
/
data_extraction.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import json
import numpy as np
import tabula
import pandas as pd
import http.client
import boto3
class DataExtractor:
def retrieve_pdf_data(self, link : str):
tables = tabula.read_pdf(link,pages="all")
joined =pd.concat(tables,ignore_index=True)
return joined
def list_number_of_stores(self,endpoint: str, header: dict):
conn = http.client.HTTPSConnection("aqj7u5id95.execute-api.eu-west-1.amazonaws.com")
headers = {
"x-api-key" : header["x-api-key"]
}
conn.request("GET", endpoint, headers=headers)
res = conn.getresponse()
data = res.read()
conn.close()
try :
#print(data.decode("utf-8"))
data = json.loads(data.decode("utf-8"))
return(data["number_stores"])
except Exception as ex:
print(type(ex))
return np.nan
def retrieve_single_stores_data(self, connection : http.client.HTTPSConnection, endpoint : str, header: dict, storeNumber : str):
connection.request("GET", endpoint + str(storeNumber), headers=header)
res = connection.getresponse()
data = res.read()
try :
data = json.loads(data.decode("utf-8"))
df = pd.DataFrame(data, index=[0])
return df
except ValueError as ex:
print(ex)
return None
def retrieve_stores_data(self, endpoint : str, header: dict, totalStores : int):
headers = {
"x-api-key" : header["x-api-key"]
}
conn = http.client.HTTPSConnection ("aqj7u5id95.execute-api.eu-west-1.amazonaws.com")
df = pd.DataFrame()
for i in range(0,totalStores+1,1):
single = self.retrieve_single_stores_data(connection=conn,endpoint=endpoint,header=headers,storeNumber=i)
if len(df) > 0 :
df = pd.concat([df,single],ignore_index=True)
else:
df = single
conn.close()
return df
def extract_from_s3(self, bucket : str, file : str):
s3 = boto3.client('s3')
s3.download_file(bucket,file,file)
if "csv" in file.lower() :
return pd.read_csv(file)
elif "json" in file.lower():
return pd.read_json(file)
else:
raise TypeError("Only CSV and JSON files are currently implemented")