Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
* dev:
  open.spotify.com url handler
  Renaming plugin.CLASS_NAME to plugin.ENTRY_CLASS
  • Loading branch information
twexler committed Feb 27, 2014
2 parents bea54c5 + f8a274a commit 73403cb
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 8 deletions.
10 changes: 10 additions & 0 deletions diatribe/app/templates/channel.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ <h3 class="panel-title"><a href="{{ url['url'].decode('UTF-8') }}">{{ url['title
frameborder="0"></iframe>
</div>
</div>
{% elif url['type'] == "spotify" %}
<div class="panel-heading">
<h3 class="panel-title">Spotify: <a href="http://open.spotify.com/{{ url['spotify_type'] }}/{{ url['spotify_id'] }}">{{ "%s - %s" % (url['artists'], url['title']) }}</a></h3>
</div>
{{ url['source'].decode('UTF-8') }}<br/><br/>
<div class='row' style="margin-left:1em;">
<div class='col-md-6'>
<iframe src="https://embed.spotify.com/?uri=spotify:{{ url['spotify_type'] }}:{{ url['spotify_id'] }}" width="300" height="380" frameborder="0" allowtransparency="true"></iframe>
</div>
</div>
{% elif url['type'] == "image" %}
<div class="panel-heading">
<h3 class="panel-title"><a href="{{ url['url'].decode('UTF-8') }}">{{ url['title'].decode('UTF-8') }}</a></h3>
Expand Down
4 changes: 2 additions & 2 deletions diatribe/bot/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ def load_plugins(self):
continue
self.plugins.update({name: {'class': plugin}})
try:
klass = getattr(plugin, plugin.CLASS_NAME)
klass = getattr(plugin, plugin.ENTRY_CLASS)
except AttributeError:
logging.error('Unable to load plugin %s, CLASS_NAME undefined' % name)
logging.error('Unable to load plugin %s, ENTRY_CLASS undefined' % name)
continue
try:
klass(self)
Expand Down
2 changes: 1 addition & 1 deletion diatribe/bot/plugins/_twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from twitter import Twitter as twitter_api, OAuth as twitter_oauth
from twitter.api import TwitterHTTPError

CLASS_NAME = "TwitterPlugin"
ENTRY_CLASS = "TwitterPlugin"
SHORT_HELP = "%(trigger)st, %(trigger)stwitter <user>: outputs \
the most recent tweet from the user specified"

Expand Down
2 changes: 1 addition & 1 deletion diatribe/bot/plugins/google.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import requests

CLASS_NAME = "GooglePlugin"
ENTRY_CLASS = "GooglePlugin"
SHORT_HELP = "%(trigger)sg, %(trigger)sgoogle <query>: \
returns the definition of <query> from google.com"

Expand Down
2 changes: 1 addition & 1 deletion diatribe/bot/plugins/imdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import requests

CLASS_NAME = "IMDBPlugin"
ENTRY_CLASS = "IMDBPlugin"
SHORT_HELP = "%(trigger)si, %(trigger)simdb, %(trigger)stv. %(trigger)smovie <query>\
<query>: returns the definition of <query> from imdb.com, see \
%(trigger)shelp imdb for more help"
Expand Down
2 changes: 1 addition & 1 deletion diatribe/bot/plugins/urbandictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import requests

CLASS_NAME = "UrbanDictionaryPlugin"
ENTRY_CLASS = "UrbanDictionaryPlugin"
SHORT_HELP = "%(trigger)sud, %(trigger)surbandictionary <query>: returns \
the definition of <query> from urbandictionary.com"

Expand Down
52 changes: 51 additions & 1 deletion diatribe/bot/plugins/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,19 @@
from twitter import Twitter as twitter_api, OAuth as twitter_oauth
from twitter.api import TwitterHTTPError

CLASS_NAME = "URLPlugin"
ENTRY_CLASS = "URLPlugin"

YOUTUBE_API_URL = "https://www.googleapis.com/youtube/v3/videos?part=snippet,contentDetails&id=%(id)s&key=%(apikey)s"
SPOTIFY_API_URL = "http://ws.spotify.com/lookup/1/.json?uri=spotify:%(type)s:%(id)s"


class URLPlugin():

def __init__(self, bot):
bot.rule_map.add(Rule('/<url("youtube.com"):url>',
endpoint=self.handle_youtube))
bot.rule_map.add(Rule('/<url("open.spotify.com"):url>',
endpoint=self.handle_spotify))
bot.rule_map.add(Rule('/<url("twitter.com"):url>',
endpoint=self.handle_twitter))
bot.rule_map.add(Rule('/<url:url>',
Expand Down Expand Up @@ -136,6 +139,53 @@ def handle_twitter(self, channel, nick, msg, args):
self.bot.msg(channel, formatted_msg)
self.log_url(url_id, url_obj, channel)

def handle_spotify(self, channel, nick, msg, args):
url = args['url']
path_parts = url.path.split('/')
spotify_type = path_parts[1]
spotify_id = path_parts[2]
if spotify_type != "track":
#only handle tracks for now
self.handle_url(channel, nick, msg,
args={'url': url.geturl()})
return
url_args = {
'type': spotify_type,
'id': spotify_id
}
try:
r = requests.get(SPOTIFY_API_URL % url_args)
except requests.exceptions.HTTPError:
# in case we can't contact the spotify api, log anyway
self.handle_url(channel, nick, msg,
args={'url': url.geturl()})
return
try:
resp = r.json()[spotify_type]
except KeyError:
self.handle_url(channel, nick, msg,
args={'url': url.geturl()})
return
url_obj = {}
url_obj['type'] = 'spotify'
artists = ', '.join([artist['name'] for artist in resp['artists']])
url_obj['artists'] = artists.encode('UTF-8')
url_obj['title'] = resp['name'].encode('UTF-8')
url_obj['album'] = resp['album']['name'].encode('UTF-8')
url_obj['released'] = resp['album']['released'].encode('UTF-8')
url_obj['spotify_id'] = spotify_id
url_obj['spotify_type'] = spotify_type
url_obj['source'] = "<%s> %s" % (nick, msg)
url_obj['ts'] = time.time()
track_name = "%s - %s" % (url_obj['artists'], url_obj['title'])
formatted_msg = assembleFormattedText(A.bold[track_name])
formatted_msg += assembleFormattedText(A.normal[' From: '])
formatted_msg += assembleFormattedText(A.bold[url_obj['album']])
formatted_msg += assembleFormattedText(A.normal[' (%s)' % url_obj['released']])
url_id = hashlib.sha1(url.geturl()).hexdigest()[:9]
self.bot.msg(channel, formatted_msg)
self.log_url(url_id, url_obj, channel)

def log_url(self, url_id, url_obj, channel):
key = "%s.%s.%s" % (self.bot.host_id,
self.bot.channels[channel]['id'], url_id)
Expand Down
2 changes: 1 addition & 1 deletion diatribe/bot/plugins/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from twisted.words.protocols.irc import assembleFormattedText, attributes as A

CLASS_NAME = "WeatherPlugin"
ENTRY_CLASS = "WeatherPlugin"
SHORT_HELP = "%(trigger)sw, %(trigger)sweather <location>: outputs \
the most recent weather from the location specified"

Expand Down

0 comments on commit 73403cb

Please sign in to comment.