aboutsummaryrefslogtreecommitdiff
path: root/crawler/nevrax/spiders/update.py
blob: b3f7aa136179f8b7f0fdb51a4f65141ed499fab2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# -*- coding: utf-8 -*-
import scrapy
from scrapy.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor
from scrapy import Selector

import config
import datetime

from database.models import Nevrax

from dateutil.relativedelta import *

import logging

class NevraxSpider(CrawlSpider):
    name = "nevrax_updater"
    custom_settings = {
        'ITEM_PIPELINES': {
            'crawler.nevrax.pipelines.NevraxPipeline': 0
        }
    }
    allow_domains = [config.NEVRAX_URL]

    datas = Nevrax.select(Nevrax.url).dicts()
    datas_array = []
    for value in datas:
        datas_array.append(value['url'])
    start_urls = datas_array

    def start_requests(self):
        for url in self.start_urls:
            logging.info(url)
            try:
                page = Nevrax.get(Nevrax.url == url)
                if page.date_updated < datetime.datetime.now()+relativedelta(weeks=-1):
                    yield scrapy.Request(url, callback=self.parse_url, dont_filter=True)
            except Nevrax.DoesNotExist:
                yield scrapy.Request(url, callback=self.parse_url, dont_filter=True)
                continue

    def parse_url(self, response):
        sel = Selector(response)
        yield {
                'url': response.url,
                'title': response.css('title::text').extract_first(),
                'content': ''.join(sel.select("//div[@class='bodya']//text()").extract()).strip(),
                'content_length': len(response.body),
                'date_updated': datetime.datetime.now()
        }