Subversion Repositories SmartDukaan

Rev

Rev 281 | Blame | Compare with Previous | Last modification | View Log | RSS feed

'''
Created on 13-May-2010

@author: gaurav
'''

from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector
from scrapy.http import Request

from demo.items import DemoItem
from scrapy.contrib.spidermiddleware import referer
from scrapy.http.headers import Headers
from scrapy.http.request.form import FormRequest
from scrapy.log import msg
from scrapy.http.response import Response

from datastore.DataAccessor import *
from datastore.DataCodeAccessor import *
from html2text.unescaping import *
from elixir import *

class infi_spider(BaseSpider):
    """
    Documentation for class infi_spider
    This spider collects the information for the individual phones
    and store them in table datastore_datadefinition_infibeam_data
    """
    def __init__(self): 
       """
        Documentation for constructor
        initialize_table is called to make all the tables known in
        the scope of this class.
        Also start url needs to be feeded to the spider through start_urls.append
        Domainname is name by which this spider is known outside
        So this will be used as an argument for calling this spider.
        As the number of pages to be crawled are not fixed so ct and no are used to make it dynamic.  
        """
       initialize_table()
       da = DataHelper()
       #INFIBEAM_DOMAINNAME = "infibeam"   
       INFIBEAM_DOMAINNAME = get_code_word("INFIBEAM_DOMAINNAME")
       print INFIBEAM_DOMAINNAME
       self.domain_name = INFIBEAM_DOMAINNAME
       #INFIBEAM_CT = 15
       #INFIBEAM_CT = int(get_code_word("INFIBEAM_CT")) 
       #ct = 15     
       ct = int(da.get_extra_vars('infibeam_count'))
       #INFIBEAM_NO = 1
       no = 1
       if ct>14:
           no = ct
       #INFIBEAM_URL = "http://www.infibeam.com/Mobiles/search?page="
       INFIBEAM_URL = get_code_word("INFIBEAM_URL")
       while(no<=ct):
            url1 = INFIBEAM_URL + str(no)
            self.start_urls.append(url1)
            no=no+1
        
    def start_requests(self):
        """
        Documentation for method start_requests
        To set various properties of the request to be made
        like referer, headers and all.
        Also suppliers entry need to be done in the table
        datastore_datadefinition_suppliers.
        @return a list of well formed requests which will be 
        crawled by spider and spider will return the response
        """
        #adding entry for the supplier i.e its name and site
        #INFIBEAM_HOMEPAGE = "www.infibeam.com"
        INFIBEAM_HOMEPAGE = get_code_word("INFIBEAM_HOMEPAGE")
        da = DataHelper()
        da.add_supplier(self.domain_name, INFIBEAM_HOMEPAGE)
        listreq = []
        
        #for each request a referer has to be set
        #INFIBEAM_REFERER = "www.google.com/search"
        INFIBEAM_REFERER = get_code_word("INFIBEAM_REFERER") 
        for url1 in self.start_urls:
            request = Request(url = str(url1), callback=self.parse, dont_filter=True)
            request.headers.setdefault("Referer", INFIBEAM_REFERER)
            listreq.append(request)    
        return listreq
        
    def parse(self, response):
        """
        Documentation for method parse
        @param response of individual requests
        Using Xpaths needed information is extracted out of the response
        and added to the database
        Xpath1 = Give us section for individual phone
        Xpath2 = Give us name for individual phone
        Xpath3 = Give us quoted price for individual phone
        vatplustax = to get final price from quoted price
        Removelist = To filer the prices so as to make them integer for eg remove ',' or 'Rs'
        """
        da = DataHelper()
        #INFIBEAM_VATPLUSTAX = 0
        INFIBEAM_VATPLUSTAX = int(get_code_word("INFIBEAM_VATPLUSTAX"))
        #list elements are separated by ';'
        #INFIBEAM_REMOVELIST = ["Rs.",",","-","/","Rs"]
        INFIBEAM_REMOVELIST = str(get_code_word("INFIBEAM_REMOVELIST"))
        if len(INFIBEAM_REMOVELIST)>0:
            INFIBEAM_REMOVELIST = INFIBEAM_REMOVELIST.split(';') 
         
        for x in INFIBEAM_REMOVELIST:
            print x
            
        hxs = HtmlXPathSelector(response)
        #INFIBEAM_XPATH1 = '//ul[@class="srch_result portrait"]/li'
        INFIBEAM_XPATH1 = get_code_word("INFIBEAM_XPATH1")
        phone_info = hxs.select(INFIBEAM_XPATH1)
        #INFIBEAM_XPATH2 = './/p/span[@class="title"]/text()'
        INFIBEAM_XPATH2 = get_code_word("INFIBEAM_XPATH2")
        #INFIBEAM_XPATH3 = './/p/span[@class="price"]/text()'
        INFIBEAM_XPATH3 = get_code_word("INFIBEAM_XPATH3")
        items = []
        
        if not phone_info:
            ct = int(da.get_extra_vars('infibeam_count'))
            if ct>14:
                fails = int(da.get_extra_vars('infibeam_fails'))
                fails = fails+1
                if fails > 0:
                    da.set_extra_vars('infibeam_flag','FALSE','')
                da.set_extra_vars('infibeam_fails',str(fails),'')
        else:
            for i in phone_info:
                item = {}
                item['name'] = i.select(INFIBEAM_XPATH2)[0].extract()
                item['price'] = i.select(INFIBEAM_XPATH3)[0].extract()
                items.append(item)
            
            for i in items:
                amnt = i['price']
                if amnt != '':        
                    for r in INFIBEAM_REMOVELIST: 
                        while amnt.find(r) != -1:
                            amnt = amnt.replace(r, "") 
                        amnt = amnt.strip()
                pr = int(amnt) + int(INFIBEAM_VATPLUSTAX)
                da.add_infiphone(i['name'], amnt,pr)
        
SPIDER = infi_spider()