Exemple #1
0
    def post(self):
        """Creates a bot"""
        error_status = 0
        user = users.get_current_user()
        if not user:
            self.response.out.write("not registered in google")
        name = self.request.get('name')
        password = self.request.get('password')
        feed = self.request.get('feed')

        # Verify the account information
        gae_twitter = GAETwitter(username=name, password=password)
        verify_result = gae_twitter.verify()
        if (verify_result != True):
            error_status = error_status + 1

        # Verify the feed URL
        d = feedparser.parse(feed)

        if (d.bozo > 0):
            error_status = error_status + 2
        server_error = ""
        if (error_status == 0):
            bot = Bot(name=name, password=password, feed=feed)
            bot.user = user
            try:
                bot.put()
            except Exception, e:
                error_status |=  4
                server_error = str(e)
Exemple #2
0
    def postfeedentry(self):
        """Posts a tweet, returns the number of tweets"""
        gae_twitter = GAETwitter(username=self.name, password=self.password)
        feed_result = feedparser.parse(self.feed)
        if 'bozo_exception' in feed_result:
            self.changestatus(str(feed_result.bozo_exception))
            logging.debug("bozo_exception : %s" % self.status)
            return 0
        # Record current time
        self.put()

        if not ("entries" in feed_result):
            self.changestatus("%s has no entries" % self.feed)
            return 0
        post_count = 0
        last_post = self.last_post
        newest_entry_datetime = last_post
        if not last_post:
            return 0
        for entry in feed_result.entries:
            # Check the entry's time
            if not 'updated_parsed' in entry:
                self.changestatus("No attribute 'updated_parsed' in %s"
                                  % (str(entry)))
                return 0
            entry_datetime = datetime(*(entry.updated_parsed[:6]))
            if entry_datetime <= last_post:
                continue
            if entry_datetime > newest_entry_datetime:
                newest_entry_datetime = entry_datetime

            message = self.create_post_message(entry)
            if not message:
                continue

            # post to twitter
            logging.debug(message)
            status = gae_twitter.post(message)
            self.status = status
            if post_count == 0:
                self.put()
            post_count = post_count + 1
            if post_count > 2:
                break
        self.last_post = newest_entry_datetime
        if post_count == 0:
            newstatus = "No new entries at %s" % str(datetime.now())
        else:
            newstatus = "Posted %d entries at %s" % (post_count,
                                                       str(datetime.now()))
        self.changestatus(newstatus)
        return post_count