]>
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',
14 # http://embed.ly/tools/generator
18 lib
= modlib
.modlib(__name__
)
19 modstart
= lib
.modstart
24 if sys
.version_info
.major
< 3:
28 from BeautifulSoup
import BeautifulSoup
30 import urllib
.request
as urllib2
31 import urllib
.parse
as urlparse
32 import html
.parser
as HTMLParser
33 from bs4
import BeautifulSoup
37 html_parser
= HTMLParser
.HTMLParser()
39 hostmask_regex
= re
.compile(r
'^(.*)!(.*)@(.*)$')
40 url_regex
= re
.compile(r
'https?://[^/\s]+\.[^/\s]+(?:/\S+)?')
42 re
.compile(r
'spotify:(?P<type>\w+):(?P<track_id>\w{22})'),
43 re
.compile(r
'https?://open.spotify.com/(?P<type>\w+)/(?P<track_id>\w+)')
46 re
.compile(r
'https?://(?:www\.)?youtube\.com/watch\?[a-zA-Z0-9=&_\-]+'),
49 re
.compile(r
'https?:\/\/(?:www\.)?twitch.tv\/([A-Za-z0-9]*)'),
52 def parser_hostmask(hostmask
):
53 if isinstance(hostmask
, dict):
60 if hostmask
is not None:
61 match
= hostmask_regex
.match(hostmask
)
76 class SmartRedirectHandler(urllib2
.HTTPRedirectHandler
):
77 def http_error_301(self
, req
, fp
, code
, msg
, headers
):
78 result
= urllib2
.HTTPRedirectHandler
.http_error_301(
79 self
, req
, fp
, code
, msg
, headers
)
83 def http_error_302(self
, req
, fp
, code
, msg
, headers
):
84 result
= urllib2
.HTTPRedirectHandler
.http_error_302(
85 self
, req
, fp
, code
, msg
, headers
)
89 @lib.hooknum("PRIVMSG")
90 def privmsg_hook(bot
, textline
):
91 user
= parser_hostmask(textline
[1:textline
.find(' ')])
92 chan
= textline
.split()[2]
95 line
= textline
.split(None, 3)[3][1:]
99 for match
in url_regex
.findall(line
):
101 response
= goturl(match
)
102 if response
is not None:
103 bot
.msg(chan
, response
)
106 return html_parser
.unescape(line
)
108 def gotspotify(type, track
):
109 url
= 'http://ws.spotify.com/lookup/1/?uri=spotify:%s:%s' % (type, track
)
110 xml
= urllib2
.urlopen(url
).read()
111 soup
= BeautifulSoup(xml
, convertEntities
=BeautifulSoup
.HTML_ENTITIES
)
112 lookup_type
= soup
.contents
[2].name
114 if lookup_type
== 'track':
115 name
= soup
.find('name').string
116 album_name
= soup
.find('album').find('name').string
117 artist_name
= soup
.find('artist').find('name').string
118 popularity
= soup
.find('popularity')
120 popularity
= float(popularity
.string
)*100
121 length
= float(soup
.find('length').string
)
122 minutes
= int(length
)/60
123 seconds
= int(length
)%60
125 return unescape('Track: %s - %s / %s %s:%.2d %2d%%' % (artist_name
, name
, album_name
, minutes
, seconds
, popularity
))
127 elif lookup_type
== 'album':
128 album_name
= soup
.find('album').find('name').string
129 artist_name
= soup
.find('artist').find('name').string
130 released
= soup
.find('released').string
131 return unescape('Album: %s - %s - %s' % (artist_name
, album_name
, released
))
134 return 'Unsupported type.'
137 url_data
= urlparse
.urlparse(url
)
138 query
= urlparse
.parse_qs(url_data
.query
)
139 video
= query
["v"][0]
140 api_url
= 'http://gdata.youtube.com/feeds/api/videos/%s?alt=json&v=2' % video
142 respdata
= urllib2
.urlopen(api_url
).read()
143 video_info
= json
.loads(respdata
)
145 title
= video_info
['entry']['title']["$t"]
146 author
= video_info
['entry']['author'][0]['name']['$t']
148 return unescape("Youtube: %s (%s)" % (title
, author
))
153 url
= 'http://api.justin.tv/api/stream/list.json?channel=%s' % uri
.split('/')[0]
154 respdata
= urllib2
.urlopen(url
).read()
155 twitch
= json
.loads(respdata
)
157 return unescape('Twitch: %s (%s playing %s)' % (twitch
[0]['channel']['status'], twitch
[0]['channel']['login'], twitch
[0]['channel']['meta_game']))
159 return 'Twitch: Channel offline.'
162 request
= urllib2
.Request(url
)
163 opener
= urllib2
.build_opener(SmartRedirectHandler())
165 soup
= BeautifulSoup(opener
.open(request
, timeout
=2))
166 return unescape('Title: %s' % (soup
.title
.string
))