@@ -35,9 +35,42 @@ def get(self) -> IO:
3535 file = self .osw .site ._site .images [self .title ]
3636 # return file.download() # in-memory - limited by available RAM
3737
38- response = self .osw .site ._site .connection .get (
39- file .imageinfo ["url" ], stream = True
40- )
38+ # use web api
39+ full_title = f"{ self .namespace } :{ self .title } "
40+ web_api_failed = False
41+ try :
42+ url = f"{ self .osw .site ._site .scheme } ://{ self .osw .site ._site .host } { self .osw .site ._site .path } api.php?action=download&format=json&title={ full_title } "
43+ # print("Use web api: ", url)
44+ response = self .osw .site ._site .connection .get (url , stream = True )
45+ api_error = response .headers .get ("Mediawiki-Api-Error" )
46+ if api_error is not None :
47+ if api_error == "download-notfound" :
48+ # File does not exist
49+ raise Exception ("File does not exist: " + full_title )
50+ elif api_error == "badvalue" :
51+ # Extension FileApi not installed on the server
52+ web_api_failed = True
53+ elif api_error == "readapidenied" :
54+ # no read permissions on file
55+ response .status_code = 403
56+
57+ except Exception :
58+ web_api_failed = True
59+ if web_api_failed :
60+ # fallback: use direct download
61+ url = file .imageinfo ["url" ]
62+ print (
63+ "Extension FileApi not installed on the server. Fallback: use direct download from " ,
64+ url ,
65+ )
66+ response = self .osw .site ._site .connection .get (url , stream = True )
67+
68+ if response .status_code != 200 :
69+ raise Exception (
70+ "Download failed. Please note that bot or OAuth logins have in general no permission for direct downloads. Error: "
71+ + response .text
72+ )
73+
4174 # for chunk in response.iter_content(1024):
4275 # destination.write(chunk)
4376 # see https://stackoverflow.com/questions/16694907/download-large-file-in-python-with-requests
0 commit comments