Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F43137186
upload_file_from_url.py
No One
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Authored By
Joe
Mar 23 2024, 10:38 AM
2024-03-23 10:38:15 (UTC+0)
Size
1 KB
Referenced Files
None
Subscribers
None
upload_file_from_url.py
View Options
#!/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
File Metadata
Details
Attached
Mime Type
text/plain; charset=utf-8
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
15037655
Default Alt Text
upload_file_from_url.py (1 KB)
Attached To
Mode
P58902 upload_file_from_url.py
Attached
Detach File
Event Timeline
Log In to Comment