Subversion Repositories SmartDukaan

Rev

Rev 4003 | Rev 4100 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3139 chandransh 1
#!/usr/bin/python
2
 
3
'''
4090 chandransh 4
Generates a TSV file to be uploaded to Amazon's seller central.
5
It has three input sources:
6
 1. The catalog database which it reads from the local database.
7
 2. FeatureValues.xls which is generated using the FeatureValueExtractor
8
  script in the ContentStore project.
9
 3. ItemNumbers.xls which is fetched from Nilesh to get UPC/EAN
10
 of all handsets since Amazon is adamant that they'll not accept data w/o it.
11
 
12
Once the CSV file is generated, a header should be added to it.
13
 
14
@attention: The columns and their order in the featurevalues.xls can
15
change depending on the object model. As such the output should be 
16
checked manually. 
17
 
3139 chandransh 18
Created on 01-Sep-2011
19
 
4090 chandransh 20
@author: Chandranshu
3139 chandransh 21
'''
22
import sys
3140 chandransh 23
import csv
3486 chandransh 24
import xlrd
3139 chandransh 25
 
26
if __name__ == '__main__' and __package__ is None:
27
    import os
28
    sys.path.insert(0, os.getcwd())
29
 
30
from shop2020.clients.CatalogClient import CatalogClient
31
 
32
def get_title(item):
4090 chandransh 33
    '''
34
    Returns the title of the Item in the format required by Amazon:
35
    <Brand> <Model Name> <Model Number> | <Color>
36
    '''
3139 chandransh 37
    title = item.brand
38
    if item.modelName:
39
        title = title + ' ' + item.modelName
40
    if item.modelNumber:
41
        title = title + ' ' + item.modelNumber
3584 chandransh 42
    if item.color:
43
        title = title + " | " + item.color
3139 chandransh 44
    return title
45
 
46
def get_hyphenated_name(item):
4090 chandransh 47
    '''
48
    Returns the URL path for a mobile phone.
49
    '''
3139 chandransh 50
    productUrl = item.brand
51
    if item.modelName:
52
        productUrl = productUrl + "-" + item.modelName
53
    if item.modelNumber:
54
        productUrl = productUrl + '-' + item.modelNumber
55
    productUrl = productUrl.replace("/", "-")
56
    productUrl = productUrl.replace(" ", "-")
57
    productUrl = productUrl.replace("--", "-")
58
    productUrl = productUrl.lower()
59
    return productUrl
60
 
61
def get_url(item):
4090 chandransh 62
    '''
63
    Returns the complete URL of a phone.
64
    '''
3139 chandransh 65
    url = "http://www.saholic.com/mobile-phones/"
66
    productUrl = get_hyphenated_name(item)
67
    productUrl = productUrl + "-" + str(item.catalogItemId)
68
    url = url + productUrl;
69
    url = url.replace("--", "-");
70
    return url;
71
 
72
def get_image_url(item):
4090 chandransh 73
    '''
74
    Returns the complete URL of the default image.
75
 
76
    @deprecated: The name of image is now available in the FeatureValues file. 
77
    '''
3139 chandransh 78
    url = "http://static0.saholic.com/images/"
79
    url = url + str(item.catalogItemId) + "/"
80
    url = url + get_hyphenated_name(item) + "-default-0.jpg"
81
    return url
82
 
3486 chandransh 83
def is_active(item):
84
    return item.itemStatus in [2, 3, 6]
85
 
3502 chandransh 86
def get_key(brand, model_number, color):
87
    model_number_str = ''
88
    try:
89
        model_number_str = str(int(model_number))
90
    except:
91
        model_number_str = str(model_number)
92
    if '(' in model_number_str:
93
        model_number_str = model_number_str.split('(')[0]
3992 chandransh 94
    return str('handsets|' + brand.strip().lower() + '|' + model_number_str.strip().lower() + '|' + color.strip().lower())
