Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- import json
- USER,PASS=u'Botname@instancename',u'[[Special:BotPassword]]'
- REMOTENAME="storkk's test upload.jpg"
- FILENAME='localfile'
- with open(FILENAME,'rb') as f:
- CONTENTS=f.read()
- api_url = 'https://commons.wikimedia.org/w/api.php'
- #get login token and log in
- payload = {'action': 'query', 'format': 'json', 'utf8': '',
- 'meta': 'tokens', 'type': 'login'}
- r1 = requests.post(api_url, data=payload)
- login_token=r1.json()['query']['tokens']['logintoken']
- payload = {'action': 'login', 'format': 'json', 'utf8': '',
- 'lgname': USER, 'lgpassword': PASS, 'lgtoken': login_token}
- r2 = requests.post(api_url, data=payload, cookies=r1.cookies)
- cookies=r2.cookies.copy()
- assert r2.json()['login']['result']=='Success'
- def get_edit_token(cookies):
- token_response=requests.post(api_url, data={'action': 'query',
- 'format': 'json',
- 'meta': 'tokens'}, cookies=cookies)
- return token_response.json()['query']['tokens']['csrftoken']
- token=get_edit_token(cookies)
- # Attempt 1 -- the local file is a duplicate, so this should fail or warn as a duplicate
- # (it's [[:File:Rhode_Island_Greenings-18d446.jpg]] )
- # It should not fail due to a malformed request
- upload_payload={'action': 'upload',
- 'format':'json',
- 'filename':REMOTENAME,
- 'file':CONTENTS,
- 'comment':'test upload (should fail/warn of duplicate)',
- 'text':'apple test upload\n\n{{OGL2}}\n\nPlease contact [[User:Storkk]] if there are issues',
- 'token':token}
- headers={'Content-Type': 'multipart/form-data', 'User-Agent': 'Drudgebane Storkksbot (contact [[C:User:Storkk]]) Based on python requests/2.11.1'}
- upload_response = requests.post(api_url, data=upload_payload, cookies=cookies,headers=headers)
- print(upload_response.json())
- # FAIL :
- #'code': 'badupload_file',
- #'info': 'File upload param file is not a file upload; be sure to use multipart/form-data for your POST and include a filename in the Content-Disposition header.'
- # For some reason after each failure I need to get a new token
- # or the api seems to start hanging and returning the HTML API help:
- upload_payload['token']=get_edit_token(cookies)
- # add Content-Disposition:
- headers['Content-Disposition']='form-data; name="file"; filename="{}"'.format(REMOTENAME)
- upload2_response=requests.post(api_url, data=upload_payload,cookies=cookies,headers=headers)
- print(upload2_response.json())
- # FAIL (identical error)...
- # Attempt new method (using MultipartEncoder)
- from requests_toolbelt.multipart.encoder import MultipartEncoder
- upload_payload['token']=get_edit_token(cookies)
- m=MultipartEncoder(fields=upload_payload)
- headers['Content-Type']=m.content_type
- upload3_response = requests.post(api_url, data=m, cookies=cookies,headers=headers)
- print(upload3_response.json())
- # FAIL again!
- #Sanity check headers sent:
- print(upload3_response.request.headers)
- #{'Connection': 'keep-alive', 'Cookie': 'forceHTTPS=true; commonswiki_BPsession=xxxxxxx', 'Accept-Encoding': 'gzip, deflate', 'User-Agent': 'Drudgebane Storkksbot (contact [[C:User:Storkk]]) Based on python requests/2.11.1', 'Content-Length': '38759', 'Accept': '*/*', 'Content-Type': 'multipart/form-data; boundary=a1eedfa09d2147efae0f62038dcc18d0', 'Content-Disposition': 'form-data; name="file"; filename="storkk\'s test upload.jpg"'}
- # requests documentation mentions sending files in as separate keyword (not sure how this interacts with MediaWikia
- # http://requests.readthedocs.io/en/latest/user/quickstart/#post-a-multipart-encoded-file
- upload_payload={'action': 'upload',
- 'format':'json',
- 'filename':REMOTENAME,
- # 'file':CONTENTS,
- 'comment':'test upload',
- 'text':'apple test upload \n\n{{OGL2}}\n\nPlease contact [[User:Storkk]] if there are issues',
- 'token':get_edit_token(cookies)}
- files={'file': (REMOTENAME, CONTENTS)}
- upload4_response=requests.post(api_url, data=upload_payload,files=files,cookies=cookies,headers=headers)
- print(upload3_response.text)
- # FAIL again, and the api won't even return JSON - and I am out of ideas >o(
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement