Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Paste
P58902
upload_file_from_url.py
Active
Public
Actions
Authored by
Joe
on Mar 23 2024, 10:38 AM.
Edit Paste
Archive Paste
View Raw File
Subscribe
Mute Notifications
Award Token
Flag For Later
Tags
None
Referenced Files
F43137186: upload_file_from_url.py
Mar 23 2024, 10:38 AM
2024-03-23 10:38:15 (UTC+0)
Subscribers
None
#!/usr/bin/env python3
"""
upload_file_from_url.py
MediaWiki API Demos
Demo of `Upload` module: Post request to upload a file from a URL
Slightly modified to account for async processing
MIT license
"""
import
os
import
requests
import
sys
url
=
sys
.
argv
[
1
]
name
=
sys
.
argv
[
2
]
S
=
requests
.
Session
()
URL
=
"http://localhost:4000/api.php"
username
=
os
.
getenv
(
'WIKI_USERNAME'
)
password
=
os
.
getenv
(
'WIKI_PASSWORD'
)
# Step 1: Retrieve a login token
PARAMS_1
=
{
"action"
:
"query"
,
"meta"
:
"tokens"
,
"type"
:
"login"
,
"format"
:
"json"
}
R
=
S
.
get
(
url
=
URL
,
params
=
PARAMS_1
)
DATA
=
R
.
json
()
LOGIN_TOKEN
=
DATA
[
"query"
][
"tokens"
][
"logintoken"
]
# Step 2: Send a post request to login. Use of main account for login is not
# supported. Obtain credentials via Special:BotPasswords
# (https://www.mediawiki.org/wiki/Special:BotPasswords) for lgname & lgpassword
PARAMS_2
=
{
"action"
:
"login"
,
"lgname"
:
username
,
"lgpassword"
:
password
,
"format"
:
"json"
,
"lgtoken"
:
LOGIN_TOKEN
}
R
=
S
.
post
(
URL
,
data
=
PARAMS_2
)
# Step 3: While logged in, retrieve a CSRF token
PARAMS_3
=
{
"action"
:
"query"
,
"meta"
:
"tokens"
,
"format"
:
"json"
}
R
=
S
.
get
(
url
=
URL
,
params
=
PARAMS_3
)
DATA
=
R
.
json
()
CSRF_TOKEN
=
DATA
[
"query"
][
"tokens"
][
"csrftoken"
]
# Step 4: Post request to upload a file from a URL
PARAMS_4
=
{
"action"
:
"upload"
,
"filename"
:
name
,
"url"
:
url
,
"format"
:
"json"
,
"token"
:
CSRF_TOKEN
,
"ignorewarnings"
:
1
,
"async"
:
1
}
R
=
S
.
post
(
URL
,
data
=
PARAMS_4
)
DATA
=
R
.
json
()
print
(
DATA
)
from
time
import
sleep
while
DATA
[
"upload"
][
"result"
]
==
"Poll"
:
PARAMS_4
[
"checkstatus"
]
=
True
R
=
S
.
post
(
URL
,
data
=
PARAMS_4
)
DATA
=
R
.
json
()
print
(
DATA
)
sleep
(
5
)
print
(
DATA
Event Timeline
Joe
created this paste.
Mar 23 2024, 10:38 AM
2024-03-23 10:38:15 (UTC+0)
Joe
mentioned this in
T295007: Upload by URL should use the job queue, possibly chunked with range requests
.
Mar 23 2024, 10:40 AM
2024-03-23 10:40:23 (UTC+0)
Log In to Comment