3502 chandransh 95
 
96
def load_item_numbers():
97
    filename = "/home/ashish/itemNumbers.xls"
98
    workbook = xlrd.open_workbook(filename)
99
    sheet = workbook.sheet_by_index(0)
100
    num_rows = sheet.nrows
101
    itemNumberMap = {}
102
    itemNumberTypeMap = {}
3992 chandransh 103
    for rownum in range(2, num_rows):
3502 chandransh 104
        itemNumber, unused_description, unused_pc, unused_pg, unused_tech, brand, model_number, color = sheet.row_values(rownum)[0:8]
3992 chandransh 105
        key = get_key(brand, model_number, color)
106
        itemNumberStr = str(itemNumber)
107
        if not itemNumberStr.isdigit():
108
            continue
109
        itemNumberMap[key] = itemNumberStr 
110
        if len(itemNumberStr) == 13:
3502 chandransh 111
            itemNumberTypeMap[key] = 'EAN'
3992 chandransh 112
        elif len(itemNumberStr) == 12:
3502 chandransh 113
            itemNumberTypeMap[key] = 'UPC'
114
    print itemNumberMap
115
    return itemNumberMap, itemNumberTypeMap
116
 
3584 chandransh 117
 
118
def normalize_form_factor(formFactor):
119
    if formFactor == 'Candybar':
120
        formFactor = 'candy-bar'
121
    elif formFactor == 'Slider':
122
        formFactor = 'slide'
123
    elif formFactor == 'Flip':
124
        formFactor = 'flip'
125
    elif formFactor == 'Clamshell':
126
        formFactor = 'flip'
127
    else:
128
        formFactor = ''
129
    return formFactor
130
 
131
 
132
def normalize_operating_system(opsys):
133
    if 'Android' in opsys:
134
        opsys = 'Android'
135
    elif 'Symbian' in opsys:
136
        opsys = 'Symbian'
137
    elif 'BlackBerry' in opsys:
138
        opsys = 'Blackberry'
139
    elif 'Windows' in opsys:
140
        opsys = 'Windows Phone'
141
    elif 'bada' in opsys or 'Bada' in opsys:
142
        opsys = 'Bada'
143
    elif 'iOS' in opsys:
144
        opsys = 'iOS'
145
    else:
146
        opsys = ''
147
    return opsys
148
 
149
 
150
def normalize_battery_type(batteryType):
151
    if 'Li-Ion' in batteryType or 'Li-ion' in batteryType or 'Lithium-ion' in batteryType:
152
        batteryType = 'lithium_ion'
153
    elif 'Li-Po' in batteryType:
154
        batteryType = 'lithium_metal'
155
    else:
156
        batteryType = ''
157
    return batteryType
158
 
159
 
160
def get_cellular_technology(multipleSIM, network3G):
161
    cellularTechnology = ''
162
    if multipleSIM == 'Dual-SIM':
163
        cellularTechnology = 'Dual SIM'
164
    if not cellularTechnology:
165
        if network3G != '':
166
            cellularTechnology = '3G'
167
        else:
168
            cellularTechnology = 'GSM'
169
    return cellularTechnology
170
 
171
 
172
def normalize_screen_type(screenType):
173
    if 'LCD' in screenType or 'Nova' in screenType or 'Retina' in screenType:
174
        screenType = 'LCD'
175
    elif 'LED' in screenType:
176
        screenType = 'LED'
177
    else:
178
        screenType = ''
179
    return screenType
180
 
3992 chandransh 181
def get_hotspot_mapping(mappings):
182
    for mapping in mappings:
183
        if mapping.vendorId == 1:
184
            return mapping.itemKey
185
    return None
186
 
3139 chandransh 187
def main():
3502 chandransh 188
    itemNumberMap, itemNumberTypeMap = load_item_numbers() 
3139 chandransh 189
    catalog_client = CatalogClient().get_client()
