Subversion Repositories SmartDukaan

Rev

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