2013-01-31 17:16:22 +01:00
import re
from util import hook , http
2013-07-15 16:46:41 +02:00
from urllib import urlencode
2013-01-31 17:16:22 +01:00
2013-07-15 16:57:24 +02:00
gateway = ' http://open.spotify.com/ {} / {} ' # http spotify gw address
spuri = ' spotify: {} : {} '
2013-02-06 07:53:08 +01:00
spotify_re = ( r ' (spotify:(track|album|artist|user):([a-zA-Z0-9]+)) ' , re . I )
http_re = ( r ' (open \ .spotify \ .com \ /(track|album|artist|user) \ / '
2013-07-15 16:57:24 +02:00
' ([a-zA-Z0-9]+)) ' , re . I )
2013-06-28 11:25:06 +02:00
2013-07-15 16:46:41 +02:00
def sptfy ( inp ) :
2013-07-15 16:57:24 +02:00
# try:
# login = http.get("http://sptfy.com/login.php", post_data=urlencode({'loginUsername': 'xDCloudBot', 'loginPassword': 'rjgw67kf', 'submit': '', 'submitme': '1'}), cookies=True)
# except Exception as e:
# return inp
2013-07-15 16:46:41 +02:00
shortenurl = " http://sptfy.com/index.php "
2013-07-15 16:57:24 +02:00
data = urlencode ( { ' longUrl ' : inp , ' shortUrlDomain ' : 1 , ' submitted ' : 1 , " shortUrlFolder " : 6 , " customUrl " : " " , " shortUrlPassword " : " " , " shortUrlExpiryDate " : " " , " shortUrlUses " : 0 , " shortUrlType " : 0 } )
try :
soup = http . get_soup ( shortenurl , post_data = data , cookies = True )
except :
return inp
2013-07-15 16:46:41 +02:00
try :
link = soup . find ( ' div ' , { ' class ' : ' resultLink ' } ) . text . strip ( )
return link
except :
message = " Unable to shorten URL: %s " % soup . find ( ' div ' , { ' class ' : ' messagebox_text ' } ) . find ( ' p ' ) . text . split ( " <br/> " ) [ 0 ]
return message
2013-01-31 17:16:22 +01:00
2013-05-18 15:27:12 +02:00
@hook.command ( ' sptrack ' )
2013-02-06 15:24:30 +01:00
@hook.command
def spotify ( inp ) :
" spotify <song> -- Search Spotify for <song> "
2013-07-15 16:57:24 +02:00
try :
data = http . get_json ( " http://ws.spotify.com/search/1/track.json " , q = inp . strip ( ) )
except Exception as e :
return " Could not get track information: {} " . format ( e )
2013-02-06 23:54:11 +01:00
try :
2013-05-09 20:22:16 +02:00
type , id = data [ " tracks " ] [ 0 ] [ " href " ] . split ( " : " ) [ 1 : ]
2013-02-06 23:54:11 +01:00
except IndexError :
return " Could not find track. "
2013-07-15 16:46:41 +02:00
url = sptfy ( gateway . format ( type , id ) )
return u " \x02 {} \x02 by \x02 {} \x02 - \x02 {} \x02 " . format ( data [ " tracks " ] [ 0 ] [ " name " ] , data [ " tracks " ] [ 0 ] [ " artists " ] [ 0 ] [ " name " ] , url )
2013-06-28 11:25:06 +02:00
2013-05-18 15:27:12 +02:00
@hook.command
def spalbum ( inp ) :
" spalbum <album> -- Search Spotify for <album> "
2013-07-15 16:57:24 +02:00
try :
data = http . get_json ( " http://ws.spotify.com/search/1/album.json " , q = inp . strip ( ) )
except Exception as e :
return " Could not get album information: {} " . format ( e )
2013-05-18 15:27:12 +02:00
try :
type , id = data [ " albums " ] [ 0 ] [ " href " ] . split ( " : " ) [ 1 : ]
except IndexError :
return " Could not find album. "
2013-07-15 16:46:41 +02:00
url = sptfy ( gateway . format ( type , id ) )
return u " \x02 {} \x02 by \x02 {} \x02 - \x02 {} \x02 " . format ( data [ " albums " ] [ 0 ] [ " name " ] , data [ " albums " ] [ 0 ] [ " artists " ] [ 0 ] [ " name " ] , url )
2013-06-28 11:25:06 +02:00
2013-05-18 15:27:12 +02:00
@hook.command
def spartist ( inp ) :
" spartist <artist> -- Search Spotify for <artist> "
2013-07-15 16:57:24 +02:00
try :
data = http . get_json ( " http://ws.spotify.com/search/1/artist.json " , q = inp . strip ( ) )
except Exception as e :
return " Could not get artist information: {} " . format ( e )
2013-05-18 15:27:12 +02:00
try :
type , id = data [ " artists " ] [ 0 ] [ " href " ] . split ( " : " ) [ 1 : ]
except IndexError :
return " Could not find artist. "
2013-07-15 16:46:41 +02:00
url = sptfy ( gateway . format ( type , id ) )
return u " \x02 {} \x02 - \x02 {} \x02 " . format ( data [ " artists " ] [ 0 ] [ " name " ] , url )
2013-06-28 11:25:06 +02:00
2013-02-06 07:53:08 +01:00
@hook.regex ( * http_re )
2013-01-31 17:16:22 +01:00
@hook.regex ( * spotify_re )
def spotify_url ( match ) :
2013-02-06 07:53:08 +01:00
type = match . group ( 2 )
2013-02-06 07:16:13 +01:00
spotify_id = match . group ( 3 )
2013-05-09 20:22:16 +02:00
url = spuri . format ( type , spotify_id )
2013-07-15 16:57:24 +02:00
# no error catching here, if the API is down fail silently
2013-05-09 20:22:16 +02:00
data = http . get_json ( " http://ws.spotify.com/lookup/1/.json " , uri = url )
2013-02-06 07:53:08 +01:00
if type == " track " :
2013-05-09 20:22:16 +02:00
name = data [ " track " ] [ " name " ]
artist = data [ " track " ] [ " artists " ] [ 0 ] [ " name " ]
album = data [ " track " ] [ " album " ] [ " name " ]
2013-07-15 16:46:41 +02:00
return u " Spotify Track: \x02 {} \x02 by \x02 {} \x02 from the album \x02 {} \x02 - \x02 {} \x02 " . format ( name , artist , album , sptfy ( gateway . format ( type , spotify_id ) ) )
2013-02-06 07:53:08 +01:00
elif type == " artist " :
2013-07-15 16:46:41 +02:00
return u " Spotify Artist: \x02 {} \x02 - \x02 {} \x02 " . format ( data [ " artist " ] [ " name " ] , sptfy ( gateway . format ( type , spotify_id ) ) )
2013-02-06 07:53:08 +01:00
elif type == " album " :
2013-07-15 16:46:41 +02:00
return u " Spotify Album: \x02 {} \x02 - \x02 {} \x02 - \x02 {} \x02 " . format ( data [ " album " ] [ " artist " ] , data [ " album " ] [ " name " ] , sptfy ( gateway . format ( type , spotify_id ) ) )