]>
jfr.im git - erebus.git/blob - modules/urls.py
1 # Erebus IRC bot - Author: Erebus Team
3 # This file is released into the public domain; see http://unlicense.org/
7 'author': 'Erebus Team',
8 'license': 'public domain',
9 'compatible': [1], # compatible module API versions
10 'depends': [], # other modules required to work properly?
15 lib
= modlib
.modlib(__name__
)
16 modstart
= lib
.modstart
20 import re
, json
, urllib2
, urlparse
, HTMLParser
21 from BeautifulSoup
import BeautifulSoup
23 hostmask_regex
= re
.compile(r
'^(.*)!(.*)@(.*)$')
26 re
.compile(r
'spotify:(?P<type>\w+):(?P<track_id>\w{22})'),
27 re
.compile(r
'http://open.spotify.com/(?P<type>\w+)/(?P<track_id>\w{22})')
30 re
.compile(r
'https?://(?:www\.)?youtube\.com/watch\?[a-zA-Z0-9=&_\-]+'),
33 re
.compile('(http|ftp|https):\/\/([\w\-_]+(?:(?:\.[\w\-_]+)+))([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?'), #TODO
36 def parser_hostmask(hostmask
):
37 if isinstance(hostmask
, dict):
44 if hostmask
is not None:
45 match
= hostmask_regex
.match(hostmask
)
60 @lib.hooknum("PRIVMSG")
61 def privmsg_hook(bot
, line
):
62 sender
= parser_hostmask(line
[1:line
.find(' ')])
65 linetx
= line
.split(None, 3)[3][1:]
69 chan
= line
.split()[2]
71 if 'open.spotify.com' in line
or 'spotify:' in line
:
72 for r
in spotify_regex
:
73 for sptype
, track
in r
.findall(linetx
):
74 bot
.msg(chan
, gotspotify(sptype
, track
))
76 elif 'youtube.com' in line
or 'youtu.be' in line
:
78 for r
in youtube_regex
:
79 for url
in r
.findall(linetx
):
80 bot
.msg(chan
, gotyoutube(url
))
82 elif 'twitch.tv' in line
: pass #TODO fix twitch
84 else: pass #TODO generic <title> checker
87 def gotspotify(type, track
):
88 url
= 'http://ws.spotify.com/lookup/1/?uri=spotify:%s:%s' % (type, track
)
89 xml
= urllib2
.urlopen(url
).read()
90 soup
= BeautifulSoup(xml
)
91 lookup_type
= soup
.contents
[2].name
93 if lookup_type
== 'track':
94 name
= soup
.find('name').string
95 album_name
= soup
.find('album').find('name').string
96 artist_name
= soup
.find('artist').find('name').string
97 popularity
= soup
.find('popularity')
99 popularity
= float(popularity
.string
)*100
100 length
= float(soup
.find('length').string
)
101 minutes
= int(length
)/60
102 seconds
= int(length
)%60
104 return 'Track: %s - %s / %s %s:%.2d %2d%%' % (artist_name
, name
, album_name
, minutes
, seconds
, popularity
)
106 elif lookup_type
== 'album':
107 album_name
= soup
.find('album').find('name').string
108 artist_name
= soup
.find('artist').find('name').string
109 released
= soup
.find('released').string
110 return 'Album: %s - %s - %s' % (artist_name
, album_name
, released
)
113 return 'Unsupported type.'
116 url_data
= urlparse
.urlparse(url
)
117 query
= urlparse
.parse_qs(url_data
.query
)
118 video
= query
["v"][0]
119 api_url
= 'http://gdata.youtube.com/feeds/api/videos/%s?alt=json&v=2' % video
121 respdata
= urllib2
.urlopen(api_url
).read()
122 video_info
= json
.loads(respdata
)
124 title
= video_info
['entry']['title']["$t"]
125 author
= video_info
['entry']['author'][0]['name']['$t']
127 return "Youtube: %s (%s)" % (title
, author
)
136 linetx
= line
.split(None, 3)[3][1:]
140 if checkfor
not in line
:
141 return # doesn't concern us
143 for p
, h
, c
in url_regex
.findall(linetx
):
145 url
= 'http://api.justin.tv/api/stream/list.json?channel=%s' % c
[1:]
146 respdata
= urllib2
.urlopen(url
).read()
147 twitch
= json
.loads(respdata
)
149 bot
.msg(line
.split()[2], 'Twitch: %s (%s playing %s)' % (twitch
[0]['channel']['status'], twitch
[0]['channel']['login'], twitch
[0]['channel']['meta_game']))
151 bot
.msg(line
.split()[2], 'Twitch: Channel offline.')