This commit is contained in:
spline 2012-11-30 12:09:09 -05:00
parent 961f0ddf04
commit a718e30144
2 changed files with 53 additions and 45 deletions

View File

@ -1,6 +1,25 @@
Overview:
Supply a username and get the latest tweet(s). Or an ID to a tweet. Or search on twitter. Or view latest trends. Does not require a user account or apikey or anything like that. Just point and shoot.
This plugin does NOT relay tweets in real time. It only fetches data from Twitter when commands are called.
This is forked from Hoaas' Tweety plugin at: http://github.com/Hoaas/Supybot-Plugins to support Twitter API v1.1 and add in a few features:
Instructions:
1.) Install the dependencies. You can go the pip route or install via source, depending on your setup. You will need:
1. Install oauth2: sudo pip install oauth2
2.) You need some keys from Twitter. See http://dev.twitter.com. Steps are:
1. If you plan to use a dedicated Twitter account, create a new twitter account.
2. Go to dev.twitter.com and log in.
3. Click create an application.
4. Fill out the information. Name does not matter.
5. default is read-only. Since we're not tweeting from this bot/code, you're fine here.
6. Your 4 magic strings (2 tokens and 2 secrets) are shown.
7. Once you /msg yourbot load Tweety, you need to set these keys:
/msg bot config plugins.Tweety.consumer_key xxxxx
/msg bot config plugins.Tweety.consumer_secret xxxxx
/msg bot config plugins.Tweety.access_key xxxxx
/msg bot config plugins.Tweety.access_secret xxxxx
Examples:
12:38:02 <@Hoaas> !twitter cnn
@ -19,30 +38,3 @@ Examples:
12:40:05 <@Hoaas> !twitter --info TheScienceGuy
12:40:07 <@Bunisher> @TheScienceGuy (Bill Nye): http://billnye.com Science Educator seeks to change the world... 29 friends, 319377 followers. Los Angeles, CA, USA
12:42:00 <@Hoaas> !twitter --num 5 wilw
12:42:01 <@Bunisher> @wilw (Wil Wheaton): Holy crap, @amazonmp3 is doing some incredible deals until midnight PDT. Tons of great albums for a buck. (5 hours ago)
12:42:02 <@Bunisher> @wilw (Wil Wheaton): Drr…Drr…Drr. (5 hours ago)
12:42:03 <@Bunisher> @wilw (Wil Wheaton): Did I make another Robert Evans tweet, because it was amusing to me? You bet your ass I did, and I did it right there in front of everyone. (1 days ago)
12:42:04 <@Bunisher> @wilw (Wil Wheaton): (Kids, ask your parents. Then go find that weird relative who loves movies from the 70s and she'll explain it to you.) (1 days ago)
12:42:05 <@Bunisher> @wilw (Wil Wheaton): Do I write tweets that are rhetorical questions, and read them to myself in the voice of Robert Evans? You bet I do. (1 days ago)
12:42:09 <@Hoaas> !twitter --rt --num 5 wilw
12:42:10 <@Bunisher> @wilw (Wil Wheaton): Holy crap, @amazonmp3 is doing some incredible deals until midnight PDT. Tons of great albums for a buck. (5 hours ago)
12:42:12 <@Bunisher> @wilw (Wil Wheaton): Drr…Drr…Drr. (5 hours ago)
12:42:14 <@Bunisher> @wilw (Wil Wheaton): RT @The_RobertEvans: @wilw Am I a fan of Wil Wheaton? "You bet your ass I am. " (7 hours ago)
12:42:15 <@Bunisher> @wilw (Wil Wheaton): RT @amazonmp3: Hear one of the finest voices in rock on the latest Florence + the Machine album for $.99 thru midnight: http://t.co/gEXg5Tbb (5 hours ago)
12:42:16 <@Bunisher> @wilw (Wil Wheaton): RT @amazonmp3: Beach House's Bloom is super dreamy. For $.99 you don't have to take our word for it. Ends midnight Pacific: http://t.co/ ... (6 hours ago)
12:41:38 <@Hoaas> !twitter --reply wilw
12:41:40 <@Bunisher> @wilw (Wil Wheaton): @chicazul ugh. Region locking makes baby jeebus cry ... until he gets a VPN. (4 hours ago)
12:43:13 <@Hoaas> !twitter --rt --reply --num 5 wilw
12:43:14 <@Bunisher> @wilw (Wil Wheaton): @chicazul ugh. Region locking makes baby jeebus cry ... until he gets a VPN. (4 hours ago)
12:43:15 <@Bunisher> @wilw (Wil Wheaton): @amazonmp3 thanks for the rockin' sale :) (4 hours ago)
12:43:16 <@Bunisher> @wilw (Wil Wheaton): Holy crap, @amazonmp3 is doing some incredible deals until midnight PDT. Tons of great albums for a buck. (5 hours ago)
12:43:18 <@Bunisher> @wilw (Wil Wheaton): @undeux USA! USA! USA! (5 hours ago)
12:43:19 <@Bunisher> @wilw (Wil Wheaton): RT @amazonmp3: Hear one of the finest voices in rock on the latest Florence + the Machine album for $.99 thru midnight: http://t.co/gEXg5Tbb (5 hours ago)
12:40:29 <@Hoaas> !tagdef ff
12:40:31 <@Bunisher> Tagdef: #ff #ff is the same as (short for) #followfriday. http://tagdef.com/ff

View File

@ -46,8 +46,10 @@ from datetime import tzinfo, datetime, timedelta
# for unescape
import re, htmlentitydefs
# reencode
import unicodedata
# oauthtwitter
import time
import urlparse
import oauth2 as oauth
@ -101,7 +103,7 @@ class OAuthApi:
http_method=http_method)
# Get a url opener that can handle Oauth basic auth
callbacks.log.info(str(extra_params))
#callbacks.log.info(str(extra_params))
opener = self._GetOpener()
if http_method == "POST":
@ -113,7 +115,7 @@ class OAuthApi:
url = req.to_url()
encoded_post_data = ""
callbacks.log.info(str(url))
#callbacks.log.info(str(url))
if encoded_post_data:
url_data = opener.open(url, encoded_post_data).read()
@ -254,6 +256,7 @@ class Tweety(callbacks.Plugin):
self.__parent.__init__(irc)
haveAuthKeys = self._checkCredentials()
def _checkCredentials(self):
"""Check for all 4 requires keys on Twitter auth."""
failTest = False
@ -261,16 +264,33 @@ class Tweety(callbacks.Plugin):
try:
testKey = self.registryValue(checkKey)
except:
self.log.debug("Failed checking keys. We're missing the config value for: {0}. Please set this and try again.".format(checkKey))
failTest = True
break
if failTest:
self.log.error('Failed getting keys')
self.log.error('Failed getting keys. You must set all 4 keys in config variables.')
return False
else:
self.log.info('Passed getting keys')
return True
def _expandLinks(self, tweet):
# if not surl.startswith('http://') and not surl.startswith('https://'):
_tco_link_re = re.compile(u'http://t.co/[a-zA-Z0-9]+')
try:
req_url = 'http://api.longurl.org/v2/expand?format=json&url=' + qurl
req = urllib2.Request(req_url, headers={'User-Agent': 'Python-longurl/1.0'})
lookup = json.loads(urllib2.urlopen(req).read())
return lookup.get('long-url', None)
except urllib2.HTTPError as e:
self.log.debug('http error {0} when trying to shorten {1}'.format(e, qurl)
return None
except urllib2.URLError as e:
self.log.debug('http error {0} when trying to shorten {1}'.format(e, qurl)
return None
#def re_encode(input_string, decoder = 'utf-8', encoder = 'utf=8'):
#try:
#output_string = unicodedata.normalize('NFD',\
@ -279,6 +299,7 @@ class Tweety(callbacks.Plugin):
#output_string = unicodedata.normalize('NFD',\
#input_string.decode('ascii', 'replace')).encode(encoder)
#return output_string
def _unescape(self, text):
"""Created by Fredrik Lundh (http://effbot.org/zone/re-sub.htm#unescape-html)"""
@ -391,7 +412,6 @@ class Tweety(callbacks.Plugin):
"""
query = "select * from geo.places where text='%s'" % lookup
params = {
"q": query,
"format":"json",
@ -412,7 +432,10 @@ class Tweety(callbacks.Plugin):
return None
return woeid
##########################
### PUBLIC FUNCTIONS #####
##########################
def woeidlookup(self, irc, msg, args, lookup):
"""[location]
@ -495,12 +518,13 @@ class Tweety(callbacks.Plugin):
except:
irc.reply("Failed to lookup trends data. Something might have gone wrong.")
return
#self.log.info(str(type(data)))
try:
location = data[0]['locations'][0]['name']
except:
irc.reply("ERROR: Cannot load trends: {0}".format(data)) # error also throws 404.
self.log.info("Trends error data: {0}".format(data))
return
ttrends = string.join([trend['name'].encode('utf-8') for trend in data[0]['trends']], " | ")
@ -631,15 +655,7 @@ class Tweety(callbacks.Plugin):
except:
irc.reply("Failed to get user's timeline. Twitter broken?")
return
# final sanity check for json
try:
data = json.loads(data)
except:
irc.reply("ERROR: Failed to parse data from Twitter: {0}".format(data))
self.log.error(str(data))
return
# process the data.
if args['id']: # If --id was given for a single tweet.
text = self._unescape(data.get('text', None))