190
    item_details = []
4003 chandransh 191
    filename = "/home/ashish/featurevalues3.xls"
3486 chandransh 192
    workbook = xlrd.open_workbook(filename)
193
    sheet = workbook.sheet_by_index(0)
194
    num_rows = sheet.nrows
3140 chandransh 195
 
3486 chandransh 196
    writer = csv.writer(open("junglee.csv", "wb"), delimiter='\t', quoting=csv.QUOTE_MINIMAL)
197
    writer.writerow(["SKU","Title","Link","Price","Delivery Time","Recommended Browse Node","Standard Product ID",\
198
                 "Product ID Type","Category","Description","Shipping Cost","Image","List Price","Availability",\
199
                 "Brand","Manufacturer","Mfr part number","Model Number","Computer CPU speed","Hard disk size",\
200
                 "Included RAM size","Optical zoom","Digital zoom","Megapixels","Display size","Screen Resolution",\
201
                 "Display Technology","Flash drive Size","Memory Card Type","Camera type","Viewfinder type","Flash type",\
202
                 "Cellular Technology","Phone Operating System","Talk Time","Standby Time","User Input","Device Type",\
203
                 "Form Factor","Colour Name","Colour Map","Item package quantity","Age","Warranty","Assembly required",\
204
                 "Battery Type","Batteries Included","Batteries Required","Power Source","Power Adapter Included",\
205
                 "Shipping Weight","Weight","Length","Height","Width","Keywords1","Keywords2", "Keywords3","Keywords4",\
206
                 "Keywords5","Bullet point1","Bullet point2","Bullet point3","Bullet point4","Bullet point5",\
207
                 "Other image-url1","Other image-url2","Other image-url3","Other image-url4","Other image-url5",
208
                 "Offer note","Is Gift Wrap Available","Registered Parameter","Update Delete"])
209
 
3992 chandransh 210
    for rownum in range(2, num_rows): #2 is used as the starting index because first row is a test product with 12 years of warranty.
4003 chandransh 211
        unused_categoryName, unused_entityName, entityID, image_url, unused_accessories, unused_softwareApplications, unused_pageTitle,\
3992 chandransh 212
        unused_metaDescription, metaKeywords, snippets, shortSnippet, tagline,\
3486 chandransh 213
        unused_skinSize, screenSize, unused_screenLeftUpperCornerDimension, unused_modelNameSynonyms, unused_modelNumberSynonyms,\
3992 chandransh 214
        warranty, unused_warranty_type, unused_warranty_coverage, \
3486 chandransh 215
        weight, size, formFactor, color, screenType, screenSize, screenResolution, numberOfColors, keyboardType,\
216
        navigation, touchscreenType, sideControls, multimediaKeys, multipleSIM, voip, network2G,\
217
        network3G, gprs, edge, g3, wifi, bluetooth, usb, musicFormats, earphone, speakerPhone,\
218
        fmRadio, internetRadio, ringtoneTypes, fileFormats, streaming, liveTV, hdVideoPlayback,\
219
        resolution, flash, imageFormats, numberOfCameras, secondaryCamera, additionalCameraFeatures,\
220
        builtIn, ram, expansionType, expansionCapacity, batteryType, powerAdaptor, musicPlayback,\
221
        videoPlayback, tvPlayback, talktime2G, talktime3G, standy2G, standby3G, types, markupLanguages,\
222
        unused_http_protocols, unused_browser, unused_mail_protocols, opsys, unused_java, unused_flashPlayer, unused_drm, unused_securityFeatures, unused_gpsType, unused_mms, unused_sms, unused_ems,\
4003 chandransh 223
        unused_instantMessaging, unused_email = sheet.row_values(rownum)[0:88]
3486 chandransh 224
 
3502 chandransh 225
        items = catalog_client.getItemsByCatalogId(entityID)
226
        active_items = filter(is_active, items)
227
        if not active_items:
