]>
jfr.im git - erebus.git/blob - modules/urls.py
1 # Erebus IRC bot - Author: Erebus Team
2 # vim: fileencoding=utf-8
4 # This file is released into the public domain; see http://unlicense.org/
8 'author': 'Erebus Team',
9 'license': 'public domain',
15 # http://embed.ly/tools/generator
19 lib
= modlib
.modlib(__name__
)
20 modstart
= lib
.modstart
25 if sys
.version_info
.major
< 3:
29 from BeautifulSoup
import BeautifulSoup
31 import urllib
.request
as urllib2
32 import urllib
.parse
as urlparse
33 import html
.parser
as HTMLParser
34 from bs4
import BeautifulSoup
38 html_parser
= HTMLParser
.HTMLParser()
40 hostmask_regex
= re
.compile(r
'^(.*)!(.*)@(.*)$')
41 url_regex
= re
.compile(r
'https?://[^/\s]+\.[^/\s]+(?:/\S+)?')
43 re
.compile(r
'spotify:(?P<type>\w+):(?P<track_id>\w{22})'),
44 re
.compile(r
'https?://open.spotify.com/(?P<type>\w+)/(?P<track_id>\w+)')
47 re
.compile(r
'https?://(?:www\.)?youtube\.com/watch\?[a-zA-Z0-9=&_\-]+'),
50 re
.compile(r
'https?:\/\/(?:www\.)?twitch.tv\/([A-Za-z0-9]*)'),
53 def parser_hostmask(hostmask
):
54 if isinstance(hostmask
, dict):
61 if hostmask
is not None:
62 match
= hostmask_regex
.match(hostmask
)
77 class SmartRedirectHandler(urllib2
.HTTPRedirectHandler
):
78 def http_error_301(self
, req
, fp
, code
, msg
, headers
):
79 result
= urllib2
.HTTPRedirectHandler
.http_error_301(
80 self
, req
, fp
, code
, msg
, headers
)
84 def http_error_302(self
, req
, fp
, code
, msg
, headers
):
85 result
= urllib2
.HTTPRedirectHandler
.http_error_302(
86 self
, req
, fp
, code
, msg
, headers
)
90 @lib.hooknum("PRIVMSG")
91 def privmsg_hook(bot
, textline
):
92 user
= parser_hostmask(textline
[1:textline
.find(' ')])
93 chan
= textline
.split()[2]
96 line
= textline
.split(None, 3)[3][1:]
100 for match
in url_regex
.findall(line
):
102 response
= goturl(match
)
103 if response
is not None:
104 bot
.msg(chan
, response
)
107 return html_parser
.unescape(line
)
109 def gotspotify(type, track
):
110 url
= 'http://ws.spotify.com/lookup/1/?uri=spotify:%s:%s' % (type, track
)
111 xml
= urllib2
.urlopen(url
).read()
112 soup
= BeautifulSoup(xml
, convertEntities
=BeautifulSoup
.HTML_ENTITIES
)
113 lookup_type
= soup
.contents
[2].name
115 if lookup_type
== 'track':
116 name
= soup
.find('name').string
117 album_name
= soup
.find('album').find('name').string
118 artist_name
= soup
.find('artist').find('name').string
119 popularity
= soup
.find('popularity')
121 popularity
= float(popularity
.string
)*100
122 length
= float(soup
.find('length').string
)
123 minutes
= int(length
)/60
124 seconds
= int(length
)%60
126 return unescape('Track: %s - %s / %s %s:%.2d %2d%%' % (artist_name
, name
, album_name
, minutes
, seconds
, popularity
))
128 elif lookup_type
== 'album':
129 album_name
= soup
.find('album').find('name').string
130 artist_name
= soup
.find('artist').find('name').string
131 released
= soup
.find('released').string
132 return unescape('Album: %s - %s - %s' % (artist_name
, album_name
, released
))
135 return 'Unsupported type.'
138 url_data
= urlparse
.urlparse(url
)
139 query
= urlparse
.parse_qs(url_data
.query
)
140 video
= query
["v"][0]
141 api_url
= 'http://gdata.youtube.com/feeds/api/videos/%s?alt=json&v=2' % video
143 respdata
= urllib2
.urlopen(api_url
).read()
144 video_info
= json
.loads(respdata
)
146 title
= video_info
['entry']['title']["$t"]
147 author
= video_info
['entry']['author'][0]['name']['$t']
149 return unescape("Youtube: %s (%s)" % (title
, author
))
154 url
= 'http://api.justin.tv/api/stream/list.json?channel=%s' % uri
.split('/')[0]
155 respdata
= urllib2
.urlopen(url
).read()
156 twitch
= json
.loads(respdata
)
158 return unescape('Twitch: %s (%s playing %s)' % (twitch
[0]['channel']['status'], twitch
[0]['channel']['login'], twitch
[0]['channel']['meta_game']))
160 return 'Twitch: Channel offline.'
163 request
= urllib2
.Request(url
)
164 opener
= urllib2
.build_opener(SmartRedirectHandler())
166 soup
= BeautifulSoup(opener
.open(request
, timeout
=2))
167 return unescape('Title: %s' % (soup
.title
.string
))