Revision: 7738 Author: alexsh Date: 2009-12-07 07:24:45 +0000 (Mon, 07 Dec 2009)
Log Message: ----------- Fix API Upload decodeError (API mode still bypass until I confirm it is stable)
Modified Paths: -------------- trunk/pywikipedia/query.py trunk/pywikipedia/upload.py
Modified: trunk/pywikipedia/query.py =================================================================== --- trunk/pywikipedia/query.py 2009-12-06 14:05:17 UTC (rev 7737) +++ trunk/pywikipedia/query.py 2009-12-07 07:24:45 UTC (rev 7738) @@ -46,7 +46,9 @@ titlecount = 0
for k,v in params.iteritems(): - if type(v) == list: + if k == u'file': + data[k] = v + elif type(v) == list: if k in [u'titles', u'pageids', u'revids', u'ususers'] and len(v) > 10: # Titles param might be long, case convert it to post request titlecount = len(params[k]) @@ -66,7 +68,8 @@ params['noprofile'] = ''
if data: - del params[data.keys()[0]] + for k in data: + del params[k]
if wikipedia.verbose: #dump params info. wikipedia.output(u"==== API action:%s ====" % params[u'action']) @@ -109,14 +112,10 @@ while retryCount >= 0: try: jsontext = "Nothing received" - if params['action'] == 'upload' and ('file' in params or cont): + if params['action'] == 'upload' and ('file' in data): import upload - if not cont: - cont = params['file'] - del params['file'] - res, jsontext = upload.post_multipart(site, path, params.items(), - (('file', params['filename'].encode(site.encoding()), cont),), + (('file', params['filename'].encode(site.encoding()), data['file']),), site.cookies(sysop=sysop) ) elif params['action'] in postAC:
Modified: trunk/pywikipedia/upload.py =================================================================== --- trunk/pywikipedia/upload.py 2009-12-06 14:05:17 UTC (rev 7737) +++ trunk/pywikipedia/upload.py 2009-12-07 07:24:45 UTC (rev 7738) @@ -47,9 +47,12 @@ lines = [] for (key, value) in fields: lines.append('--' + boundary) - lines.append('Content-Disposition: form-data; name="%s"' % key) + lines.append('Content-Disposition: form-data; name="%s"' % str(key)) lines.append('') - lines.append(value) + try: + lines.append(str(value)) + except UnicodeEncodeError: + lines.append(value.encode('utf-8')) for (key, filename, value) in files: lines.append('--' + boundary) lines.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (key, filename))