Add search function

This commit is contained in:
Klaus-Uwe Mitterer 2017-03-24 17:02:21 +01:00
parent 09d2d75443
commit 60c32fcb25
2 changed files with 22 additions and 3 deletions

View file

@ -279,7 +279,7 @@ def thread(bot, update, args):
i += 1
for tweet in tweets:
tweetMessage(tweet, update.message.chat_id, bot)
tweetMessage(tweet, message.chat_id, bot)
def like(bot, update, args):
message = update.message or update.callback_query.message
@ -327,6 +327,23 @@ def tweetMessage(status, cid, bot):
bot.sendMessage(chat_id = cid, text = "Tweet %i:\n%s (@%s) at %s:\n%s" % (i, status.author.name, status.author.screen_name, status.created_at, html.unescape(status.text)), reply_markup=rmu)
def search(bot, update, args):
try:
count = int(args[0])
query = ' '.join(args[1:])
except:
count = 5
query = ' '.join(args)
try:
two = bottools.methods.getTwo(update.message)
for tweet in two.api.search(q=query, rpp=count, result_type="recent")[:count]:
tweetMessage(tweet, update.message.chat_id, bot)
except tweepy.error.TweepError as e:
bottools.methods.twoExceptions(e, update.message)
def user(bot, update, args):
try:
count = int(args[1])
@ -461,6 +478,7 @@ commands = {
"reply": reply,
"restart": restart,
"retweet": retweet,
"search": search,
"self": selfTweets,
"start": start,
"thread": thread,
@ -482,6 +500,7 @@ pargs = [
quote,
reply,
retweet,
search,
selfTweets,
thread,
timeline,

View file

@ -13,8 +13,8 @@ class twObject:
def delete(self, id):
self.api.destroy_status(id)
def search(self, query, savepoint = 0):
tweets = list(tweepy.Cursor(self.api.search, q=query, since_id=savepoint, include_entities=True).items())
def search(self, query, savepoint = 0, count = None):
tweets = list(tweepy.Cursor(self.api.search, q=query, since_id=savepoint, count=count, include_entities=True).items())
tweets.reverse()
return tweets