| 12854 |
kshitij.so |
1 |
from elixir import *
|
|
|
2 |
from shop2020.model.v1.catalog.impl.DataService import Item, Category
|
|
|
3 |
from shop2020.model.v1.catalog.impl import DataService
|
|
|
4 |
from shop2020.config.client.ConfigClient import ConfigClient
|
|
|
5 |
from shop2020.clients.InventoryClient import InventoryClient
|
|
|
6 |
from shop2020.model.v1.catalog.script import AmazonAsyncScraper
|
|
|
7 |
import xlwt
|
|
|
8 |
from datetime import datetime
|
|
|
9 |
import time
|
|
|
10 |
|
|
|
11 |
config_client = ConfigClient()
|
|
|
12 |
host = config_client.get_property('staging_hostname')
|
|
|
13 |
|
|
|
14 |
amScraper = AmazonAsyncScraper.Products("AKIAII3SGRXBJDPCHSGQ", "B92xTbNBTYygbGs98w01nFQUhbec1pNCkCsKVfpg", "AF6E3O0VE0X4D")
|
|
|
15 |
DataService.initialize(db_hostname=host)
|
|
|
16 |
|
|
|
17 |
|
|
|
18 |
def writeReport():
|
|
|
19 |
maxNodeLength = 0
|
|
|
20 |
countItems = 0
|
|
|
21 |
sheet_iterator = 1
|
|
|
22 |
wbk = xlwt.Workbook(encoding="UTF-8")
|
|
|
23 |
sheet = wbk.add_sheet('Browsing Nodes')
|
|
|
24 |
xstr = lambda s: s or ""
|
|
|
25 |
heading_xf = xlwt.easyxf('font: bold on; align: wrap off, vert centre, horiz center')
|
|
|
26 |
excel_integer_format = '0'
|
|
|
27 |
integer_style = xlwt.XFStyle()
|
|
|
28 |
integer_style.num_format_str = excel_integer_format
|
|
|
29 |
inventory_client = InventoryClient().get_client()
|
|
|
30 |
fbaAvailableInventorySnapshot = inventory_client.getAllAvailableAmazonFbaItemInventory()
|
|
|
31 |
for fbaInventoryItem in fbaAvailableInventorySnapshot:
|
|
|
32 |
if fbaInventoryItem.location==0:
|
|
|
33 |
sku = 'FBA'+str(fbaInventoryItem.item_id)
|
|
|
34 |
elif fbaInventoryItem.location==1:
|
|
|
35 |
sku = 'FBB'+str(fbaInventoryItem.item_id)
|
|
|
36 |
else:
|
|
|
37 |
continue
|
|
|
38 |
nodes = amScraper.get_product_category_for_sku('A21TJRUUN4KGV', sku)
|
|
|
39 |
countItems+=1
|
|
|
40 |
if countItems > 10:
|
|
|
41 |
time.sleep(10)
|
|
|
42 |
countItems = 0
|
|
|
43 |
if maxNodeLength < len(nodes):
|
|
|
44 |
maxNodeLength = len(nodes)
|
|
|
45 |
sheet.write(sheet_iterator, 0, sku[3:])
|
|
|
46 |
sheet.write(sheet_iterator, 1, sku)
|
|
|
47 |
try:
|
|
|
48 |
it = Item.query.filter_by(id=fbaInventoryItem.item_id).one()
|
|
|
49 |
category = Category.query.filter_by(id=it.category).one()
|
|
|
50 |
parent_category = Category.query.filter_by(id=category.parent_category_id).first()
|
|
|
51 |
sheet.write(sheet_iterator, 2, xstr(it.brand)+" "+xstr(it.model_name)+" "+xstr(it.model_number)+" "+xstr(it.color))
|
|
|
52 |
sheet.write(sheet_iterator, 3, parent_category.display_name)
|
|
|
53 |
except:
|
|
|
54 |
pass
|
|
|
55 |
row_iterator = 3
|
|
|
56 |
for node in nodes:
|
|
|
57 |
row_iterator+=1
|
|
|
58 |
sheet.write(sheet_iterator, row_iterator, getNodeValue(node))
|
|
|
59 |
sheet_iterator+=1
|
|
|
60 |
writeheaders(sheet,heading_xf,maxNodeLength)
|
|
|
61 |
filename = "/tmp/browsing-node"+str(datetime.now())+".xls"
|
|
|
62 |
wbk.save(filename)
|
|
|
63 |
|
|
|
64 |
|
|
|
65 |
def getNodeValue(node):
|
|
|
66 |
x = ""
|
|
|
67 |
for eachNode in node:
|
|
|
68 |
x+=eachNode+'>'
|
|
|
69 |
return x[:-1]
|
|
|
70 |
|
|
|
71 |
def writeheaders(sheet,heading_xf,maxNodeLength):
|
|
|
72 |
sheet.write(0, 0, "Item Id", heading_xf)
|
|
|
73 |
sheet.write(0, 1, "Amazon Sku", heading_xf)
|
|
|
74 |
sheet.write(0, 2, "Product Name", heading_xf)
|
|
|
75 |
sheet.write(0, 3, "Category", heading_xf)
|
|
|
76 |
for node_header in range(maxNodeLength):
|
|
|
77 |
sheet.write(0, node_header+4, "Browsing Node "+str(node_header+1), heading_xf)
|
|
|
78 |
|
|
|
79 |
def main():
|
|
|
80 |
writeReport()
|
|
|
81 |
|
|
|
82 |
|
|
|
83 |
if __name__=='__main__':
|
|
|
84 |
main()
|