-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrecycling.py
More file actions
133 lines (104 loc) · 4.87 KB
/
recycling.py
File metadata and controls
133 lines (104 loc) · 4.87 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import urllib.request
import csv
import boto3
#creating CSV files
outputFile_receipt = open("machine_receipt.csv","w", encoding='utf-8')
outputWriter_receipt = csv.writer(outputFile_receipt)
#opening connection with S3
s3 = boto3.resource('s3')
client = boto3.client('s3')
#listing the contents of the bucket
response = client.list_objects(Bucket="centauro-recycling-machine-processing")
r = len(response['Contents'])
for r in response['Contents']:
receipt = (r['Key'])
try:
#formating the URL with the filename
with urllib.request.urlopen("https://s3-us-west-2.amazonaws.com/centauro-recycling-machine-processing/{0}".format(receipt)) as receipt_file:
data = (receipt_file.read(1024).decode('utf-8'))
except urllib.error.HTTPError:
pass
# spliting the receipt in a list
mylist_data = data.splitlines()
#index to iteract with the list
i = -1
for a in mylist_data:
i += 1
#locate the [RefVal] entry in the receipt to know the quantity of bottles
if mylist_data[i] == "[RefVal]":
refval = i
#if RefVal's index is 2 it means there's only 1 bottle
if i == 2:
row_array=[]
b = mylist_data[1:i]
s = str(b)
#machine name from S3
row_array.append(r['Key'][0:8])
#bootle identification from receipt [BotGr]
row_array.append(''.join(c for c in s if c in '0123456789= '))
#date of the receipt from S3
date=r['LastModified']
date_str=str(date)
row_array.append(date_str[0:19])
z = -1
for w in mylist_data:
#index to iteract with the list
z += 1
#receipt number
if "RcptSer" in mylist_data[z]:
s = mylist_data[z]
row_array.append(''.join(c for c in s if c in '0123456789'))
#number of bottles refunded
if "Refunded" in mylist_data[z]:
s = mylist_data[z]
row_array.append(''.join(c for c in s if c in '0123456789'))
#total value for the refunded bottles
if "RefundTotalVal" in mylist_data[z]:
s = mylist_data[z]
row_array.append(''.join(c for c in s if c in '0123456789'))
#barcode of the receipt
if "Barcode1" in mylist_data[z]:
s = mylist_data[z]
row_array.append(s[9::])
outputWriter_receipt.writerow(row_array)
else:
#creating x and y to get the position of each bottle; from last to first
x = i
while x > 1:
y = x-1
#index to iteract with the list
z = -1
row_array_2=[]
b = mylist_data[y:x]
s = str(b)
#machine name from S3
row_array_2.append(r['Key'][0:8])
#bootle identification from receipt [BotGr]
row_array_2.append(''.join(c for c in s if c in '0123456789= '))
#date of the receipt from S3
date=r['LastModified']
date_str=str(date)
row_array_2.append(date_str[0:19])
#iteract through the receipt to get the information for each bottle in a new line
for w in mylist_data:
z += 1
#receipt number
if "RcptSer" in mylist_data[z]:
s = mylist_data[z]
row_array_2.append(''.join(c for c in s if c in '0123456789'))
#number of bottles refunded
if "Refunded" in mylist_data[z]:
s = mylist_data[z]
row_array_2.append(''.join(c for c in s if c in '0123456789'))
#total value for the refunded bottles
if "RefundTotalVal" in mylist_data[z]:
s = mylist_data[z]
row_array_2.append(''.join(c for c in s if c in '0123456789'))
#barcode of the receipt
if "Barcode1" in mylist_data[z]:
s = mylist_data[z]
row_array_2.append(s[9::])
outputWriter_receipt.writerow(row_array_2)
x = x - 1
y = y - 1
outputFile_receipt.close()