File size: 3,216 Bytes
63a63dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import os.path as osp
from threading import current_thread

from .utils import ThreadPool


class Feeder(ThreadPool):
    """Base class for feeder.

    A thread pool of feeder threads, in charge of feeding urls to parsers.

    Attributes:
        thread_num (int): An integer indicating the number of threads.
        global_signal (Signal): A :class:`Signal` object for communication
            among all threads.
        out_queue (Queue): A queue connected with parsers' inputs,
            storing page urls.
        session (Session): A session object.
        logger (Logger): A logging.Logger object used for logging.
        workers (list): A list storing all the threading.Thread objects
            of the feeder.
        lock (Lock): A :class:`Lock` instance shared by all feeder threads.
    """

    def __init__(self, thread_num, signal, session):
        """Init Feeder with some shared variables."""
        super().__init__(thread_num=thread_num, in_queue=None, name="feeder")
        self.signal = signal
        self.session = session

    def feed(self, **kwargs):
        """Feed urls.

        This method should be implemented by users.
        """
        raise NotImplementedError

    def worker_exec(self, **kwargs):
        """Target function of workers"""
        self.feed(**kwargs)
        self.logger.info(f"thread {current_thread().name} exit")

    def __exit__(self):
        self.logger.info("all feeder threads exited")


class UrlListFeeder(Feeder):
    """Url list feeder which feed a list of urls"""

    def feed(self, url_list, offset=0, max_num=0):
        if isinstance(url_list, str):
            if osp.isfile(url_list):
                with open(url_list) as fin:
                    url_list = [line.rstrip("\n") for line in fin]
            else:
                raise OSError(f"url list file {url_list} not found")
        elif not isinstance(url_list, list):
            raise TypeError('"url_list" can only be a filename or a str list')

        if offset < 0 or offset >= len(url_list):
            raise ValueError('"offset" exceed the list length')
        else:
            if max_num > 0:
                end_idx = min(len(url_list), offset + max_num)
            else:
                end_idx = len(url_list)
            for i in range(offset, end_idx):
                url = url_list[i]
                self.out_queue.put(url)
                self.logger.debug(f"put url to url_queue: {url}")


class SimpleSEFeeder(Feeder):
    """Simple search engine like Feeder"""

    def feed(self, url_template, keyword, offset, max_num, page_step):
        """Feed urls once

        Args:
            url_template: A string with parameters replaced with "{}".
            keyword: A string indicating the searching keyword.
            offset: An integer indicating the starting index.
            max_num: An integer indicating the max number of images to be crawled.
            page_step: An integer added to offset after each iteration.
        """
        for i in range(offset, offset + max_num, page_step):
            url = url_template.format(keyword, i)
            self.out_queue.put(url)
            self.logger.debug(f"put url to url_queue: {url}")