def getTweets(self, name='random', searchterm=None): name = name.lower() if name == 'random': name = random.choice(self.twitterUsernames.keys()) if name not in self.twitterUsernames: return (False, "I don't know anybody by the name of '{}', sorry. ".format(name)) tweetFileName = os.path.join(GlobalStore.scriptfolder, 'data', 'tweets', '{}.txt'.format(self.twitterUsernames[name])) if not os.path.exists(tweetFileName): self.executeScheduledFunction() return (False, "I don't seem to have the tweets for '{}', sorry! I'll retrieve them right away, try again in a bit".format(name)) tweets = FileUtil.getAllLinesFromFile(tweetFileName) if searchterm is not None: #Search terms provided! Go through all the tweets to find matches regex = None try: regex = re.compile(searchterm, re.IGNORECASE) except (re.error, SyntaxError): self.logWarning("[STtip] '{}' is an invalid regular expression. Using it literally".format(searchterm)) for i in xrange(0, len(tweets)): #Take a tweet from the start, and only put it back at the end if it matches the regex tweet = tweets.pop(0) if regex and regex.search(tweet) or searchterm in tweet: tweets.append(tweet) if len(tweets) == 0: return (False, "Sorry, no tweets matching your search were found") else: return (True, tweets)
def getTweets(self, name='random', searchterm=None): name = name.lower() if name == 'random': name = random.choice(self.twitterUsernames.keys()) if name not in self.twitterUsernames: return (False, "I don't know anybody by the name of '{}', sorry. ".format( name)) tweetFileName = os.path.join( GlobalStore.scriptfolder, 'data', 'tweets', '{}.txt'.format(self.twitterUsernames[name])) if not os.path.exists(tweetFileName): self.executeScheduledFunction() return ( False, "I don't seem to have the tweets for '{}', sorry! I'll retrieve them right away, try again in a bit" .format(name)) tweets = FileUtil.getAllLinesFromFile(tweetFileName) if searchterm is not None: #Search terms provided! Go through all the tweets to find matches regex = None try: regex = re.compile(searchterm, re.IGNORECASE) except (re.error, SyntaxError): self.logWarning( "[STtip] '{}' is an invalid regular expression. Using it literally" .format(searchterm)) for i in xrange(0, len(tweets)): #Take a tweet from the start, and only put it back at the end if it matches the regex tweet = tweets.pop(0) if regex and regex.search(tweet) or searchterm in tweet: tweets.append(tweet) if len(tweets) == 0: return (False, "Sorry, no tweets matching your search were found") else: return (True, tweets)