]>
jfr.im git - erebus.git/blob - modules/urls.py
1 # Erebus IRC bot - Author: Conny Sjoblom
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 html
= HTMLParser
.HTMLParser()
30 from BeautifulSoup
import BeautifulSoup
32 import urllib
.request
as urllib2
33 import urllib
.parse
as urlparse
35 from bs4
import BeautifulSoup
37 import re
, json
, datetime
39 hostmask_regex
= re
.compile(r
'^(.*)!(.*)@(.*)$')
41 def parser_hostmask(hostmask
):
42 if isinstance(hostmask
, dict):
49 if hostmask
is not None:
50 match
= hostmask_regex
.match(hostmask
)
65 class SmartRedirectHandler(urllib2
.HTTPRedirectHandler
):
66 def http_error_301(self
, req
, fp
, code
, msg
, headers
):
67 result
= urllib2
.HTTPRedirectHandler
.http_error_301(
68 self
, req
, fp
, code
, msg
, headers
)
72 def http_error_302(self
, req
, fp
, code
, msg
, headers
):
73 result
= urllib2
.HTTPRedirectHandler
.http_error_302(
74 self
, req
, fp
, code
, msg
, headers
)
78 def process_line(line
):
81 limit
= lib
.parent
.cfg
.getint('urls', 'limit', 2)
82 for action
, group
, prefix
in regexes
:
84 for match
in regex
.findall(line
):
91 responses
.append("%s: %s" % (prefix
, action(match
)))
94 @lib.hooknum("PRIVMSG")
95 def privmsg_hook(bot
, textline
):
96 user
= parser_hostmask(textline
[1:textline
.find(' ')])
97 chan
= textline
.split()[2]
100 line
= textline
.split(None, 3)[3][1:]
104 responses
= process_line(line
)
105 if len(responses
) > 0:
106 if lib
.parent
.cfg
.getboolean('urls', 'multiline'):
108 bot
.msg(chan
, r
, True)
110 bot
.msg(chan
, ' | '.join(responses
), True)
113 return re
.sub('\s+', ' ', html
.unescape(line
))
115 def gotspotify(type, track
):
116 url
= 'http://ws.spotify.com/lookup/1/?uri=spotify:%s:%s' % (type, track
)
117 xml
= urllib2
.urlopen(url
).read()
118 soup
= BeautifulSoup(xml
, convertEntities
=BeautifulSoup
.HTML_ENTITIES
)
119 lookup_type
= soup
.contents
[2].name
121 if lookup_type
== 'track':
122 name
= soup
.find('name').string
123 album_name
= soup
.find('album').find('name').string
124 artist_name
= soup
.find('artist').find('name').string
125 popularity
= soup
.find('popularity')
127 popularity
= float(popularity
.string
)*100
128 length
= float(soup
.find('length').string
)
129 minutes
= int(length
)/60
130 seconds
= int(length
)%60
132 return unescape('Track: %s - %s / %s %s:%.2d %2d%%' % (artist_name
, name
, album_name
, minutes
, seconds
, popularity
))
134 elif lookup_type
== 'album':
135 album_name
= soup
.find('album').find('name').string
136 artist_name
= soup
.find('artist').find('name').string
137 released
= soup
.find('released').string
138 return unescape('Album: %s - %s - %s' % (artist_name
, album_name
, released
))
141 return 'Unsupported type.'
144 mo
= re
.match(r
'P(\d+D)?T(\d+H)?(\d+M)?(\d+S)?', s
)
145 pcs
= [x
for x
in mo
.groups() if x
]
146 return ''.join(pcs
).lower()
148 mo
= re
.match(r
'(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})\.(\d+)Z', s
)
149 return datetime
.datetime(*(int(x
) for x
in mo
.groups())).strftime(f
)
153 return '%.1ft' % (n
/10**12)
155 return '%.1fb' % (n
/10**9)
157 return '%.1fm' % (n
/10**6)
159 return '%.1fk' % (n
/10**3)
164 url_data
= urlparse
.urlparse(url
)
165 query
= urlparse
.parse_qs(url_data
.query
)
166 video
= query
["v"][0]
167 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'))
169 respdata
= urllib2
.urlopen(api_url
).read()
170 v
= json
.loads(respdata
)
173 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]") % {
174 'title': v
['snippet']['title'],
175 'author': v
['snippet']['channelTitle'],
176 'duration': _yt_duration(v
['contentDetails']['duration']),
177 'uploaded': _yt_date(v
['snippet']['publishedAt'], lib
.parent
.cfg
.get('urls', 'yt_date_format', '%b %d %Y')),
178 'views': _yt_round(v
['statistics']['viewCount']),
179 'likes': _yt_round(v
['statistics']['likeCount']),
180 'dislikes': _yt_round(v
['statistics']['dislikeCount']),
182 except urllib2
.HTTPError
as e
:
183 if e
.getcode() == 403:
184 return 'API limit exceeded'
189 except Exception as e
:
193 url
= 'https://api.twitch.tv/helix/streams?user_login=%s' % uri
.split('/')[0]
194 opener
= urllib2
.build_opener()
195 opener
.addheaders
= [('Client-ID', lib
.parent
.cfg
.get('urls', 'twitch_api_key'))]
196 respdata
= opener
.open(url
).read()
197 twitch
= json
.loads(respdata
)['data']
199 # TODO: add current game.
200 return unescape('\037%s\037 is %s (%s)' % (twitch
[0]['user_name'], twitch
[0]['type'], twitch
[0]['title']))
202 return 'Channel offline.'
205 for _
, group
, _
in other_regexes
:
209 request
= urllib2
.Request(url
)
210 opener
= urllib2
.build_opener(SmartRedirectHandler())
212 soup
= BeautifulSoup(opener
.open(request
, timeout
=0.5))
214 return unescape('%s' % (soup
.title
.string
))
217 except urllib2
.HTTPError
as e
:
218 return 'Error: %s %s' % (e
.code
, e
.reason
)
219 except Exception as e
:
220 return 'Error: %r' % (e
.message
)
223 re
.compile(r
'https?://[^/\s]+\.[^/\s]+(?:/\S+)?'),
226 re
.compile(r
'spotify:(?P<type>\w+):(?P<track_id>\w{22})'),
227 re
.compile(r
'https?://open\.spotify\.com/(?P<type>\w+)/(?P<track_id>\w+)')
230 re
.compile(r
'https?://(?:www\.)?youtube\.com/watch\?[a-zA-Z0-9=&_\-]+'),
233 re
.compile(r
'https?:\/\/(?:www\.)?twitch.tv\/([A-Za-z0-9]*)'),
236 (gotspotify
, spotify_regex
, 'Spotify'),
237 (gotyoutube
, youtube_regex
, 'YouTube'),
238 (gottwitch
, twitch_regex
, 'Twitch'),
240 regexes
= other_regexes
+ (
241 (goturl
, url_regex
, 'Title'),