* introduced Translingo class
[translingo.git] / translingo
blobcb221763f84e04c820a54ebb9ccdb3a08edfad42
1 #!/usr/bin/python
2 # -*- coding: utf8 -*-
3 # Copyright (c) 2007 Alejandro Mery <amery@geeks.cl>
6 VERSION = "0"
8 def describe( name, o, indent="" ):
9 o_type = type( o )
11 if o_type == list:
12 print "%s%s: (%d entries)" % (indent, name, len(o))
13 for i in range(len(o)):
14 describe( "%d" % i, o[i], indent + " " )
15 elif o_type == tuple:
16 print "%s%s: %d %s" % (indent, name, len(o), str(o))
17 elif o_type == dict:
18 print "%s%s: (%d entries)" % (indent, name, len(o))
19 for k,v in o.iteritems():
20 describe( k, v, indent + " " )
21 elif o_type == str:
22 print "%s%s: %s" % ( indent, name, o )
23 elif name:
24 print "%s%s: %s" % ( indent, name, o_type )
25 else:
26 print "%s%s" % (indent, o_type )
28 class Translingo:
29 def __init__( self ):
30 import httplib
31 self.conn = httplib.HTTPConnection("www.translingo.org")
32 self.cookies = {}
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 }
40 if method == "POST":
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 ) )
50 if headers:
51 for k,v in headers.iteritems():
52 h[ k ] = v
54 if True:
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"
65 self.cookies = {}
66 for cookie in head[1].split(", "):
67 for field in cookie.split("; "):
68 (name,value) = field.split("=")
70 if name == "path":
71 continue
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 )
80 return response
82 def connect( self, username, password ):
83 params = {
84 "username": username,
85 "password": password,
86 "submit": "Login",
87 "return": 1,
89 response = self.request("POST", "/login.php", params, None )
90 print response.status, response.reason
91 print response.read()
92 response.close()
94 def disconnect( self ):
95 params = {
96 "action": "logout",
99 response = self.request("GET", "/main.php", params, None )
100 print response.status, response.reason
101 print response.read()
102 response.close()
104 self.conn.close()
108 def translingo_version(option, opt, value, parser):
109 import sys
110 print "Translingo v%s" % VERSION
111 print "Copyright (c) 2007 Alejandro Mery <amery@geeks.cl>"
112 sys.exit()
114 def main():
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()
124 print options
125 print args
127 tl = Translingo()
128 tl.connect( options.username, options.password )
129 tl.disconnect()
131 if __name__ == "__main__":
132 main()
134 # :vim:set ft=python: