Vés al contingut

Viquipèdia:Viquitrobada 2009/Taller de bots/Crida al traductor automatic de Gencat

De la Viquipèdia, l'enciclopèdia lliure
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import httplib, urllib

user_agent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; ca; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 GTB6 (.NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729)"
def geturl(params):
    params = urllib.urlencode(params)
    encaps= {
        'User-Agent': user_agent,
        "Content-type": "application/x-www-form-urlencoded",
        "Accept": "text/plain",
    }
    conn = httplib.HTTPConnection('traductor.gencat.net')
    conn.request("POST", "/url.do", params, encaps)
    resposta = conn.getresponse()
    return resposta.read()

url = "http://en.wikipedia.org/wiki/Mapping_of_Unicode_characters"
params={
    "translationDirection": "ENGLISH-CATALAN",
    "subjectArea": "GV",
    "MARK_ALTERNATIVES": "0",
    "url":url+"?action=edit"
}
print geturl(params)