Rev 13631 | Rev 13677 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
'''Created on Jan 15, 2015@author: amit'''from pymongo.mongo_client import MongoClientimport importlibimport jsonimport mathimport mechanizeimport tracebackimport urllibimport urllib2sourceMap = {1:"amazon", 2:"flipkart", 3:"snapdeal", 4:"spice", 5:"homeshop18"}headers = {'User-agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11','Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8','Accept-Language' : 'en-US,en;q=0.8','Accept-Charset' : 'ISO-8859-1,utf-8;q=0.7,*;q=0.3'}CASHBACK_URL = 'http://api.profittill.com/cashbacks/index/%s/%s'def getStore(source_id):#module = sourceMap[source_id]store = Store(source_id)try:module = importlib.import_module("dtr.sources." + sourceMap[source_id])store = getattr(module, "Store")(source_id)return storeexcept:traceback.print_exc()return Noneclass ScrapeException(Exception):"""Exception raised for errors in the input.Attributes:expr -- input expression in which the error occurredmsg -- explanation of the error"""def __init__(self, expr, msg):self.expr = exprself.msg = msgclass ParseException(Exception):"""Exception raised for errors in the input.Attributes:expr -- input expression in which the error occurredmsg -- explanation of the error"""def __init__(self, expr, msg):self.expr = exprself.msg = msgclass Store(object):ORDER_PLACED = 'Order Placed'ORDER_DELIVERED = 'Delivered'ORDER_SHIPPED = 'Shipped' #Lets see if we can make use of itORDER_CANCELLED = 'Cancelled'CB_INIT = 'Waiting Confirmation'CB_PENDING = 'Pending'CB_CREDITED = 'Credited to wallet'CB_NA = 'Not Applicable'CB_APPROVED = 'Approved'CB_CANCELLED = 'Cancelled'CONF_CB_SELLING_PRICE = 0CONF_CB_DISCOUNTED_PRICE = 1def __init__(self, store_id):client = MongoClient('mongodb://localhost:27017/')self.db = client.Dtrself.store_id = store_idself.store_name = sourceMap[store_id]'''To Settle payback for respective stores.Also ensures that settlement happens only for approved orders'''def settlePayBack(self):orders = self.db.merchantOrder.find({'subOrders.cashBackStatus':Store.CB_APPROVED}, {'userId':1, 'subOrders.cashBackAmount':1})#fordef getName(self):raise NotImplementedErrordef scrapeAffiliate(self, startDate=None, endDate=None):raise NotImplementedErrordef saveToAffiliate(self, offers):raise NotImplementedErrordef scrapeStoreOrders(self,):raise NotImplementedErrordef getCashbackAmount(self, productCode, amount):alagvar = CASHBACK_URL % (self.store_id,productCode)print alagvarfilehandle = urllib2.Request(alagvar,headers=headers)x= urllib2.urlopen(filehandle)map = json.loads(x.read())if map['cashback']==0:return 0else:if map['cashback_type'] == 'percentage':return math.floor((amount * map['cashback'])/100)else:return map['cashback']'''Parses the order for specific storeorder id, total amount, created on(now() if could not parsesuborder id, title, quantity, unit price, expected delivery date,status (default would be Order placed)once products are identified, each suborder can then be updatedwith respective cashback.Possible fields to display for Not yet delivered orders areProduct/Quantity/Amount/Store/CashbackAmount/OrderDate/ExpectedDelivery/OrderStaus/DetailedStatus/CashbackStatusNo need to show cancelled orders.CashbackStatus - NotApplicable/Pending/Approved/Cancelled/CreditedToWalletOrderStatus - Placed/Cancelled/Delivered'''def parseOrderRawHtml(self, orderId, subTagId, userId, rawHtml, orderSuccessUrl):passdef main():store = getStore(3)print store.getCashbackAmount('864683341', 100)if __name__ == '__main__':main()def getBrowserObject():import cookielibbr = mechanize.Browser(factory=mechanize.RobustFactory())cj = cookielib.LWPCookieJar()br.set_cookiejar(cj)br.set_handle_equiv(True)br.set_handle_redirect(True)br.set_handle_referer(True)br.set_handle_robots(False)br.set_debug_http(False)br.set_debug_redirects(False)br.set_debug_responses(False)br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)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'),('Accept', 'text/html,application/xhtml+xml,application/json,application/xml;q=0.9,*/*;q=0.8'),('Accept-Encoding', 'gzip,deflate,sdch'),('Accept-Language', 'en-US,en;q=0.8'),('Accept-Charset', 'ISO-8859-1,utf-8;q=0.7,*;q=0.3')]return br