Tweety: tsearch --new, better location lookups

This commit is contained in:
oddluck 2020-03-15 00:38:19 +00:00
parent e24f02cbc0
commit 51fad0e83b
2 changed files with 30 additions and 16 deletions

View File

@ -47,6 +47,7 @@ from supybot.commands import *
import supybot.plugins as plugins import supybot.plugins as plugins
import supybot.ircutils as ircutils import supybot.ircutils as ircutils
import supybot.callbacks as callbacks import supybot.callbacks as callbacks
import supybot.log as log
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
import os import os
@ -277,7 +278,7 @@ class Tweety(callbacks.Plugin):
else: # bold otherwise. else: # bold otherwise.
ret = "@{0}".format(self._bu(nick)) ret = "@{0}".format(self._bu(nick))
if verified: if verified:
string = self._bold(ircutils.mircColor("", 'white', 'blue')) string = self._bold(ircutils.mircColor("", 'white', 'blue'))
ret += "{}".format(string) ret += "{}".format(string)
# show real name in tweet output? # show real name in tweet output?
if not self.registryValue('hideRealName', msg.args[0]): if not self.registryValue('hideRealName', msg.args[0]):
@ -319,10 +320,14 @@ class Tweety(callbacks.Plugin):
"""<location> """<location>
Use Yahoo's API to look-up a WOEID. Use Yahoo's API to look-up a WOEID.
""" """
woeidlist = open('{0}/woeidList.json'.format(os.path.dirname(os.path.abspath(__file__)))) try:
data = json.loads(woeidlist.read()) data = self.twitterApi.ApiCall('trends/available')
data = json.loads(data.read())
except:
data = None
log.debug('Tweety: error retrieving data from Trends API')
if not data: if not data:
irc.reply("ERROR opening woeidList.json") log.debug("No location results for {0}".format(lookup))
return return
return next((item["woeid"] for item in data if lookup.lower() in item["name"].lower()), None) return next((item["woeid"] for item in data if lookup.lower() in item["name"].lower()), None)
@ -446,7 +451,7 @@ class Tweety(callbacks.Plugin):
trends = wrap(trends, [getopts({'exclude':''}), optional('text')]) trends = wrap(trends, [getopts({'exclude':''}), optional('text')])
def tsearch(self, irc, msg, args, optlist, optterm): def tsearch(self, irc, msg, args, optlist, optterm):
"""[--num number] [--searchtype mixed,recent,popular] [--lang xx] <term> """[--num number] [--searchtype mixed,recent,popular] [--lang xx] [--new] <term>
Searches Twitter for the <term> and returns the most recent results. Searches Twitter for the <term> and returns the most recent results.
--num is number of results. (1-10) --num is number of results. (1-10)
@ -466,6 +471,9 @@ class Tweety(callbacks.Plugin):
irc.reply("ERROR: Twitter is not authorized. Please check logs before running this command.") irc.reply("ERROR: Twitter is not authorized. Please check logs before running this command.")
return return
self.since_id.setdefault(msg.channel, {})
self.since_id[msg.channel].setdefault('{0}'.format(optterm), None)
new = False
# default arguments. # default arguments.
tsearchArgs = {'include_entities':'false', tsearchArgs = {'include_entities':'false',
'tweet_mode': 'extended', 'tweet_mode': 'extended',
@ -486,6 +494,9 @@ class Tweety(callbacks.Plugin):
tsearchArgs['result_type'] = value # limited by getopts to valid values. tsearchArgs['result_type'] = value # limited by getopts to valid values.
if key == 'lang': # lang . Uses ISO-639 codes like 'en' http://en.wikipedia.org/wiki/ISO_639-1 if key == 'lang': # lang . Uses ISO-639 codes like 'en' http://en.wikipedia.org/wiki/ISO_639-1
tsearchArgs['lang'] = value tsearchArgs['lang'] = value
if key == 'new' and self.since_id[msg.channel]['{0}'.format(optterm)]:
new = True
tsearchArgs['since_id'] = self.since_id[msg.channel]['{0}'.format(optterm)]
# now build our API call. # now build our API call.
data = self.twitterApi.ApiCall('search/tweets', parameters=tsearchArgs) data = self.twitterApi.ApiCall('search/tweets', parameters=tsearchArgs)
try: try:
@ -496,9 +507,11 @@ class Tweety(callbacks.Plugin):
# check the return data. # check the return data.
results = data.get('statuses') # data returned as a dict. results = data.get('statuses') # data returned as a dict.
if not results or len(results) == 0: # found nothing or length 0. if not results or len(results) == 0: # found nothing or length 0.
if not new:
irc.reply("ERROR: No Twitter Search results found for '{0}'".format(optterm)) irc.reply("ERROR: No Twitter Search results found for '{0}'".format(optterm))
return return
else: # we found something. else: # we found something.
self.since_id[msg.channel]['{0}'.format(optterm)] = results[0].get('id')
for result in results[0:int(tsearchArgs['count'])]: # iterate over each. for result in results[0:int(tsearchArgs['count'])]: # iterate over each.
nick = self._unescape(result['user'].get('screen_name')) nick = self._unescape(result['user'].get('screen_name'))
name = self._unescape(result["user"].get('name')) name = self._unescape(result["user"].get('name'))
@ -512,7 +525,8 @@ class Tweety(callbacks.Plugin):
tsearch = wrap(tsearch, [getopts({'num':('int'), tsearch = wrap(tsearch, [getopts({'num':('int'),
'searchtype':('literal', ('popular', 'mixed', 'recent')), 'searchtype':('literal', ('popular', 'mixed', 'recent')),
'lang':('somethingWithoutSpaces')}), 'lang':('somethingWithoutSpaces'),
'new':''}),
('text')]) ('text')])
def twitter(self, irc, msg, args, optlist, optnick, opturl): def twitter(self, irc, msg, args, optlist, optnick, opturl):
@ -526,7 +540,8 @@ class Tweety(callbacks.Plugin):
Ex: --info @cnn OR --id 337197009729622016 OR --number 3 @drudge Ex: --info @cnn OR --id 337197009729622016 OR --number 3 @drudge
""" """
self.since_id.setdefault('{0}-{1}'.format(optnick, msg.args[0]), None) self.since_id.setdefault(msg.channel, {})
self.since_id[msg.channel].setdefault('{0}'.format(optnick), None)
# enforce +voice or above to use command? # enforce +voice or above to use command?
if self.registryValue('requireVoiceOrAbove', msg.args[0]): # should we check? if self.registryValue('requireVoiceOrAbove', msg.args[0]): # should we check?
if ircutils.isChannel(msg.args[0]): # are we in a channel? if ircutils.isChannel(msg.args[0]): # are we in a channel?
@ -580,8 +595,8 @@ class Tweety(callbacks.Plugin):
twitterArgs = {'screen_name': optnick, 'include_entities':'false'} twitterArgs = {'screen_name': optnick, 'include_entities':'false'}
elif args['new']: # --new. elif args['new']: # --new.
apiUrl = 'statuses/user_timeline' apiUrl = 'statuses/user_timeline'
if self.since_id['{0}-{1}'.format(optnick, msg.args[0])]: if self.since_id[msg.channel]['{0}'.format(optnick)]:
twitterArgs = {'screen_name': optnick, 'since_id':self.since_id['{0}-{1}'.format(optnick, msg.args[0])], 'count': args['num'], 'tweet_mode': 'extended'} twitterArgs = {'screen_name': optnick, 'since_id':self.since_id[msg.channel]['{0}'.format(optnick)], 'count': args['num'], 'tweet_mode': 'extended'}
if args['nort']: # show retweets? if args['nort']: # show retweets?
twitterArgs['include_rts'] = 'false' twitterArgs['include_rts'] = 'false'
else: # default is to show retweets. else: # default is to show retweets.
@ -687,7 +702,7 @@ class Tweety(callbacks.Plugin):
else: else:
irc.reply("ERROR: '{0}' has not tweeted yet.".format(optnick)) irc.reply("ERROR: '{0}' has not tweeted yet.".format(optnick))
return return
self.since_id['{0}-{1}'.format(optnick, msg.args[0])] = data[0].get('id') self.since_id[msg.channel]['{0}'.format(optnick)] = data[0].get('id')
for tweet in data: # n+1 tweets found. iterate through each tweet. for tweet in data: # n+1 tweets found. iterate through each tweet.
text = self._unescape(tweet.get('full_text')) or self._unescape(tweet.get('text')) text = self._unescape(tweet.get('full_text')) or self._unescape(tweet.get('text'))
nick = self._unescape(tweet["user"].get('screen_name')) nick = self._unescape(tweet["user"].get('screen_name'))

File diff suppressed because one or more lines are too long