Subversion Repositories SmartDukaan

Rev

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