-
Notifications
You must be signed in to change notification settings - Fork 16
/
sheets.py
248 lines (213 loc) · 8.17 KB
/
sheets.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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import gspread
# import pdb
import datetime
import os
from oauth2client.service_account import ServiceAccountCredentials
URL = os.environ['AE_gsheet_url']
SHEET_NAME = 'Sheet2'
scope = ['https://spreadsheets.google.com/feeds']
credentials = ServiceAccountCredentials.from_json_keyfile_name('CAMSCAS-6ac566f0e517.json', scope)
def get_sheet_dict(sheet_url,worksheet_name):
gc = gspread.authorize(credentials)
wkb = gc.open_by_url(sheet_url)
wks = wkb.worksheet(worksheet_name)
v_list = wks.get_all_values()
heading = v_list[0]
v_list_of_dict = []
for v in v_list[2:]:
# pdb.set_trace()
v_list_of_dict.append(dict((heading[i],v[i]) for i in range(len(v))))
return v_list_of_dict
def clear_google_sheet(sheet_url, worksheet_name):
gc = gspread.authorize(credentials)
wkb = gc.open_by_url(sheet_url)
wks = wkb.worksheet(worksheet_name)
wks.resize(rows=1)
# Add a record to the end of google sheet url supplied
# no need to pass index
def add_record_from_dict(sheet_url, worksheet_name, dict_rec):
# one at a time
gc = gspread.authorize(credentials)
wkb = gc.open_by_url(sheet_url)
wks = wkb.worksheet(worksheet_name)
values_list = [ i for i in wks.row_values(1) if i]
dict_rec["#"] = wks.row_count #index = row_count + 1(next item) - 1(1st row heading)
if not set(dict_rec.keys()) == set(values_list):
raise Exception("Dictionary is not well formed for the sheet.")
new_row = []
for i in values_list:
new_row.append(dict_rec[i])
wks.append_row(new_row)
# index is handled in insertion logic
def create_order_dict(order_id, title, tracking_id, carrier, status, order_dt, recv_dt, price, updated_on):
return {
"Order ID" :order_id,
"Title" :title,
"Tracking ID":tracking_id,
"Tracking Status" :carrier,
"Status" :status,
"Order Date" :order_dt,
"Days Left" :recv_dt,
"Price" :price,
"Updated On" :updated_on
}
def batch_update_gsheet(sheet_url, worksheet_name, list_rec, ts):
gc = gspread.authorize(credentials)
wkb = gc.open_by_url(sheet_url)
wks = wkb.worksheet(worksheet_name)
rc = len(list_rec)
# cc = len(list_rec[0].keys())
# cc = len(list_rec[0])
print("number of rows:"+str(wks.row_count))
wks.resize(rc+10,10)
print("number of rows after resize:"+str(wks.row_count))
# wks.add_rows(rc+10-wks.row_count)
print('A2:J'+str(rc+1))
cell_list = wks.range('A2:J'+str(rc+1))
i = 0
j = 0
for cell in cell_list:
# print(list_rec[i])
if(j==0):
cell.value = str(i+1)
elif(j==1):
cell.value = list_rec[i]['Order ID']
elif(j==2):
cell.value = list_rec[i]['Title']
elif(j==3):
cell.value = list_rec[i]['Tracking ID']
elif(j==4):
cell.value = list_rec[i]['Tracking Status']
elif(j==5):
cell.value = list_rec[i]['Status']
elif(j==6):
cell.value = list_rec[i]['Order Date']
elif(j==7):
cell.value = list_rec[i]['Days Left']
elif(j==8):
cell.value = list_rec[i]['Price']
elif(j==9):
cell.value = str(datetime.datetime.now())
j=-1 # made0 in increment step
i+=1
# elif(j==10):
# j=0
# i += 1
# continue
j += 1
# cell.value = 'O_o'
wks.update_cells(cell_list) # Update in batch
def save_aliexpress_orders(dict_orders):
if 'Not Shipped' in dict_orders:
list_awaiting_shipment = dict_orders['Not Shipped']
else:
list_awaiting_shipment = []
if 'Shipped' in dict_orders:
list_awaiting_delivery = dict_orders['Shipped']
else:
list_awaiting_delivery = []
if 'Order Awaiting Payment' in dict_orders:
list_awaiting_payment = dict_orders['Order Awaiting Payment']
else:
list_awaiting_payment = []
if 'Order Completed' in dict_orders:
list_completed = dict_orders['Order Completed']
else:
list_completed = []
# batch update
batch_save_list = []
#tracking, carrier, status should be item wise, not order wise
# for awaiting shipment, tracking id is sent as blank
for i in list_awaiting_shipment:
for j in i['product_list']:
dict_save = create_order_dict(
i['order_id'],
j['title'],
'',
'',
i['status'],
i['order_dt'],
'',
j['amount'],
str(datetime.datetime.now())
)
# add_record_from_dict(URL,SHEET_NAME,dict_save)
# Append to batch list
batch_save_list.append(dict_save)
for i in list_awaiting_delivery:
for j in i['product_list']:
try:
dict_save = create_order_dict(
i['order_id'],
j['title'],
i['tracking_id'],
i['tracking_status'],
i['status'],
i['order_dt'],
''.join(i['status_days_left'].strip('Your order will be closed in:').strip().split(' ')),
j['amount'],
str(datetime.datetime.now())
)
# add_record_from_dict(URL,SHEET_NAME,dict_save)
# Append to batch list
batch_save_list.append(dict_save)
except:
print(j.keys())
import sys
print(sys.exc_info())
print('Error in saving order'+i['order_id'])
for i in list_awaiting_payment:
for j in i['product_list']:
try:
dict_save = create_order_dict(
i['order_id'],
j['title'],
i['tracking_id'],
i['tracking_status'],
i['status'],
i['order_dt'],
''.join(i['status_days_left'].strip('Your order will be closed in:').strip().split(' ')),
j['amount'],
str(datetime.datetime.now())
)
# add_record_from_dict(URL,SHEET_NAME,dict_save)
# Append to batch list
batch_save_list.append(dict_save)
except:
print(j.keys())
import sys
print(sys.exc_info())
print('Error in saving order'+i['order_id'])
for i in list_completed:
for j in i['product_list']:
try:
dict_save = create_order_dict(
i['order_id'],
j['title'],
i['tracking_id'],
i['tracking_status'],
i['status'],
i['order_dt'],
'0 days',
j['amount'],
str(datetime.datetime.now())
)
# add_record_from_dict(URL,SHEET_NAME,dict_save)
# Append to batch list
batch_save_list.append(dict_save)
except:
print(j.keys())
import sys
print(sys.exc_info())
print('Error in saving order'+i['order_id'])
batch_update_gsheet(URL,SHEET_NAME, batch_save_list, str(datetime.datetime.now()))
if __name__ == '__main__':
# clear_google_sheet(URL, SHEET_NAME)
# add_record_from_dict(URL,SHEET_NAME,create_order_dict('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', str(datetime.datetime.now())))
batch_update_gsheet(URL,
SHEET_NAME, [
create_order_dict('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', str(datetime.datetime.now())),
create_order_dict('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', str(datetime.datetime.now())),
create_order_dict('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', str(datetime.datetime.now())),
create_order_dict('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', str(datetime.datetime.now()))],
str(datetime.datetime.now()))