| Line 2... |
Line 2... |
| 2 |
from shop2020.config.client.ConfigClient import ConfigClient
|
2 |
from shop2020.config.client.ConfigClient import ConfigClient
|
| 3 |
from shop2020.model.v1.catalog.impl import DataService
|
3 |
from shop2020.model.v1.catalog.impl import DataService
|
| 4 |
from shop2020.model.v1.catalog.impl.DataService import SnapdealItem, MarketplaceItems, Item, Category, \
|
4 |
from shop2020.model.v1.catalog.impl.DataService import SnapdealItem, MarketplaceItems, Item, Category, \
|
| 5 |
MarketPlaceUpdateHistory
|
5 |
MarketPlaceUpdateHistory
|
| 6 |
from shop2020.thriftpy.model.v1.order.ttypes import OrderSource
|
6 |
from shop2020.thriftpy.model.v1.order.ttypes import OrderSource
|
| 7 |
import mechanize
|
- |
|
| 8 |
import sys
|
7 |
import sys
|
| 9 |
import cookielib
|
8 |
import cookielib
|
| 10 |
from time import sleep
|
9 |
from time import sleep
|
| 11 |
import json
|
10 |
import json
|
| 12 |
import smtplib
|
11 |
import smtplib
|
| Line 18... |
Line 17... |
| 18 |
import email
|
17 |
import email
|
| 19 |
from email.mime.multipart import MIMEMultipart
|
18 |
from email.mime.multipart import MIMEMultipart
|
| 20 |
import email.encoders
|
19 |
import email.encoders
|
| 21 |
import copy
|
20 |
import copy
|
| 22 |
import traceback
|
21 |
import traceback
|
| - |
|
22 |
import urllib2
|
| - |
|
23 |
import pymongo
|
| - |
|
24 |
from dtr.utils.utils import get_mongo_connection
|
| 23 |
|
25 |
|
| 24 |
config_client = ConfigClient()
|
26 |
config_client = ConfigClient()
|
| 25 |
host = config_client.get_property('staging_hostname')
|
27 |
host = config_client.get_property('staging_hostname')
|
| 26 |
DataService.initialize(db_hostname=host)
|
28 |
DataService.initialize(db_hostname=host)
|
| 27 |
|
29 |
|
| 28 |
courierCostToSync = []
|
30 |
courierCostToSync = []
|
| 29 |
oldPricing = []
|
31 |
oldPricing = []
|
| 30 |
|
32 |
|
| - |
|
33 |
headers = {
|
| - |
|
34 |
'User-agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11',
|
| - |
|
35 |
'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
|
| - |
|
36 |
'Accept-Language' : 'en-US,en;q=0.8',
|
| - |
|
37 |
'Accept-Charset' : 'ISO-8859-1,utf-8;q=0.7,*;q=0.3'
|
| - |
|
38 |
}
|
| - |
|
39 |
|
| 31 |
class __SnapdealInfo:
|
40 |
class __SnapdealInfo:
|
| 32 |
def __init__(self, sellingPrice, weight, transferPrice, commission, commissionPercentage, courierCost, sellingPriceSnapdeal, transferPriceSnapdeal, fixedMargin, \
|
41 |
def __init__(self, sellingPrice, weight, transferPrice, commission, commissionPercentage, courierCost, sellingPriceSnapdeal, transferPriceSnapdeal, fixedMargin, \
|
| 33 |
fixedMarginPercentage, collectionCharges, logisticCostSnapdeal, weightSnapdeal, supc, itemId, parentCategory, productGroup ,brand, modelName, modelNumber, \
|
42 |
fixedMarginPercentage, collectionCharges, logisticCostSnapdeal, weightSnapdeal, supc, itemId, parentCategory, productGroup ,brand, modelName, modelNumber, \
|
| 34 |
color,reason,woodenPackagingCost):
|
43 |
color,reason,woodenPackagingCost):
|
| 35 |
|
44 |
|
| Line 56... |
Line 65... |
| 56 |
self.color = color
|
65 |
self.color = color
|
| 57 |
self.reason = reason
|
66 |
self.reason = reason
|
| 58 |
self.woodenPackagingCost = woodenPackagingCost
|
67 |
self.woodenPackagingCost = woodenPackagingCost
|
| 59 |
|
68 |
|
| 60 |
|
69 |
|
| 61 |
def getBrowserObject():
|
70 |
def addCookie():
|
| 62 |
br = mechanize.Browser(factory=mechanize.RobustFactory())
|
- |
|
| 63 |
cj = cookielib.LWPCookieJar()
|
- |
|
| 64 |
br.set_cookiejar(cj)
|
- |
|
| 65 |
br.set_handle_equiv(True)
|
- |
|
| 66 |
br.set_handle_redirect(True)
|
- |
|
| 67 |
br.set_handle_referer(True)
|
- |
|
| 68 |
br.set_handle_robots(False)
|
- |
|
| 69 |
br.set_debug_http(False)
|
- |
|
| 70 |
br.set_debug_redirects(False)
|
- |
|
| 71 |
br.set_debug_responses(False)
|
- |
|
| 72 |
|
- |
|
| 73 |
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
|
- |
|
| 74 |
|
71 |
try:
|
| 75 |
br.addheaders = [('User-agent','Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11'),
|
- |
|
| 76 |
('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'),
|
- |
|
| 77 |
('Accept-Encoding', 'gzip,deflate,sdch'),
|
- |
|
| 78 |
('Accept-Language', 'en-US,en;q=0.8'),
|
- |
|
| 79 |
('Accept-Charset', 'ISO-8859-1,utf-8;q=0.7,*;q=0.3')]
|
72 |
mongoHost = config_client.get_property('support_backup_host')
|
| 80 |
return br
|
73 |
except:
|
| 81 |
|
- |
|
| 82 |
def login(url):
|
- |
|
| 83 |
br = getBrowserObject()
|
- |
|
| 84 |
br.open(url)
|
- |
|
| 85 |
response = br.open(url)
|
74 |
mongoHost = "localhost"
|
| 86 |
ungzipResponse(response, br)
|
- |
|
| 87 |
br.select_form(name="pgLogin:navbar")
|
- |
|
| 88 |
br.form['pgLogin:navbar:txtUserName'] = "saholic-snapdeal@saholic.com"
|
75 |
cookie = get_mongo_connection(host=mongoHost).Snapdeal.Cookies.find_one()
|
| 89 |
br.form['pgLogin:navbar:txtPassword'] = "snapsaholic15"
|
76 |
headers['Cookie'] = 'SERVERID=%s;sfJSESSIONID=%s;'%(cookie.get('SERVERID'),cookie.get('sfJSESSIONID'))
|
| 90 |
print "Trying to login"
|
- |
|
| 91 |
response = br.submit()
|
- |
|
| 92 |
return br
|
77 |
print headers
|
| 93 |
|
78 |
|
| 94 |
def ungzipResponse(r,b):
|
79 |
def ungzipResponse(r,b):
|
| 95 |
headers = r.info()
|
80 |
headers = r.info()
|
| 96 |
if headers['Content-Encoding']=='gzip':
|
81 |
if headers.get('Content-Encoding')=='gzip':
|
| 97 |
import gzip
|
82 |
import gzip
|
| 98 |
print "********************"
|
83 |
print "********************"
|
| 99 |
print "Deflating gzip response"
|
84 |
print "Deflating gzip response"
|
| 100 |
print "********************"
|
85 |
print "********************"
|
| 101 |
gz = gzip.GzipFile(fileobj=r, mode='rb')
|
86 |
gz = gzip.GzipFile(fileobj=r, mode='rb')
|
| Line 104... |
Line 89... |
| 104 |
headers["Content-type"] = "text/html; charset=utf-8"
|
89 |
headers["Content-type"] = "text/html; charset=utf-8"
|
| 105 |
r.set_data( html )
|
90 |
r.set_data( html )
|
| 106 |
b.set_response(r)
|
91 |
b.set_response(r)
|
| 107 |
|
92 |
|
| 108 |
|
93 |
|
| 109 |
def populateStuff(br):
|
94 |
def populateStuff():
|
| 110 |
exceptionList = []
|
95 |
exceptionList = []
|
| 111 |
fetchedItems = []
|
96 |
fetchedItems = []
|
| 112 |
items = session.query(SnapdealItem,MarketplaceItems,Item).join((MarketplaceItems,SnapdealItem.item_id==MarketplaceItems.itemId)).join((Item,SnapdealItem.item_id==Item.id)).filter(MarketplaceItems.source==OrderSource.SNAPDEAL).all()
|
97 |
items = session.query(SnapdealItem,MarketplaceItems,Item).join((MarketplaceItems,SnapdealItem.item_id==MarketplaceItems.itemId)).join((Item,SnapdealItem.item_id==Item.id)).filter(MarketplaceItems.source==OrderSource.SNAPDEAL).all()
|
| 113 |
#items = session.query(SnapdealItem,MarketplaceItems,Item).join((MarketplaceItems,SnapdealItem.item_id==MarketplaceItems.itemId)).join((Item,SnapdealItem.item_id==Item.id)).filter(SnapdealItem.item_id==12613).filter(MarketplaceItems.source==OrderSource.SNAPDEAL).all()
|
98 |
#items = session.query(SnapdealItem,MarketplaceItems,Item).join((MarketplaceItems,SnapdealItem.item_id==MarketplaceItems.itemId)).join((Item,SnapdealItem.item_id==Item.id)).filter(SnapdealItem.item_id==12613).filter(MarketplaceItems.source==OrderSource.SNAPDEAL).all()
|
| 114 |
for item in items:
|
99 |
for item in items:
|
| Line 116... |
Line 101... |
| 116 |
marketplaceItem = item[1]
|
101 |
marketplaceItem = item[1]
|
| 117 |
ds_item = item[2]
|
102 |
ds_item = item[2]
|
| 118 |
category = Category.query.filter_by(id=ds_item.category).one()
|
103 |
category = Category.query.filter_by(id=ds_item.category).one()
|
| 119 |
parent_category = Category.query.filter_by(id=category.parent_category_id).first()
|
104 |
parent_category = Category.query.filter_by(id=category.parent_category_id).first()
|
| 120 |
try:
|
105 |
try:
|
| 121 |
snapdealInfo = fetchData(br,snapdealItem.supc)
|
106 |
snapdealInfo = fetchData(snapdealItem.supc)
|
| 122 |
except Exception as e:
|
107 |
except Exception as e:
|
| 123 |
exceptionList.append(item)
|
108 |
exceptionList.append(item)
|
| 124 |
traceback.print_exc()
|
109 |
traceback.print_exc()
|
| 125 |
print "Unable to fetch details ",e
|
110 |
print "Unable to fetch details ",e
|
| 126 |
continue
|
111 |
continue
|
| Line 138... |
Line 123... |
| 138 |
snapdealInfo.color = ds_item.color
|
123 |
snapdealInfo.color = ds_item.color
|
| 139 |
snapdealInfo.weight = ds_item.weight
|
124 |
snapdealInfo.weight = ds_item.weight
|
| 140 |
fetchedItems.append(snapdealInfo)
|
125 |
fetchedItems.append(snapdealInfo)
|
| 141 |
return exceptionList, fetchedItems
|
126 |
return exceptionList, fetchedItems
|
| 142 |
|
127 |
|
| 143 |
def fetchData(br,supc):
|
128 |
def fetchData(supc):
|
| 144 |
print "Fetching data for ",supc
|
129 |
print "Fetching data for ",supc
|
| 145 |
url="http://seller.snapdeal.com/pricing/search?_search=false&gridType=normal&page=1&rows=30&searchType=SUPC&searchValue=%s&sidx=priceUpdateTime&sord=desc"%(supc)
|
130 |
url="http://seller.snapdeal.com/pricing/search?_search=false&gridType=normal&page=1&rows=30&searchType=SUPC&searchValue=%s&sidx=priceUpdateTime&sord=desc"%(supc)
|
| 146 |
response = br.open(url)
|
131 |
request = urllib2.Request(url, headers=headers)
|
| 147 |
ungzipResponse(response, br)
|
132 |
response = urllib2.urlopen(request)
|
| 148 |
print response.read()
|
- |
|
| 149 |
#dataform = str(response.read()).strip("'<>() ").replace('\'', '\"')
|
133 |
#dataform = str(response.read()).strip("'<>() ").replace('\'', '\"')
|
| 150 |
struct = json.loads(response.read())
|
134 |
struct = json.loads(response.read())
|
| 151 |
sdObj = struct['rows'][0]
|
135 |
sdObj = struct['rows'][0]
|
| 152 |
print sdObj
|
136 |
print sdObj
|
| 153 |
if type(sdObj['catalogLive']) is None or not (sdObj['catalogLive']):
|
137 |
if type(sdObj['catalogLive']) is None or not (sdObj['catalogLive']):
|
| Line 635... |
Line 619... |
| 635 |
print "Successfully sent email"
|
619 |
print "Successfully sent email"
|
| 636 |
except:
|
620 |
except:
|
| 637 |
print "Error: unable to send email."
|
621 |
print "Error: unable to send email."
|
| 638 |
|
622 |
|
| 639 |
def main():
|
623 |
def main():
|
| 640 |
print "Opening snapdeal seller login page"
|
- |
|
| 641 |
br = login("http://sellers.snapdeal.com/")
|
624 |
addCookie()
|
| 642 |
exceptionList, fetchedItems = populateStuff(br)
|
625 |
exceptionList, fetchedItems = populateStuff()
|
| 643 |
filteredData = filterData(fetchedItems)
|
626 |
filteredData = filterData(fetchedItems)
|
| 644 |
sendMail(filteredData,exceptionList)
|
627 |
sendMail(filteredData,exceptionList)
|
| 645 |
write_report(filteredData,exceptionList)
|
628 |
write_report(filteredData,exceptionList)
|
| 646 |
syncCourierCost(courierCostToSync)
|
629 |
syncCourierCost(courierCostToSync)
|
| 647 |
sendCCSyncMail()
|
630 |
sendCCSyncMail()
|