]> jfr.im git - erebus.git/blame - modules/urls.py
urls.py module redone
[erebus.git] / modules / urls.py
CommitLineData
a83e1f9c 1# Erebus IRC bot - Author: Erebus Team
2# URL Checker
3# This file is released into the public domain; see http://unlicense.org/
4
5# module info
6modinfo = {
7 'author': 'Erebus Team',
8 'license': 'public domain',
9 'compatible': [1], # compatible module API versions
10 'depends': [], # other modules required to work properly?
11}
12
99366200
CS
13# http://embed.ly/tools/generator
14
a83e1f9c 15# preamble
16import modlib
17lib = modlib.modlib(__name__)
18modstart = lib.modstart
19modstop = lib.modstop
20
21# module code
390fbad4 22import re, urllib2, urlparse, json, HTMLParser
a83e1f9c 23from BeautifulSoup import BeautifulSoup
24
390fbad4 25html_parser = HTMLParser.HTMLParser()
a83e1f9c 26
390fbad4
CS
27hostmask_regex = re.compile(r'^(.*)!(.*)@(.*)$')
28url_regex = re.compile(r'((?:https?://|spotify:)[^\s]+)')
a83e1f9c 29spotify_regex = (
30 re.compile(r'spotify:(?P<type>\w+):(?P<track_id>\w{22})'),
390fbad4 31 re.compile(r'https?://open.spotify.com/(?P<type>\w+)/(?P<track_id>\w{22})')
a83e1f9c 32)
33youtube_regex = (
34 re.compile(r'https?://(?:www\.)?youtube\.com/watch\?[a-zA-Z0-9=&_\-]+'),
35)
36twitch_regex = (
390fbad4 37 re.compile(r'https?://(?:www\.)?twitch.tv/(.*)\w{1,}'),
a83e1f9c 38)
39
40def parser_hostmask(hostmask):
41 if isinstance(hostmask, dict):
42 return hostmask
43
44 nick = None
45 user = None
46 host = None
47
48 if hostmask is not None:
49 match = hostmask_regex.match(hostmask)
50
51 if not match:
52 nick = hostmask
53 else:
54 nick = match.group(1)
55 user = match.group(2)
56 host = match.group(3)
57
58 return {
59 'nick': nick,
60 'user': user,
61 'host': host
62 }
63
64@lib.hooknum("PRIVMSG")
390fbad4
CS
65def privmsg_hook(bot, textline):
66 user = parser_hostmask(textline[1:textline.find(' ')])
67 chan = textline.split()[2]
a83e1f9c 68
69 try:
390fbad4 70 line = textline.split(None, 3)[3][1:]
a83e1f9c 71 except IndexError:
390fbad4 72 line = ''
a83e1f9c 73
390fbad4
CS
74 for match in url_regex.findall(line):
75 if match:
76 print match
77 if 'open.spotify.com' in match or 'spotify:' in match:
78 for r in spotify_regex:
79 for sptype, track in r.findall(match):
80 bot.msg(chan, unescape(gotspotify(sptype, track)))
a83e1f9c 81
390fbad4
CS
82 elif 'youtube.com' in match or 'youtu.be' in match:
83 for r in youtube_regex:
84 for url in r.findall(match):
85 bot.msg(chan, unescape(gotyoutube(url)))
a83e1f9c 86
390fbad4
CS
87 elif 'twitch.tv' in match:
88 for r in twitch_regex:
89 for uri in r.findall(match):
90 bot.msg(chan, unescape(gottwitch(uri)))
a83e1f9c 91
390fbad4
CS
92 else:
93 bot.msg(chan, unescape(goturl(match)))
a83e1f9c 94
390fbad4
CS
95def unescape(line):
96 return html_parser.unescape(line)
a83e1f9c 97
98def gotspotify(type, track):
99 url = 'http://ws.spotify.com/lookup/1/?uri=spotify:%s:%s' % (type, track)
100 xml = urllib2.urlopen(url).read()
390fbad4 101 soup = BeautifulSoup(xml, convertEntities=BeautifulSoup.HTML_ENTITIES)
a83e1f9c 102 lookup_type = soup.contents[2].name
390fbad4 103
a83e1f9c 104 if lookup_type == 'track':
105 name = soup.find('name').string
106 album_name = soup.find('album').find('name').string
107 artist_name = soup.find('artist').find('name').string
108 popularity = soup.find('popularity')
109 if popularity:
110 popularity = float(popularity.string)*100
111 length = float(soup.find('length').string)
112 minutes = int(length)/60
113 seconds = int(length)%60
390fbad4 114
a83e1f9c 115 return 'Track: %s - %s / %s %s:%.2d %2d%%' % (artist_name, name, album_name, minutes, seconds, popularity)
390fbad4 116
a83e1f9c 117 elif lookup_type == 'album':
118 album_name = soup.find('album').find('name').string
119 artist_name = soup.find('artist').find('name').string
120 released = soup.find('released').string
121 return 'Album: %s - %s - %s' % (artist_name, album_name, released)
390fbad4 122
a83e1f9c 123 else:
124 return 'Unsupported type.'
125
126def gotyoutube(url):
127 url_data = urlparse.urlparse(url)
128 query = urlparse.parse_qs(url_data.query)
129 video = query["v"][0]
130 api_url = 'http://gdata.youtube.com/feeds/api/videos/%s?alt=json&v=2' % video
131 try:
132 respdata = urllib2.urlopen(api_url).read()
133 video_info = json.loads(respdata)
134
135 title = video_info['entry']['title']["$t"]
136 author = video_info['entry']['author'][0]['name']['$t']
137
138 return "Youtube: %s (%s)" % (title, author)
139 except:
140 pass
141
390fbad4
CS
142def gottwitch(uri):
143 url = 'http://api.justin.tv/api/stream/list.json?channel=%s' % uri.split('/')[0]
144 respdata = urllib2.urlopen(url).read()
145 twitch = json.loads(respdata)
146 try:
147 return 'Twitch: %s (%s playing %s)' % (twitch[0]['channel']['status'], twitch[0]['channel']['login'], twitch[0]['channel']['meta_game'])
148 except:
149 return 'Twitch: Channel offline.'
150
151def goturl(url):
152 soup = BeautifulSoup(urllib2.urlopen(url))
153 return soup.title.string