3 # Copyright (c) 2007 Alejandro Mery <amery@geeks.cl>
8 def describe( name
, o
, indent
="" ):
12 print "%s%s: (%d entries)" % (indent
, name
, len(o
))
13 for i
in range(len(o
)):
14 describe( "%d" % i
, o
[i
], indent
+ " " )
16 print "%s%s: %d %s" % (indent
, name
, len(o
), str(o
))
18 print "%s%s: (%d entries)" % (indent
, name
, len(o
))
19 for k
,v
in o
.iteritems():
20 describe( k
, v
, indent
+ " " )
22 print "%s%s: %s" % ( indent
, name
, o
)
24 print "%s%s: %s" % ( indent
, name
, o_type
)
26 print "%s%s" % (indent
, o_type
)
31 self
.conn
= httplib
.HTTPConnection("www.translingo.org")
34 def request( self
, method
, location
, params
, headers
, referer
= "http://www.translingo.org/main.php" ):
35 from urllib
import urlencode
37 p
= urlencode( params
)
38 h
= { "Referer": referer
}
41 h
[ "Content-type" ] = "application/x-www-form-urlencoded"
42 h
[ "Accept" ] = "text/plain"
44 if len(self
.cookies
) > 0:
45 h
[ "Cookies" ] = list()
47 for name
,value
in self
.cookies
.iteritems():
48 h
[ "Cookies" ].append( "%s=%s" % (name
, value
) )
51 for k
,v
in headers
.iteritems():
55 print "request: %s, params: %s" % (location
, p
)
56 describe( "headers", h
)
58 self
.conn
.request(method
, location
, p
, h
)
59 response
= self
.conn
.getresponse()
61 for head
in response
.getheaders():
62 if head
[0] == "set-cookie":
63 # Got a cookie! (at least one)
64 print "reseting cookies"
66 for cookie
in head
[1].split(", "):
67 for field
in cookie
.split("; "):
68 (name
,value
) = field
.split("=")
73 if not self
.cookies
.has_key( name
):
74 self
.cookies
[ name
] = list()
76 self
.cookies
[ name
].append( value
)
78 print " Cookie: %s=%s" % ( name
, value
)
82 def connect( self
, username
, password
):
89 response
= self
.request("POST", "/login.php", params
, None )
90 print response
.status
, response
.reason
94 def disconnect( self
):
99 response
= self
.request("GET", "/main.php", params
, None )
100 print response
.status
, response
.reason
101 print response
.read()
108 def translingo_version(option
, opt
, value
, parser
):
110 print "Translingo v%s" % VERSION
111 print "Copyright (c) 2007 Alejandro Mery <amery@geeks.cl>"
115 from optparse
import OptionParser
117 parser
= OptionParser()
118 parser
.add_option( "-V", "--version", action
="callback", callback
=translingo_version
, help="print version and copyright" )
119 parser
.add_option( "-u", "--username", action
="store", type="string", dest
="username", help="username to use when connecting to translingo" )
120 parser
.add_option( "-p", "--password", action
="store", type="string", dest
="password", help="password to use when connecting to translingo" )
122 (options
, args
) = parser
.parse_args()
128 tl
.connect( options
.username
, options
.password
)
131 if __name__
== "__main__":
134 # :vim:set ft=python: