Subversion Repositories SmartDukaan

Rev

Rev 253 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

'''
Created on 06-Jun-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 import *
from babel.messages.pofile import unescape
import urllib

class babuchak3(BaseSpider):
    
    def __init__(self):
        initialize_table()
        #BABUCHAK_DOMAINNAME2 = "babuchak2"   
        BABUCHAK_DOMAINNAME2 = get_code_word("BABUCHAK_DOMAINNAME2")
        self.domain_name = BABUCHAK_DOMAINNAME2  
        da = DataHelper()
        for item in da.get_allbabuchakphoneurls():
            self.start_urls.append(item.url)
            
    def start_requests(self):
        listreq = []
        #for each request a referer has to be set
        #BABUCHAK_REFERER = "www.google.com/search"
        BABUCHAK_REFERER = get_code_word("BABUCHAK_REFERER")
        for url1 in self.start_urls:
            request = Request(url = str(url1), callback=self.parse)
            request.headers.setdefault("Referer", BABUCHAK_REFERER)
            listreq.append(request)
        return listreq
        
    def parse(self, response):
        #url1 needed to get complete urls
        da = DataHelper()
        hxs = HtmlXPathSelector(response)
        #BABUCHAK_XPATH5 = '//td[@class="text-header"]/text()'
        BABUCHAK_XPATH5 = get_code_word("BABUCHAK_XPATH5")
        #BABUCHAK_XPATH6 = '//td[@class="xl63"]//strong/span/text()'
        BABUCHAK_XPATH6 = get_code_word("BABUCHAK_XPATH6")
        #BABUCHAK_XPATH7 = '//td[@class="mod-item-body-title"]/b/text()'
        BABUCHAK_XPATH7 = get_code_word("BABUCHAK_XPATH7")
        #BABUCHAK_REMOVELIST = ["Rs.","Rs",",","-","/"]
        #list separated by ';'
        BABUCHAK_REMOVELIST = get_code_word("BABUCHAK_REMOVELIST")
        BABUCHAK_REMOVELIST = BABUCHAK_REMOVELIST.split(';')
        name = hxs.select(BABUCHAK_XPATH5)[0].extract()
        try:
            shown_price = hxs.select(BABUCHAK_XPATH6)[0].extract()
            final_price = hxs.select(BABUCHAK_XPATH6)[2].extract()
        except:
            final_price = shown_price = hxs.select(BABUCHAK_XPATH7)[0].extract()
            
             
        name = name.strip()
        shown_price = shown_price.strip()
        final_price = final_price.strip()
        
        if shown_price != '':        
            for r in BABUCHAK_REMOVELIST: 
                while shown_price.find(r) != -1:
                    shown_price = shown_price.replace(r, "")
        shown_price = shown_price.strip()
            
        if final_price != '':        
            for r in BABUCHAK_REMOVELIST: 
                while final_price.find(r) != -1:
                    final_price = final_price.replace(r, "")
        final_price = final_price.strip()
        ps1 = shown_price.find('.')
        if ps1 != -1:
            shown_price = shown_price[0:ps1]
            final_price = shown_price
        shown_price = int(shown_price)
        final_price = int(final_price)
        if shown_price>final_price:
            try:
                shown_price = hxs.select(BABUCHAK_XPATH6)[1].extract()
                final_price = hxs.select(BABUCHAK_XPATH6)[2].extract()
            except:
                final_price = shown_price = hxs.select(BABUCHAK_XPATH7)[0].extract()
                
            name = name.strip()
            shown_price = shown_price.strip()
            final_price = final_price.strip()
            
            if shown_price != '':        
                for r in BABUCHAK_REMOVELIST: 
                    while shown_price.find(r) != -1:
                        shown_price = shown_price.replace(r, "")
            shown_price = shown_price.strip()
                
            if final_price != '':        
                for r in BABUCHAK_REMOVELIST: 
                    while final_price.find(r) != -1:
                        final_price = final_price.replace(r, "")
            final_price = final_price.strip()
            ps1 = shown_price.find('.')
            if ps1 != -1:
                shown_price = shown_price[0:ps1]
                final_price = shown_price
        
            shown_price = int(shown_price)
            final_price = int(final_price)
            
        print name
        print shown_price
        print final_price
        da.add_babuchakphone(name,shown_price,final_price)        
SPIDER = babuchak3()