當前位置: 首頁>>代碼示例>>Python>>正文


Python Reddit.get_daily_top方法代碼示例

本文整理匯總了Python中reddit.Reddit.get_daily_top方法的典型用法代碼示例。如果您正苦於以下問題:Python Reddit.get_daily_top方法的具體用法?Python Reddit.get_daily_top怎麽用?Python Reddit.get_daily_top使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在reddit.Reddit的用法示例。


在下文中一共展示了Reddit.get_daily_top方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: ShowerThoughtBot

# 需要導入模塊: from reddit import Reddit [as 別名]
# 或者: from reddit.Reddit import get_daily_top [as 別名]
class ShowerThoughtBot(Bot):
    def __init__(self, file):
        # Initialize the Bot
        super().__init__(file)
        self.update_time = datetime.now()

        # Load the configurations.
        with open(file, 'r') as y:
            # Load the configs
            config = yaml.load(y)

        # Grab the database filename from the configs.
        self.dbfile = config['database']
        # Create a Reddit object to handle the Reddit-specific tasks.
        self.reddit = Reddit(self.dbfile)


    def parse_message(self, msg, chan, fromNick):
        # logger.debug("parse_message starting with msg " + msg)
        if msg.find("PING :") != -1:
            self.ping()
        elif (msg.find(":hello {}".format(self.nick)) != -1 or
              msg.find(":hello, {}".format(self.nick)) != -1 or
              msg.find(":hi {}".format(self.nick)) != -1):
            logger.info(msg)
            self.hello(chan, fromNick)
        elif (msg.find(":!showerthought") != -1 or
              msg.find(":{}: thought".format(self.nick)) != -1 or
              msg.find(":!stb thought") != -1):
            logger.info(msg)
            self.print_shower_thought(chan, fromNick)
        elif (msg.find(":{}: help".format(self.nick)) != -1 or
              msg.find(":!stb help") != -1):
            logger.info(msg)
            self.print_help(chan)
        elif (msg.find(":!stb source") != -1 or
              msg.find(":{}: source".format(self.nick)) != -1):
            logger.info(msg)
            self.print_source_link(chan)
        elif msg.find(":{}: updatedb".format(self.nick)) != -1:
            if not fromNick == 'mlane':
                self.send_message(chan, "Don't tell me what to do!")
            else:
                self.send_message(chan, "Pulling in some thoughts.")
                self.update_database(False)
        elif msg.find(":{}: shruggie".format(self.nick)) != -1:
            logger.debug("trying to print shruggie")
            self.print_shruggie(chan)
        else:
            logger.info(msg)
            return


    def print_source_link(self, chan):
        self.send_message(chan, "ShowerThoughtBot is by Mike Lane, "
                                "https://github.com/mikelane/ShowerThoughtBot")
        self.send_message(chan, "Feel free to fork or report issues.")


    def print_help(self, chan):
        lines = []
        lines.append("I respond to {}: $command or !stb command".format(
            self.nick))
        lines.append("$command = [help|thought|source]")
        lines.append("Get a shower thought with !showerthought.")
        lines.append("More to come...")
        lines.append("[email protected] for bugs.")

        for line in lines:
            self.send_message(chan, line)


    def print_shower_thought(self, chan, nick):
        # #self.db_lock.acquire()
        db = DBAdapter(self.dbfile)
        thought = db.get_random_thought()
        self.send_message(chan, "okay {}: \"{}\" -{}\r\n".format(
            nick, thought[1], thought[2]))


    def print_shruggie(self, chan):
        self.send_message(chan, "\udcc2\udcaf\_("
                          "\udce3\udc83\udc84)_/\udcc2\udcaf")


    def update_database(self, Scheduled=True):
        if Scheduled:
            now = datetime.now()
            duration = now - self.update_time
            duration = int(duration.total_seconds())
            if duration >= 86400:
                logger.info('Updating database on schedule.')
                self.update_time = now
                #self.db_lock.acquire()
                self.reddit.get_daily_top()
                #self.db_lock.release()
        else:
            self.reddit.get_daily_top()


#.........這裏部分代碼省略.........
開發者ID:mikelane,項目名稱:ShowerThoughtBot,代碼行數:103,代碼來源:ShowerThoughtBot.py


注:本文中的reddit.Reddit.get_daily_top方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。