@ -12,19 +12,19 @@ from ttp import ttp
class TwistParser ( ttp . Parser ) :
class TwistParser ( ttp . Parser ) :
def format_tag ( self , tag , text ) :
def format_tag ( self , tag , text ) :
''' Return formatted HTML for a hashtag. '''
''' Return formatted HTML for a hashtag. '''
return ' <a href= " {0} /tag/ {1} " > {2} {3} </a> ' . format (
return ' <a target= " _top " href= " {0} /tag/ {1} " > {2} {3} </a> ' . format (
cherrypy . request . base + cherrypy . request . script_name ,
cherrypy . request . base + cherrypy . request . script_name ,
ttp . urllib . quote ( text . lower ( ) . encode ( ' utf-8 ' ) , ' xmlcharrefreplace ' ) , tag , text )
ttp . urllib . quote ( text . lower ( ) . encode ( ' utf-8 ' ) , ' xmlcharrefreplace ' ) , tag , text )
def format_username ( self , at_char , user ) :
def format_username ( self , at_char , user ) :
''' Return formatted HTML for a username. '''
''' Return formatted HTML for a username. '''
return ' <a href= " {0} /user/ {1} " > {2} {3} </a> ' . format (
return ' <a target= " _top " href= " {0} /user/ {1} " > {2} {3} </a> ' . format (
cherrypy . request . base + cherrypy . request . script_name ,
cherrypy . request . base + cherrypy . request . script_name ,
user , at_char , user . lower ( ) )
user , at_char , user . lower ( ) )
def format_list ( self , at_char , user , list_name ) :
def format_list ( self , at_char , user , list_name ) :
''' We don ' t have lists, so we see it as " @user " followed by " /something " '''
''' We don ' t have lists, so we see it as " @user " followed by " /something " '''
return ' <a href= " {0} /user/ {1} " > {2} {3} </a>/ {4} ' . format (
return ' <a target= " _top " href= " {0} /user/ {1} " > {2} {3} </a>/ {4} ' . format (
cherrypy . request . base + cherrypy . request . script_name ,
cherrypy . request . base + cherrypy . request . script_name ,
cherrypy . request . base + cherrypy . request . script_name , user , at_char , user . lower ( ) , list_name )
cherrypy . request . base + cherrypy . request . script_name , user , at_char , user . lower ( ) , list_name )
@ -45,47 +45,47 @@ def format_trending(twister,num_messages=8):
### The Swizzler app
### The Swizzler app
class SwizzlerApp ( object ) :
class SwizzlerApp ( object ) :
def _standard_params ( self , twister , q , num_items = 8 ) :
@cherrypy . expose
def search ( self , q = ' ' ) :
conf = cherrypy . request . app . config [ ' swizzler ' ]
twister = Twister ( conf [ ' rpc_url ' ] , format_twist )
result = { ' site_root ' : cherrypy . request . base + cherrypy . request . script_name }
result = { ' site_root ' : cherrypy . request . base + cherrypy . request . script_name }
result [ ' here ' ] = result [ ' site_root ' ] + cherrypy . request . path_info
q = q . strip ( ) . split ( ' ' ) [ 0 ] # ignore anything after the first space if any
q = ( q or ' ' ) . strip ( ) . split ( ' ' ) [ 0 ] # ignore anything after the first space if any
if q . startswith ( ' # ' ) : # user said "#sometag", change to "sometag"
if q . startswith ( ' # ' ) : # user said "#sometag", change to "sometag"
q = q [ 1 : ]
q = q [ 1 : ]
if q and not q . startswith ( ' @ ' ) : # Tag. redirect.
if q and not q . startswith ( ' @ ' ) : # Tag. redirect.
raise cherrypy . HTTPRedirect ( result [ ' site_root ' ] + ' /tag/ {0} ' . format ( q ) )
raise cherrypy . HTTPRedirect ( result [ ' site_root ' ] + ' /tag/ {0} ' . format ( q ) )
if q :
if q :
result [ ' user_prefix ' ] = q
result [ ' user_prefix ' ] = q
result [ ' users ' ] = twister . get_users_by_partial_name ( q [ 1 : ] , num_items )
result [ ' users ' ] = twister . get_users_by_partial_name ( q [ 1 : ] , conf [ ' num_messages ' ] )
return result
else :
result [ ' trending ' ] = format_trending ( twister , num_items )
result [ ' trending ' ] = format_trending ( twister , conf [ ' num_messages ' ] )
return result
return stache . render ( stache . load_template ( ' search ' ) , result )
@cherrypy . expose
@cherrypy . expose
def twist ( self , username , k , q = None ) :
def twist ( self , username , k ) :
conf = cherrypy . request . app . config [ ' swizzler ' ]
conf = cherrypy . request . app . config [ ' swizzler ' ]
twister = Twister ( conf [ ' rpc_url ' ] , format_twist )
twister = Twister ( conf [ ' rpc_url ' ] , format_twist )
params = self . _standard_params ( twister , q , conf [ ' num_messages ' ] ) # called as soon as possible, because it might raise a redirect
twist = twister . get_twist ( username , k )
twist = twister . get_twist ( username , k )
twist [ ' style_large ' ] = True
rts = twister . get_twist_rts ( username , k )
rts = twister . get_twist_rts ( username , k )
print rts
result = {
result = {
' is_twist ' : True ,
' is_twist ' : True ,
' title ' : u " @ {0} : {1} - Swizzler " . format ( twist [ ' user ' ] [ ' username' ] , twist [ ' time ' ] ) ,
' title ' : u " @ {0} : {1} - Swizzler " . format ( username , twist [ ' time ' ] ) ,
' twist ' : twist ,
' twist ' : twist ,
' in_reply_to ' : twist . get ( ' reply ' ) and twister . get_twist ( twist [ ' reply ' ] [ ' user ' ] [ ' user name' ] , twist [ ' reply ' ] [ ' k ' ] ) or None ,
' in_reply_to ' : twist . get ( ' reply ' ) and twister . get_twist ( twist [ ' reply ' ] [ ' username ' ] , twist [ ' reply ' ] [ ' k ' ] ) or None ,
' replies ' : twister . get_twist_replies ( username , k ) ,
' replies ' : twister . get_twist_replies ( username , k ) ,
' rts ' : rts ,
' rts ' : rts ,
' any_rts ' : not not rts ,
' any_rts ' : not not rts ,
' local_users ' : twister . local_user_menu ( ) [ ' users ' ] ,
' local_users ' : twister . local_user_menu ( ) [ ' users ' ] ,
' info ' : twister . get_info ( ) ,
' info ' : twister . get_info ( ) ,
' site_root ' : cherrypy . request . base + cherrypy . request . script_name ,
}
}
result . update ( params )
return stache . render ( stache . load_template ( ' twist ' ) , result )
return stache . render ( stache . load_template ( ' twist ' ) , result )
@cherrypy . expose
@cherrypy . expose
def user ( self , username , q = None ) :
def user ( self , username ) :
conf = cherrypy . request . app . config [ ' swizzler ' ]
conf = cherrypy . request . app . config [ ' swizzler ' ]
twister = Twister ( conf [ ' rpc_url ' ] , format_twist )
twister = Twister ( conf [ ' rpc_url ' ] , format_twist )
params = self . _standard_params ( twister , q , conf [ ' num_messages ' ] ) # called as soon as possible, because it might raise a redirect
user = twister . get_user_info ( username )
user = twister . get_user_info ( username )
messages = twister . get_user_posts ( username , conf [ ' num_messages ' ] )
messages = twister . get_user_posts ( username , conf [ ' num_messages ' ] )
result = {
result = {
@ -96,14 +96,26 @@ class SwizzlerApp(object):
' any_messages ' : not not messages ,
' any_messages ' : not not messages ,
' local_users ' : twister . local_user_menu ( ) [ ' users ' ] ,
' local_users ' : twister . local_user_menu ( ) [ ' users ' ] ,
' info ' : twister . get_info ( ) ,
' info ' : twister . get_info ( ) ,
' site_root ' : cherrypy . request . base + cherrypy . request . script_name ,
}
}
result . update ( params )
return stache . render ( stache . load_template ( ' standard ' ) , result )
return stache . render ( stache . load_template ( ' standard ' ) , result )
@cherrypy . expose
@cherrypy . expose
def tag ( self , tag , q = None ) :
def user_embed ( self , username = ' nobody ' , style = ' normal ' ) :
if username == ' nobody ' : username = ' ' # to enable /nobody/large
conf = cherrypy . request . app . config [ ' swizzler ' ]
twister = Twister ( conf [ ' rpc_url ' ] , format_twist )
user = twister . get_user_info ( username )
result = {
' title ' : ' @ {0} - Swizzler ' . format ( username ) ,
' site_root ' : cherrypy . request . base + cherrypy . request . script_name ,
' user ' : twister . get_user_info ( username )
}
result [ ' style_ {0} ' . format ( style ) ] = True
return stache . render ( stache . load_template ( ' user-iframe ' ) , result )
@cherrypy . expose
def tag ( self , tag ) :
conf = cherrypy . request . app . config [ ' swizzler ' ]
conf = cherrypy . request . app . config [ ' swizzler ' ]
twister = Twister ( conf [ ' rpc_url ' ] , format_twist )
twister = Twister ( conf [ ' rpc_url ' ] , format_twist )
params = self . _standard_params ( twister , q , conf [ ' num_messages ' ] ) # called as soon as possible, because it might raise a redirect
messages = twister . get_tag_posts ( tag )
messages = twister . get_tag_posts ( tag )
result = {
result = {
' is_tag ' : True ,
' is_tag ' : True ,
@ -113,15 +125,13 @@ class SwizzlerApp(object):
' any_messages ' : not not messages ,
' any_messages ' : not not messages ,
' local_users ' : twister . local_user_menu ( ) [ ' users ' ] ,
' local_users ' : twister . local_user_menu ( ) [ ' users ' ] ,
' info ' : twister . get_info ( ) ,
' info ' : twister . get_info ( ) ,
#the filter avoids some utf etc. that ttf can't handle (TODO: fix or replace format_twist)
' site_root ' : cherrypy . request . base + cherrypy . request . script_name ,
}
}
result . update ( params )
return stache . render ( stache . load_template ( ' standard ' ) , result )
return stache . render ( stache . load_template ( ' standard ' ) , result )
@cherrypy . expose
@cherrypy . expose
def home ( self , localusername , mode = ' feed ' , q = None ) :
def home ( self , localusername , mode = ' feed ' ) :
conf = cherrypy . request . app . config [ ' swizzler ' ]
conf = cherrypy . request . app . config [ ' swizzler ' ]
twister = Twister ( conf [ ' rpc_url ' ] , format_twist )
twister = Twister ( conf [ ' rpc_url ' ] , format_twist )
params = self . _standard_params ( twister , q , conf [ ' num_messages ' ] ) # called as soon as possible, because it might raise a redirect
menu = twister . local_user_menu ( localusername )
menu = twister . local_user_menu ( localusername )
if mode == ' mentions ' :
if mode == ' mentions ' :
messages = twister . get_user_mentions ( localusername )
messages = twister . get_user_mentions ( localusername )
@ -131,20 +141,19 @@ class SwizzlerApp(object):
' is_home ' : True ,
' is_home ' : True ,
' is_mentions ' : mode == ' mentions ' ,
' is_mentions ' : mode == ' mentions ' ,
' is_feed ' : mode != ' mentions ' ,
' is_feed ' : mode != ' mentions ' ,
' title ' : u " {fullname} (@ {username} ): Home - Swizzler " . format ( * * menu [ ' active ' ] ) ,
' title ' : u " {fullname} (@ {username} ): {mode} - Swizzler" . format ( mode = mode == ' mentions ' and ' Mentions ' or ' Home ' , * * menu [ ' active ' ] ) ,
' local_users ' : menu [ ' users ' ] ,
' local_users ' : menu [ ' users ' ] ,
' info ' : twister . get_info ( ) ,
' info ' : twister . get_info ( ) ,
' subject ' : menu [ ' active ' ] ,
' subject ' : menu [ ' active ' ] ,
' messages ' : messages ,
' messages ' : messages ,
' any_messages ' : not not messages ,
' any_messages ' : not not messages ,
' site_root ' : cherrypy . request . base + cherrypy . request . script_name ,
}
}
result . update ( params )
return stache . render ( stache . load_template ( ' standard ' ) , result )
return stache . render ( stache . load_template ( ' standard ' ) , result )
@cherrypy . expose
@cherrypy . expose
def messages ( self , localusername , remoteusername = None , q = None ) :
def messages ( self , localusername , remoteusername = None ) :
conf = cherrypy . request . app . config [ ' swizzler ' ]
conf = cherrypy . request . app . config [ ' swizzler ' ]
twister = Twister ( conf [ ' rpc_url ' ] , format_twist )
twister = Twister ( conf [ ' rpc_url ' ] , format_twist )
params = self . _standard_params ( twister , q , conf [ ' num_messages ' ] ) # called as soon as possible, because it might raise a redirect
localuser = twister . get_user_info ( localusername )
localuser = twister . get_user_info ( localusername )
remoteuser = remoteusername and twister . get_user_info ( remoteusername ) or None
remoteuser = remoteusername and twister . get_user_info ( remoteusername ) or None
threads = remoteusername and twister . get_user_messages ( localusername , remoteusername , conf [ ' num_messages ' ] ) or twister . get_user_messages ( localusername )
threads = remoteusername and twister . get_user_messages ( localusername , remoteusername , conf [ ' num_messages ' ] ) or twister . get_user_messages ( localusername )
@ -158,18 +167,17 @@ class SwizzlerApp(object):
' any_threads ' : not not threads ,
' any_threads ' : not not threads ,
' local_users ' : twister . local_user_menu ( ) [ ' users ' ] ,
' local_users ' : twister . local_user_menu ( ) [ ' users ' ] ,
' info ' : twister . get_info ( ) ,
' info ' : twister . get_info ( ) ,
#the filter avoids some utf etc. that ttf can't handle (TODO: fix or replace format_twist)
' site_root ' : cherrypy . request . base + cherrypy . request . script_name ,
}
}
result . update ( params )
return stache . render ( stache . load_template ( ' messages ' ) , result )
return stache . render ( stache . load_template ( ' messages ' ) , result )
@cherrypy . expose
@cherrypy . expose
def index ( self , q = None ) :
def index ( self ) :
conf = cherrypy . request . app . config [ ' swizzler ' ]
conf = cherrypy . request . app . config [ ' swizzler ' ]
twister = Twister ( conf [ ' rpc_url ' ] , format_twist )
twister = Twister ( conf [ ' rpc_url ' ] , format_twist )
params = self . _standard_params ( twister , q , conf [ ' num_messages ' ] ) # called as soon as possible, because it might raise a redirect
messages = twister . get_sponsored_posts ( conf [ ' num_messages ' ] )
messages = twister . get_sponsored_posts ( conf [ ' num_messages ' ] )
result = {
result = {
' is_user ' : True , # i.e. we want to display "bio" and not mentions/DMs/profile buttons
' is_user ' : True , # i.e. we want to display "bio" and not mentions/DMs/profile buttons
' is_sponsored ' : True , # message template needs to know not to show "permalink"
' title ' : " Welcome to Swizzler " ,
' title ' : " Welcome to Swizzler " ,
' local_users ' : twister . local_user_menu ( ' ' ) [ ' users ' ] , # '' means: "Nobody" is active
' local_users ' : twister . local_user_menu ( ' ' ) [ ' users ' ] , # '' means: "Nobody" is active
' info ' : twister . get_info ( ) ,
' info ' : twister . get_info ( ) ,
@ -184,9 +192,8 @@ Start mining today, and all this (AND moral satisfaction) can be yours.""")
} ,
} ,
' messages ' : messages ,
' messages ' : messages ,
' any_messages ' : not not messages ,
' any_messages ' : not not messages ,
#the filter avoids some utf etc. that ttf can't handle (TODO: fix or replace format_twist)
' site_root ' : cherrypy . request . base + cherrypy . request . script_name ,
}
}
result . update ( params )
return stache . render ( stache . load_template ( ' standard ' ) , result )
return stache . render ( stache . load_template ( ' standard ' ) , result )
if __name__ == ' __main__ ' :
if __name__ == ' __main__ ' :