Subversion Repositories SmartDukaan

Rev

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

'''
Created on 16-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 html2text.unescaping import *
from datastore.DataAccessor import *
from datastore.DataCodeAccessor import *



class indiaplaza_spider(BaseSpider):
    """
    Documentation for class indiaplaza_spider
    This spider collects the url for the individual phones
    and store them in table datastore_datadefinition_indiaplaza_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
        Since, the number of pages is not fixed Ct and no are used to make it dynamic 
       """
       initialize_table()
       da = DataHelper() 
       #INDIAPLAZA_DOMAINNAME = "indiaplaza"
       INDIAPLAZA_DOMAINNAME = get_code_word("INDIAPLAZA_DOMAINNAME")   
       self.domain_name = INDIAPLAZA_DOMAINNAME
       #INDIAPLAZA_CT = 18
       #INDIAPLAZA_CT = int(get_code_word("INDIAPLAZA_CT"))
       #INDIAPLAZA_NO = 1
       CT = int(da.get_extra_vars('indiaplaza_count'))
       NO = 1
       if CT>18:
           NO = CT
       #INDIAPLAZA_URL = "http://www.indiaplaza.in/mobile-phones-Mobiles-1.htm?PageNo="
       INDIAPLAZA_URL = get_code_word("INDIAPLAZA_URL")
       while(NO<=CT):
            url1 = INDIAPLAZA_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
        #INDIAPLAZA_HOMEPAGE = "www.indiaplaza.com"
        INDIAPLAZA_HOMEPAGE = get_code_word("INDIAPLAZA_HOMEPAGE")
        da = DataHelper()
        da.add_supplier(self.domain_name, INDIAPLAZA_HOMEPAGE)
        listreq = []
        
        #for each request a referer has to be set
        #INDIAPLAZA_REFERER = "www.google.com/search"
        INDIAPLAZA_REFERER = get_code_word("INDIAPLAZA_REFERER")
        for url1 in self.start_urls:
            request = Request(url = str(url1), callback=self.parse, dont_filter=True)
            request.headers.setdefault("Referer", INDIAPLAZA_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 of individual phone
        Xpath3 = Give us url of individual phone
        Url1 = To get full url for individual phones
        """
        da = DataHelper()
        #INDIAPLAZA_URL1 = "http://www.indiaplaza.in"
        INDIAPLAZA_URL1 = get_code_word("INDIAPLAZA_URL1")
        hxs = HtmlXPathSelector(response)
        #INDIAPLAZA_XPATH1 = '//tr/td/table[@id="browsesku"]'
        INDIAPLAZA_XPATH1 = get_code_word("INDIAPLAZA_XPATH1")
        phone_info = hxs.select(INDIAPLAZA_XPATH1)
        items = []
        #INDIAPLAZA_XPATH2 = './/div[@class="skuimg"]/a/@title'
        INDIAPLAZA_XPATH2 = get_code_word("INDIAPLAZA_XPATH2")
        #INDIAPLAZA_XPATH3 = './/div[@class="skuimg"]/a/@href'
        INDIAPLAZA_XPATH3 = get_code_word("INDIAPLAZA_XPATH3")
        if not phone_info:
            ct = int(da.get_extra_vars('indiaplaza_count'))
            if ct>18:
                fails = int(da.get_extra_vars('indiaplaza_fails'))
                fails = fails+1
                da.set_extra_vars('indiaplaza_fails',str(fails),'')
                if fails > 0:
                    da.set_extra_vars('indiaplaza_flag','FALSE','')
                da.set_extra_vars('indiaplaza_fails',str(fails),'')
        else:            
            for i in phone_info:
                item = {}
                item['title'] = i.select(INDIAPLAZA_XPATH2)[0].extract()
                item['url'] = i.select(INDIAPLAZA_XPATH3)[0].extract()
                items.append(item)
                
            for item in items:
                str2 = INDIAPLAZA_URL1 + str(item['url'])
                da.add_ipbasic(item['title'],unescape(str2))
                     
SPIDER = indiaplaza_spider()