228
            continue
229
 
3486 chandransh 230
        if screenSize:
231
            screenSize = screenSize.split()[0]
232
 
233
        if screenResolution:
234
            screenResolution = screenResolution.rsplit(' ', 1)[0]
235
 
3584 chandransh 236
        screenType = normalize_screen_type(screenType)
3486 chandransh 237
 
3584 chandransh 238
        cellularTechnology = get_cellular_technology(multipleSIM, network3G)
3486 chandransh 239
 
3584 chandransh 240
        opsys = normalize_operating_system(opsys)
3486 chandransh 241
 
242
        userInput = "keypad"
243
        if touchscreenType != "":
244
            userInput = "touchscreen"
245
 
3584 chandransh 246
        formFactor = normalize_form_factor(formFactor)
3486 chandransh 247
 
248
        if warranty:
249
            warranty = warranty + " manufacturer warranty"
250
 
3584 chandransh 251
        batteryType = normalize_battery_type(batteryType)
3486 chandransh 252
 
3505 chandransh 253
        weight_parts = weight.split()
254
        if len(weight_parts) > 1:
255
            weight = weight_parts[0]
256
            try:
257
                float(weight)
258
            except:
259
                weight = ''
260
 
3486 chandransh 261
        if size == "Not available" or size == '':
262
            length, width, height = ["", "", ""]
263
        else:
264
            list = size.split()
265
            length, width, height = [list[0], list[2], list[4]] 
266
 
3992 chandransh 267
        keywords = []
268
        for keyword in metaKeywords.split(","):
269
            keywords.append(keyword.strip())
3486 chandransh 270
        if len(keywords) < 5:
271
            length = len(keywords)
272
            while length < 5:
273
                keywords.append('')
3502 chandransh 274
                length = length + 1       
3486 chandransh 275
 
3992 chandransh 276
        for keyword in keywords:
277
            if len(keyword) > 50:
278
                print keyword
279
 
3502 chandransh 280
        for item in active_items:
281
            stdProductId = ''
282
            stdProductIdType = ''
283
            if not item.color:
284
                item.color = ''
3992 chandransh 285
 
286
            mappings = catalog_client.getVendorItemMappings(item.id)
287
            key = get_hotspot_mapping(mappings)
288
            if key and itemNumberTypeMap.has_key(key):
3502 chandransh 289
                stdProductId = itemNumberMap[key]
290
                stdProductIdType = itemNumberTypeMap[key]
291
            item_details.append(
292
                        [item.id, get_title(item), get_url(item), item.sellingPrice, "1", "803546031", stdProductId,\
4003 chandransh 293
                         stdProductIdType, "Wireless", tagline, '0', 'http://' + str(image_url), item.mrp, "TRUE",\
3502 chandransh 294
                         item.brand, "", "", item.modelNumber, "", builtIn,\
295
                         ram, "", "", "", screenSize, screenResolution,\
296
                         screenType, "", "", "", "", "",\
3505 chandransh 297
                         cellularTechnology, opsys,\
4090 chandransh 298
                         #talktime2G, standy2G, - TODO: use these values after converting them to minutes and hours respectively
3505 chandransh 299
                         '', '', userInput, "",\
3502 chandransh 300
                         formFactor, item.color, "", "1", "", warranty, "FALSE",\
301
                         batteryType, "TRUE","TRUE", "battery-powered", "TRUE",\
3584 chandransh 302
                         "", weight, length, width, height, keywords[0].strip(), keywords[1].strip(), keywords[2].strip(), keywords[3].strip(),\
303
                         keywords[4].strip(), "", "","","","",\
3502 chandransh 304
                         "","","","","",\
305
                         "","","",""]);
3486 chandransh 306
 
3140 chandransh 307
    for item_detail in item_details:
308
        writer.writerow(item_detail)
3139 chandransh 309
 
310
if __name__ == '__main__':
311
    main()