Subversion Repositories SmartDukaan

Rev

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