当前位置: 首页>>代码示例>>Python>>正文


Python Connector.ads_after方法代码示例

本文整理汇总了Python中connector.Connector.ads_after方法的典型用法代码示例。如果您正苦于以下问题:Python Connector.ads_after方法的具体用法?Python Connector.ads_after怎么用?Python Connector.ads_after使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在connector.Connector的用法示例。


在下文中一共展示了Connector.ads_after方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Observer

# 需要导入模块: from connector import Connector [as 别名]
# 或者: from connector.Connector import ads_after [as 别名]
class Observer(threading.Thread):

    # observer states
    RUNNING = "RUNNING"
    PAUSED = "PAUSED"
    
    def __init__(self, url, profile, store, assessor, notifications, update_interval = 180, name = "Unnamed Observer"):
        super(Observer, self).__init__()
        self._interval = update_interval
        self._connector = Connector(url, profile)
        self._store = store
        self._assessor = assessor
        self._notifications = notifications
        self._name = name
        self._quit = False
        self._time_mark = datetime.datetime.now() - datetime.timedelta(days = 1)
        self._state = Observer.RUNNING
        self.name = name
    
    def serialize(self):
        d = dict()
        d["name"] = self._name
        d["url"] = self._connector.url
        d["interval"] = self._interval
        d["profile"] = self._connector.profile_name
        d["store"] = self._store.path is not None

        if self._assessor is not None:
            d["criteria"] = [criterion.serialize() for criterion in self._assessor.criteria]
        # if self._notifications is not None:
        #     d["notifications"] = [notification.serialize() for notification in self._notifications]
        return d

    @property
    def state(self):
        return self._state

    @state.setter
    def state(self, value):
        self._state = value

    @property
    def notifications(self):
        return self._notifications

    def quit(self):
        """
        Make the Thread quit
        """
        self._quit = True

    def _process_ads(self, ads):
        if len(ads) == 0: return
        hits = map(self._assessor.check, ads)
        hit_ads = [ad for ad in compress(ads, hits)]
        new_ads = self._store.add_ads(hit_ads)
        for ad in new_ads:
            try:
                logging.info("Observer '{}' Found Ad: {}".format(self._name, ad["title"]))
            except KeyError:
                logging.info("Observer '{}' Found Ad: {}".format(self._name, ad.key))
            if self._notifications:
                self._notifications.notify_all(ad)
        self._time_mark = sorted(ads, key = lambda ad: ad.datetime)[-1].datetime

    def run(self):
        self._state = Observer.RUNNING
        while True:
            if self._state == Observer.RUNNING:
                logging.info("Observer '{}' polling for new ads since {}".format(self._name, self._time_mark))
                try:
                    ads = self._connector.ads_after(self._time_mark)
                    if self._quit:
                        return   # Quit now if quit() was called while fetching ads

                    self._process_ads(ads)

                except ConnectionError as ex:
                    logging.info("Observer '{}' connection failed with message: {}".format(self._name, ex.args[0]))

            # going to sleep
            next_time = datetime.datetime.now() + datetime.timedelta(seconds=self._interval)
            while datetime.datetime.now() < next_time:
                time.sleep(1)
                if self._quit:
                    return   # Quit now if quit() was called while sleeping
开发者ID:kolomanschaft,项目名称:updatejunkie,代码行数:88,代码来源:observer.py


注:本文中的connector.Connector.ads_after方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。