Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- self._tweetingjitter)
- interval = self._tweetinginterval + jitter
- # Sleep for the interval (in seconds, hence * 60)
- self._message(u'_autotweet', \
- u'Next tweet in %d minutes.' % (interval))
- time.sleep(interval*60)
- def _check_file(self, filename, allowedext=None):
- """Checks whether a file exists, and has a certain extension.
- Arguments
- filename - String that indicates the path to a .txt file
- that should be read by the bot.
- Keyword Arguments
- allowedext - List of allowed extensions, or None to allow all
- extensions. Default value is None.
- Returns
- ok - Boolean that indicates whether the file exists,
- andhas an allowed extension (True), or does not
- (False)
- """
- # Check whether the file exists
- ok = os.path.isfile(filename)
- # Check whether the extension is allowed
- if allowedext != None:
- name, ext = os.path.splitext(filename)
- if ext not in allowedext:
- ok = False
- return ok
- def _construct_tweet(self, database=u'default', seedword=None, \
- prefix=None, suffix=None):
- """Constructs a text for a tweet, based on the current Markov chain.
- The text will be of a length of 140 characters or less, and will
- contain a maximum of 20 words (excluding the prefix and suffix)
- Keyword Arguments
- seedword - A string that indicates what word should be in
- the sentence. If None is passed, or if the word
- is not in the database, a random word will be
- chosen. This value can also be a list of words,
- in which case the list will be processed
- one-by-one until a word is found that is in the
- database. Default value is None.
- database - A string that indicates the name of the
- specific database that you want to use to
- generate the text, or u'default' to use the
- default database. (default = 'default')
- prefix - A string that will be added at the start of each
- tweet (no ending space required). Pass None if
- you don't want a prefix. Default value is None.
- suffix - A string that will be added at the end of each
- tweet (no starting space required). Pass None if
- you don't want a suffix. Default value is None.
- Returns
- tweet - A string with a maximum length of 140 characters.
- """
- sl = 10
- response = u''
- while response == u'' or len(response) > 160:
- # Generate some random text
- response = self.generate_text(sl, seedword=seedword, \
- database=database, verbose=False, maxtries=100)
- # Add the prefix
- if prefix != None:
- response = u'%s %s' % (prefix, response)
- # Add the suffix
- if suffix != None:
- response = u'%s %s' % (response, suffix)
- # Reduce the amount of words if the response is too long
- if len(response) > 160:
- sl -= 1
- return response
- def _error(self, methodname, msg):
- """Raises an Exception on behalf of the method involved.
- Arguments
- methodname - String indicating the name of the method that is
- throwing the error.
- message - String with the error message.
- """
- raise Exception(u"ERROR in Markovbot.%s: %s" % (methodname, msg))
- def _isalphapunct(self, string):
- """Returns True if all characters in the passed string are
- alphabetic or interpunction, and there is at least one character in
- the string.
- Allowed interpunction is . , ; : ' " ! ?
- Arguments
- string - String that needs to be checked.
- Returns
- ok - Boolean that indicates whether the string
- contains only letters and allowed interpunction
- (True) or not (False).
- """
- if string.replace(u'.',u'').replace(u',',u'').replace(u';',u''). \
- replace(u':',u'').replace(u'!',u'').replace(u'?',u''). \
- replace(u"'",u'').isalpha():
- return True
- else:
- return False
- def _message(self, methodname, msg):
- """Prints a message on behalf of the method involved. Friendly
- verion of self._error
- Arguments
- methodname - String indicating the name of the method that is
- throwing the error.
- message - String with the error message.
- """
- print(u"MSG from Markovbot.%s: %s" % (methodname, msg))
- def _triples(self, words):
- """Generate triplets from the word list
- This is inspired by Shabda Raaj's blog on Markov text generation:
- http://agiliq.com/blog/2009/06/generating-pseudo-random-text-with-markov-chains-u/
- Moves over the words, and returns three consecutive words at a time.
- On each call, the function moves one word to the right. For example,
- "What a lovely day" would result in (What, a, lovely) on the first
- call, and in (a, lovely, day) on the next call.
- Arguments
- words - List of strings.
- Yields
- (w1, w2, w3) - Tuple of three consecutive words
- """
- # We can only do this trick if there are more than three words left
- if len(words) < 3:
- return
- for i in range(len(words) - 2):
- yield (words[i], words[i+1], words[i+2])
- def _twitter_reconnect(self):
- """Logs in to Twitter, using the stored OAuth. This function is
- intended for internal use, and should ONLY be called after
- twitter_login has been called.
- """
- # Report the reconnection attempt.
- self._message(u'_twitter_reconnect', \
- u"Attempting to reconnect to Twitter.")
- # Raise an Exception if the twitter library wasn't imported
- if not IMPTWITTER:
- self._error(u'_twitter_reconnect', u"The 'twitter' library could not be imported. Check whether it is installed correctly.")
- # Log in to a Twitter account
- self._t = twitter.Twitter(auth=self._oauth)
- self._ts = twitter.TwitterStream(auth=self._oauth)
- self._loggedin = True
- # Get the bot's own user credentials
- self._credentials = self._t.account.verify_credentials()
- # Report the reconnection success.
- self._message(u'_twitter_reconnect', \
- u"Successfully reconnected to Twitter!")
- os.path.realpath('./')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement