Advertisement
Guest User

Untitled

a guest
Nov 12th, 2016
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.00 KB | None | 0 0
  1. import requests
  2. import json
  3.  
  4.  
  5. USER,PASS=u'Botname@instancename',u'[[Special:BotPassword]]'
  6. REMOTENAME="storkk's test upload.jpg"
  7. FILENAME='localfile'
  8. with open(FILENAME,'rb') as f:
  9.     CONTENTS=f.read()
  10.  
  11. api_url = 'https://commons.wikimedia.org/w/api.php'
  12.  
  13. #get login token and log in
  14. payload = {'action': 'query', 'format': 'json', 'utf8': '',
  15.        'meta': 'tokens', 'type': 'login'}
  16.  
  17. r1 = requests.post(api_url, data=payload)
  18. login_token=r1.json()['query']['tokens']['logintoken']
  19.  
  20. payload = {'action': 'login', 'format': 'json', 'utf8': '',
  21.        'lgname': USER, 'lgpassword': PASS, 'lgtoken': login_token}
  22.  
  23. r2 = requests.post(api_url, data=payload, cookies=r1.cookies)
  24. cookies=r2.cookies.copy()
  25. assert r2.json()['login']['result']=='Success'
  26.  
  27. def get_edit_token(cookies):
  28.     token_response=requests.post(api_url, data={'action': 'query',
  29.                             'format': 'json',
  30.                             'meta': 'tokens'}, cookies=cookies)
  31.     return token_response.json()['query']['tokens']['csrftoken']
  32.  
  33. token=get_edit_token(cookies)
  34.  
  35.  
  36. # Attempt 1 -- the local file is a duplicate, so this should fail or warn as a duplicate
  37. #   (it's [[:File:Rhode_Island_Greenings-18d446.jpg]] )
  38. # It should not fail due to a malformed request
  39.  
  40. upload_payload={'action': 'upload',
  41.         'format':'json',
  42.         'filename':REMOTENAME,
  43.         'file':CONTENTS,
  44.         'comment':'test upload (should fail/warn of duplicate)',
  45.         'text':'apple test upload\n\n{{OGL2}}\n\nPlease contact [[User:Storkk]] if there are issues',
  46.         'token':token}
  47. headers={'Content-Type': 'multipart/form-data', 'User-Agent': 'Drudgebane Storkksbot (contact [[C:User:Storkk]]) Based on python requests/2.11.1'}
  48.  
  49. upload_response = requests.post(api_url, data=upload_payload, cookies=cookies,headers=headers)
  50. print(upload_response.json())
  51. # FAIL :
  52. #'code': 'badupload_file',
  53. #'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.'
  54.  
  55.  
  56. # For some reason after each failure I need to get a new token
  57. # or the api seems to start hanging and returning the HTML API help:
  58.  
  59. upload_payload['token']=get_edit_token(cookies)
  60.  
  61. # add Content-Disposition:
  62. headers['Content-Disposition']='form-data; name="file"; filename="{}"'.format(REMOTENAME)
  63. upload2_response=requests.post(api_url, data=upload_payload,cookies=cookies,headers=headers)
  64.  
  65. print(upload2_response.json())
  66. # FAIL (identical error)...
  67.  
  68.  
  69. # Attempt new method (using MultipartEncoder)
  70. from requests_toolbelt.multipart.encoder import MultipartEncoder
  71.  
  72. upload_payload['token']=get_edit_token(cookies)
  73.  
  74. m=MultipartEncoder(fields=upload_payload)
  75. headers['Content-Type']=m.content_type
  76.  
  77. upload3_response = requests.post(api_url, data=m, cookies=cookies,headers=headers)
  78. print(upload3_response.json())
  79. # FAIL again!
  80.  
  81. #Sanity check headers sent:
  82. print(upload3_response.request.headers)
  83.  
  84. #{'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"'}
  85.  
  86.  
  87. # requests documentation mentions sending files in as separate keyword (not sure how this interacts with MediaWikia
  88. # http://requests.readthedocs.io/en/latest/user/quickstart/#post-a-multipart-encoded-file
  89. upload_payload={'action': 'upload',
  90.         'format':'json',
  91.         'filename':REMOTENAME,
  92.         # 'file':CONTENTS,
  93.         'comment':'test upload',
  94.         'text':'apple test upload \n\n{{OGL2}}\n\nPlease contact [[User:Storkk]] if there are issues',
  95.         'token':get_edit_token(cookies)}
  96.  
  97. files={'file': (REMOTENAME, CONTENTS)}
  98.  
  99. upload4_response=requests.post(api_url, data=upload_payload,files=files,cookies=cookies,headers=headers)
  100. print(upload3_response.text)
  101. # 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