2013-06-18 08:26:17 +00:00
import re
2013-09-03 03:00:55 +08:00
from HTMLParser import HTMLParser
2013-06-18 08:26:17 +00:00
2014-02-14 16:36:57 +13:00
from util import hook , http
2013-06-18 08:26:17 +00:00
twitch_re = ( r ' (.*:)//(twitch.tv|www.twitch.tv)(:[0-9]+)?(.*) ' , re . I )
2013-09-03 03:00:55 +08:00
multitwitch_re = ( r ' (.*:)//(www.multitwitch.tv|multitwitch.tv)/(.*) ' , re . I )
2013-06-18 08:26:17 +00:00
def test ( s ) :
2013-09-03 03:00:55 +08:00
valid = set ( ' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_/ ' )
return set ( s ) < = valid
def truncate ( msg ) :
nmsg = msg . split ( " " )
out = None
x = 0
for i in nmsg :
if x < = 7 :
if out :
out = out + " " + nmsg [ x ]
else :
out = nmsg [ x ]
2013-09-04 18:30:04 +08:00
x + = 1
2013-09-03 03:00:55 +08:00
if x < = 7 :
return out
else :
return out + " ... "
@hook.regex ( * multitwitch_re )
def multitwitch_url ( match ) :
usernames = match . group ( 3 ) . split ( " / " )
out = " "
for i in usernames :
if not test ( i ) :
print " Not a valid username "
return None
if out == " " :
out = twitch_lookup ( i )
else :
out = out + " \x02 | \x02 " + twitch_lookup ( i )
return out
2013-06-18 08:26:17 +00:00
@hook.regex ( * twitch_re )
2013-09-04 18:30:04 +08:00
def twitch_url ( match ) :
2013-09-03 03:00:55 +08:00
bit = match . group ( 4 ) . split ( " # " ) [ 0 ]
location = " / " . join ( bit . split ( " / " ) [ 1 : ] )
if not test ( location ) :
print " Not a valid username "
return None
return twitch_lookup ( location )
@hook.command ( ' twitchviewers ' )
@hook.command
def twviewers ( inp ) :
inp = inp . split ( " / " ) [ - 1 ]
if test ( inp ) :
location = inp
else :
return " Not a valid channel name. "
2013-09-03 03:22:34 +08:00
return twitch_lookup ( location ) . split ( " ( " ) [ - 1 ] . split ( " ) " ) [ 0 ] . replace ( " Online now! " , " " )
2013-09-03 03:00:55 +08:00
def twitch_lookup ( location ) :
locsplit = location . split ( " / " )
if len ( locsplit ) > 1 and len ( locsplit ) == 3 :
channel = locsplit [ 0 ]
2013-09-04 18:30:04 +08:00
type = locsplit [ 1 ] # should be b or c
2013-09-03 03:00:55 +08:00
id = locsplit [ 2 ]
2013-06-18 08:26:17 +00:00
else :
2013-09-03 03:00:55 +08:00
channel = locsplit [ 0 ]
type = None
id = None
h = HTMLParser ( )
2013-09-04 18:30:04 +08:00
fmt = " {} : {} playing {} ( {} ) " # Title: nickname playing Game (x views)
2013-09-03 03:00:55 +08:00
if type and id :
2013-09-04 18:30:04 +08:00
if type == " b " : # I haven't found an API to retrieve broadcast info
2013-09-03 03:00:55 +08:00
soup = http . get_soup ( " http://twitch.tv/ " + location )
title = soup . find ( ' span ' , { ' class ' : ' real_title js-title ' } ) . text
playing = soup . find ( ' a ' , { ' class ' : ' game js-game ' } ) . text
views = soup . find ( ' span ' , { ' id ' : ' views-count ' } ) . text + " view "
2013-09-03 03:22:34 +08:00
views = views + " s " if not views [ 0 : 2 ] == " 1 " else views
2013-09-03 03:00:55 +08:00
return h . unescape ( fmt . format ( title , channel , playing , views ) )
elif type == " c " :
data = http . get_json ( " https://api.twitch.tv/kraken/videos/ " + type + id )
title = data [ ' title ' ]
playing = data [ ' game ' ]
views = str ( data [ ' views ' ] ) + " view "
2013-09-03 03:22:34 +08:00
views = views + " s " if not views [ 0 : 2 ] == " 1 " else views
2013-09-03 03:00:55 +08:00
return h . unescape ( fmt . format ( title , channel , playing , views ) )
2013-06-18 08:26:17 +00:00
else :
2014-01-13 23:16:35 -08:00
data = http . get_json ( " http://api.justin.tv/api/stream/list.json?channel= " + channel )
if data and len ( data ) > = 1 :
data = data [ 0 ]
2013-09-03 03:22:34 +08:00
title = data [ ' title ' ]
playing = data [ ' meta_game ' ]
viewers = " \x03 3 \x02 Online now! \x02 \x0f " + str ( data [ " channel_count " ] ) + " viewer "
print viewers
viewers = viewers + " s " if not " 1 view " in viewers else viewers
print viewers
2013-09-03 03:00:55 +08:00
return h . unescape ( fmt . format ( title , channel , playing , viewers ) )
else :
2014-01-13 23:16:35 -08:00
try :
data = http . get_json ( " https://api.twitch.tv/kraken/channels/ " + channel )
except :
return
2013-09-03 03:00:55 +08:00
title = data [ ' status ' ]
playing = data [ ' game ' ]
viewers = " \x03 4 \x02 Offline \x02 \x0f "
return h . unescape ( fmt . format ( title , channel , playing , viewers ) )