]>
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
36 import re
, json
, datetime
38 html_parser
= HTMLParser
.HTMLParser()
40 hostmask_regex
= re
.compile(r
'^(.*)!(.*)@(.*)$')
42 def parser_hostmask(hostmask
):
43 if isinstance(hostmask
, dict):
50 if hostmask
is not None:
51 match
= hostmask_regex
.match(hostmask
)
66 class SmartRedirectHandler(urllib2
.HTTPRedirectHandler
):
67 def http_error_301(self
, req
, fp
, code
, msg
, headers
):
68 result
= urllib2
.HTTPRedirectHandler
.http_error_301(
69 self
, req
, fp
, code
, msg
, headers
)
73 def http_error_302(self
, req
, fp
, code
, msg
, headers
):
74 result
= urllib2
.HTTPRedirectHandler
.http_error_302(
75 self
, req
, fp
, code
, msg
, headers
)
79 def process_line(line
):
82 limit
= lib
.parent
.cfg
.getint('urls', 'limit', 2)
83 for action
, group
, prefix
in regexes
:
85 for match
in regex
.findall(line
):
92 responses
.append("%s: %s" % (prefix
, action(match
)))
95 @lib.hooknum("PRIVMSG")
96 def privmsg_hook(bot
, textline
):
97 user
= parser_hostmask(textline
[1:textline
.find(' ')])
98 chan
= textline
.split()[2]
101 line
= textline
.split(None, 3)[3][1:]
105 responses
= process_line(line
)
106 if len(responses
) > 0:
107 if lib
.parent
.cfg
.getboolean('urls', 'multiline'):
109 bot
.msg(chan
, r
, True)
111 bot
.msg(chan
, ' | '.join(responses
), True)
114 return re
.sub('\s+', ' ', html_parser
.unescape(line
))
116 def gotspotify(type, track
):
117 url
= 'http://ws.spotify.com/lookup/1/?uri=spotify:%s:%s' % (type, track
)
118 xml
= urllib2
.urlopen(url
).read()
119 soup
= BeautifulSoup(xml
, convertEntities
=BeautifulSoup
.HTML_ENTITIES
)
120 lookup_type
= soup
.contents
[2].name
122 if lookup_type
== 'track':
123 name
= soup
.find('name').string
124 album_name
= soup
.find('album').find('name').string
125 artist_name
= soup
.find('artist').find('name').string
126 popularity
= soup
.find('popularity')
128 popularity
= float(popularity
.string
)*100
129 length
= float(soup
.find('length').string
)
130 minutes
= int(length
)/60
131 seconds
= int(length
)%60
133 return unescape('Track: %s - %s / %s %s:%.2d %2d%%' % (artist_name
, name
, album_name
, minutes
, seconds
, popularity
))
135 elif lookup_type
== 'album':
136 album_name
= soup
.find('album').find('name').string
137 artist_name
= soup
.find('artist').find('name').string
138 released
= soup
.find('released').string
139 return unescape('Album: %s - %s - %s' % (artist_name
, album_name
, released
))
142 return 'Unsupported type.'
145 mo
= re
.match(r
'P(\d+D)?T(\d+H)?(\d+M)?(\d+S)?', s
)
146 pcs
= [x
for x
in mo
.groups() if x
]
147 return ''.join(pcs
).lower()
149 mo
= re
.match(r
'(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})\.(\d+)Z', s
)
150 return datetime
.datetime(*(int(x
) for x
in mo
.groups())).strftime(f
)
154 return '%.1ft' % (n
/10**12)
156 return '%.1fb' % (n
/10**9)
158 return '%.1fm' % (n
/10**6)
160 return '%.1fk' % (n
/10**3)
165 url_data
= urlparse
.urlparse(url
)
166 query
= urlparse
.parse_qs(url_data
.query
)
167 video
= query
["v"][0]
168 api_url
= 'https://www.googleapis.com/youtube/v3/videos?part=snippet,contentDetails,statistics&id=%s&key=%s' % (video
, lib
.parent
.cfg
.get('urls', 'api_key'))
170 respdata
= urllib2
.urlopen(api_url
).read()
171 v
= json
.loads(respdata
)
174 return unescape(lib
.parent
.cfg
.get('urls', 'yt_format', "\002%(author)s\002: \037%(title)s\037 [%(duration)s, uploaded %(uploaded)s, %(views)s v/%(likes)s l/%(dislikes)s d]") % {
175 'title': v
['snippet']['title'],
176 'author': v
['snippet']['channelTitle'],
177 'duration': _yt_duration(v
['contentDetails']['duration']),
178 'uploaded': _yt_date(v
['snippet']['publishedAt'], lib
.parent
.cfg
.get('urls', 'yt_date_format', '%b %d %Y')),
179 'views': _yt_round(v
['statistics']['viewCount']),
180 'likes': _yt_round(v
['statistics']['likeCount']),
181 'dislikes': _yt_round(v
['statistics']['dislikeCount']),
183 except urllib2
.HTTPError
as e
:
184 if e
.getcode() == 403:
185 return 'API limit exceeded'
190 except Exception as e
:
194 url
= 'https://api.twitch.tv/helix/streams?user_login=%s' % uri
.split('/')[0]
195 opener
= urllib2
.build_opener()
196 opener
.addheaders
= [('Client-ID', lib
.parent
.cfg
.get('urls', 'twitch_api_key'))]
197 respdata
= opener
.open(url
).read()
198 twitch
= json
.loads(respdata
)['data']
200 # TODO: add current game.
201 return unescape('\037%s\037 is %s (%s)' % (twitch
[0]['user_name'], twitch
[0]['type'], twitch
[0]['title']))
203 return 'Channel offline.'
206 for _
, group
, _
in other_regexes
:
210 request
= urllib2
.Request(url
)
211 opener
= urllib2
.build_opener(SmartRedirectHandler())
213 soup
= BeautifulSoup(opener
.open(request
, timeout
=0.5))
215 return unescape('%s' % (soup
.title
.string
))
218 except urllib2
.HTTPError
as e
:
219 return 'Error: %s %s' % (e
.code
, e
.reason
)
220 except Exception as e
:
221 return 'Error: %r' % (e
.message
)
224 re
.compile(r
'https?://[^/\s]+\.[^/\s]+(?:/\S+)?'),
227 re
.compile(r
'spotify:(?P<type>\w+):(?P<track_id>\w{22})'),
228 re
.compile(r
'https?://open.spotify.com/(?P<type>\w+)/(?P<track_id>\w+)')
231 re
.compile(r
'https?://(?:www\.)?youtube\.com/watch\?[a-zA-Z0-9=&_\-]+'),
234 re
.compile(r
'https?:\/\/(?:www\.)?twitch.tv\/([A-Za-z0-9]*)'),
237 (gotspotify
, spotify_regex
, 'Spotify'),
238 (gotyoutube
, youtube_regex
, 'YouTube'),
239 (gottwitch
, twitch_regex
, 'Twitch'),
241 regexes
= other_regexes
+ (
242 (goturl
, url_regex
, 'Title'),