Subversion Repositories SmartDukaan

Rev

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

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

class univercell_price(BaseSpider):
    
    def __init__(self):
       initialize_table()
       #UNIVERCELL_DOMAINNAME1 = "univercell1"   
       UNIVERCELL_DOMAINNAME1 = get_code_word("UNIVERCELL_DOMAINNAME1")
       self.domain_name = UNIVERCELL_DOMAINNAME1 
       
       # get urls from the database and append them in the list for crawling
       da = DataHelper()
       for pitem in da.get_all_univervendors():
            self.start_urls.append(pitem.v_site.strip())
    
    def start_requests(self):
        
        #for each request a referer has to be set
        listreq = []
        #UNIVERCELL_REFERER = "www.google.com/search"
        UNIVERCELL_REFERER = get_code_word("UNIVERCELL_REFERER")
        for url1 in self.start_urls:
            request = Request(url = str(url1), callback=self.parse)
            request.headers.setdefault("Referer", UNIVERCELL_REFERER)
            listreq.append(request)
        return listreq
        
    def parse(self, response):
        da = DataHelper()
        #VATPLUSTAX = 0
        #removelist is used for converting price to decimal format containing only numbers and '.'
        #UNIVERCELL_REMOVELIST = ["Rs",",","-","/"]
        #list separated by ';'
        UNIVERCELL_REMOVELIST = get_code_word("UNIVERCELL_REMOVELIST")
        UNIVERCELL_REMOVELIST = UNIVERCELL_REMOVELIST.split(';')
        hxs = HtmlXPathSelector(response)
        #UNIVERCELL_XPATH4 = '//td[@class="gray-border"]' 
        UNIVERCELL_XPATH4 = get_code_word("UNIVERCELL_XPATH4")
        sites = hxs.select(UNIVERCELL_XPATH4)
        items = []
        for site in sites:
            item = {}
            #UNIVERCELL_XPATH5 = './/tr[2]/td/a/text()'
            UNIVERCELL_XPATH5 = get_code_word("UNIVERCELL_XPATH5")
            item['title'] = site.select(UNIVERCELL_XPATH5)[0].extract()
            #UNIVERCELL_XPATH6 = './/tr[3]/th/label/text()'
            UNIVERCELL_XPATH6 = get_code_word("UNIVERCELL_XPATH6")
            item['price'] =site.select(UNIVERCELL_XPATH6)[0].extract()
            items.append(item)
              
        for i in items:
            str1 = str(i['title']).strip()
            amnt = i['price']
            if amnt != '':        
                for r in UNIVERCELL_REMOVELIST: 
                    while amnt.find(r) != -1:
                        amnt = amnt.replace(r, "")
            amnt = amnt.strip() 
            # 4% additional vat is there on the price
            UNIVERCELL_VATPLUSTAX = 4*int(amnt)/100
            pr = int(amnt) + UNIVERCELL_VATPLUSTAX 
            #adding model-name,quotedprice and finalprice
            da.add_new_univerphone(unescape(str1),amnt,pr) 
                    
SPIDER = univercell_price()