Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
13569 amit.gupta 1
'''
2
Created on Jan 15, 2015
3
 
4
@author: amit
5
'''
6
from pymongo.mongo_client import MongoClient
7
import importlib
8
import mechanize
13582 amit.gupta 9
sourceMap = {1:"amazon", 2:"flipkart", 3:"snapdeal", 4:"spice", 5:"homeshop18"}
13569 amit.gupta 10
 
13582 amit.gupta 11
 
13569 amit.gupta 12
def getStore(source_id):
13
    #module = sourceMap[source_id]
14
    store = Store(source_id)
15
    module = importlib.import_module("dtr.sources." + sourceMap[source_id])
16
    store = getattr(module, "Store")(source_id)
17
    return store
18
 
19
class ScrapeException(Exception):
20
    """Exception raised for errors in the input.
21
 
22
    Attributes:
23
        expr -- input expression in which the error occurred
24
        msg  -- explanation of the error
25
    """
26
 
27
    def __init__(self, expr, msg):
28
        self.expr = expr
29
        self.msg = msg
30
 
31
class ParseException(Exception):
32
    """Exception raised for errors in the input.
33
 
34
    Attributes:
35
        expr -- input expression in which the error occurred
36
        msg  -- explanation of the error
37
    """
38
 
39
    def __init__(self, expr, msg):
40
        self.expr = expr
41
        self.msg = msg
42
 
43
class Store(object):
44
 
45
    ORDER_PLACED = 'Order Placed'
46
    ORDER_DELIVERED = 'Delivered'
47
    ORDER_SHIPPED = 'Shipped' #Lets see if we can make use of it
48
    ORDER_CANCELLED = 'Cancelled'
49
 
13610 amit.gupta 50
    CB_PENDING = 'Pending'
51
    CB_CREDITED = 'Credited to wallet'
52
    CB_NA = 'Not Applicable'
53
    CB_APPROVED = 'Approved'
54
    CB_CANCELLED = 'Cancelled'
13569 amit.gupta 55
 
13610 amit.gupta 56
 
13569 amit.gupta 57
    def __init__(self, store_id):
58
        self.store_id = store_id
13576 amit.gupta 59
        self.store_name = sourceMap[store_id]
13569 amit.gupta 60
 
61
 
62
    def getName(self):
63
        raise NotImplementedError
64
 
65
    def scrapeAffiliate(self, startDate=None, endDate=None):
66
        raise NotImplementedError
67
 
68
    def saveToAffiliate(self, offers):
69
        raise NotImplementedError
70
 
71
    def scrapeStoreOrders(self,):
72
        raise NotImplementedError
13610 amit.gupta 73
 
13569 amit.gupta 74
    '''
13610 amit.gupta 75
    To Settle payback for respective stores.
76
    Also ensures that settlement happens only for closed orders
77
    '''
78
    def settlePayBack(self,):
79
        raise NotImplemented
80
    '''
13569 amit.gupta 81
    Parses the order for specific store
82
 
83
    order id, total amount, created on(now() if could not parse
84
    suborder id, title, quantity, unit price, expected delivery date,
85
    status (default would be Order placed)
86
 
87
    once products are identified, each suborder can then be updated
88
    with respective cashback.
89
 
90
    Possible fields to display for Not yet delivered orders are 
91
    Product/Quantity/Amount/Store/CashbackAmount/OrderDate/ExpectedDelivery/OrderStaus/DetailedStatus/CashbackStatus
92
    No need to show cancelled orders.
13610 amit.gupta 93
    CashbackStatus - NotApplicable/Pending/Approved/Cancelled/CreditedToWallet
13569 amit.gupta 94
    OrderStatus - Placed/Cancelled/Delivered
95
    '''
13576 amit.gupta 96
    def parseOrderRawHtml(self, orderId, subTagId, userId, rawHtml, orderSuccessUrl):
13569 amit.gupta 97
 
98
        pass
99
 
100
if __name__ == '__main__':
101
    store = getStore(3)
102
    store.scrapeAffiliate()
103
 
104
 
105
def getBrowserObject():
106
    import cookielib
107
    br = mechanize.Browser(factory=mechanize.RobustFactory())
108
    cj = cookielib.LWPCookieJar()
109
    br.set_cookiejar(cj)
110
    br.set_handle_equiv(True)
111
    br.set_handle_redirect(True)
112
    br.set_handle_referer(True)
113
    br.set_handle_robots(False)
114
    br.set_debug_http(False)
115
    br.set_debug_redirects(False)
116
    br.set_debug_responses(False)
117
 
118
    br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
119
 
120
    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'),
121
                     ('Accept', 'text/html,application/xhtml+xml,application/json,application/xml;q=0.9,*/*;q=0.8'),
122
                     ('Accept-Encoding', 'gzip,deflate,sdch'),                  
123
                     ('Accept-Language', 'en-US,en;q=0.8'),                     
124
                     ('Accept-Charset', 'ISO-8859-1,utf-8;q=0.7,*;q=0.3')]
125
    return br