Subversion Repositories SmartDukaan

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13735 kshitij.so 1
from elixir import *
2
import pymongo
3
import MySQLdb
4
import codecs
5
from shop2020.config.client.ConfigClient import ConfigClient
6
from shop2020.model.v1.catalog.impl import DataService
7
from shop2020.model.v1.catalog.impl.DataService import Item, Category
8
from shop2020.thriftpy.model.v1.catalog.ttypes import status
9
 
10
config_client = ConfigClient()
11
host = config_client.get_property('catalog_service_db_hostname')
12
host_mongo = config_client.get_property('staging_hostname')
13
host_warehouse = config_client.get_property('warehouse_service_server_host')
14
 
15
DataService.initialize(db_hostname=host)
16
 
17
categoryMap ={}
18
itemMap = {} 
19
 
20
con = None
21
BASE_URL = "http://saholic.com/"
22
xstr = lambda s: s or ""
23
 
24
db = MySQLdb.connect(host_warehouse,"root","shop2020","warehouse" )
25
cursor = db.cursor()
26
 
27
class __ItemInfo:
28
 
29
    def __init__(self, itemId, price, url, imageUrl, ean, shortDesc, brand, modelName, color, productGroup, catalogItemId, modelNumber):
30
        self.itemId = itemId
31
        self.price = price
32
        self.url = url
33
        self.imageUrl = imageUrl
34
        self.ean = ean
35
        self.shortDesc = shortDesc
36
        self.modelName = modelName
37
        self.color = color
38
        self.productGroup = productGroup
39
        self.catalogItemId = catalogItemId
40
        self.modelNumber = modelNumber
41
        self.brand = brand
42
 
43
def get_mongo_connection(host='localhost', port=27017):
44
    global con
45
    if con is None:
46
        print "Establishing connection %s host and port %d" %(host,port)
47
        try:
48
            con = pymongo.MongoClient(host, port)
49
        except Exception, e:
50
            print e
51
            return None
52
    return con
53
 
54
def generateCategoryMap():
55
    global categoryMap
56
    result = session.query(Category).all()
57
    for cat in result:
58
        categoryMap[cat.id] = [cat.parent_category_id ,cat.display_name]    
59
 
60
def populateActiveProducts():
61
    generateCategoryMap()
62
    items = session.query(Item).filter(Item.status == status.ACTIVE).all()
63
    try:
64
        for item in items:
65
            info = __ItemInfo(item.id, item.sellingPrice, None, None, None, None, item.brand, item.model_name, item.color, None, item.catalog_item_id, item.model_number)
66
            if item.category == 10006:
67
                info.productGroup = "Mobile Phones > "+item.brand
68
            elif item.category == 10010:
69
                info.productGroup = "Tablets > "+item.brand
70
            else:
71
                info.productGroup = (categoryMap.get(categoryMap.get(item.category)[0])[1]+" > "+categoryMap.get(item.category)[1]+" > "+item.brand).replace(","," ")
72
 
73
            itemMap[item.id] = info
74
    finally:
75
        session.close()
76
 
77
def populateContent():
78
    global itemMap
79
    for v in itemMap.itervalues():
80
        try:
81
            print "MOngo ",v.catalogItemId
82
            col = list(get_mongo_connection(host_mongo).CONTENT.siteContent.find({'_id':v.catalogItemId}))
83
            if len(col) ==0:
84
                continue
85
            else:
86
                v.url = col[0]['url']
87
                v.imageUrl = col[0]['defaultImageUrl']
88
                v.shortDesc = col[0]['keySpecs']
89
        except Exception as e:
90
            print e
91
 
92
def getEanInformation():
93
    global itemMap
94
    for v in itemMap.itervalues():
95
        sql = '''select itemNumber  from inventoryItem where itemId=%d limit 1''' % \
96
            (v.itemId)
97
        print sql
98
        cursor.execute(sql)
99
        data = cursor.fetchone()
100
        print data
101
        if (data is None or data[0] is None or len(data[0])==0):
102
            v.ean = ""
103
        else:
104
            v.ean = data[0]
105
 
106
def writeFile():
107
    f = codecs.open('IdealoProductFeed-V1','w','utf-8')
108
    f.write('Article number\tEAN (European article number) / GTIN (Global Trade Item Number)\tManufacturers code / number\t \
109
    Manufacturer\tProduct Name\tDescription\tProduct Group\tPrice INR\tDelivery status\tProduct URL\tPicture URL\tDelivery Costs\n')
110
    for v in itemMap.itervalues():
111
        if v.url is None:
112
            continue
113
        try:
114
            f.write(str(v.itemId)+'\t'+str(v.ean)+'\t'+""'\t'+v.brand+'\t'+getProductName(v)+'\t'+getShortDesc(v.shortDesc)+'\t'+v.productGroup+'\t'+str(v.price)+'\t'+'In Stock'+'\t'+BASE_URL+v.url+'\t'+v.imageUrl+'\t'+'0.00'+'\n')
115
        except Exception as e:
116
            print e
117
            print "Exception in writing ",v.itemId
118
    f.close()
119
 
120
def getShortDesc(descArr):
121
    description = "" 
122
    if descArr is None or len(descArr) == 0:
123
        return description
124
    else:
125
        for desc in descArr:
126
            description += desc.replace(',',' ')
127
        return description
128
 
129
 
130
def getProductName(item):
131
    return (xstr(item.brand)+" "+xstr(item.modelName)+" "+xstr(item.modelNumber)+" "+xstr(item.color)).replace(",","")
132
 
133
def main():
134
    populateActiveProducts()
135
    populateContent()
136
    try:
137
        getEanInformation()
138
    finally:
139
        db.close()
140
        print "Db connection closed"
141
    writeFile()
142
 
143
 
144
if __name__=='__main__':
145
    main()