kanasimi / wikiapi

JavaScript MediaWiki API for node.js
https://kanasimi.github.io/wikiapi/
BSD 3-Clause "New" or "Revised" License
50 stars 6 forks source link

Failed to get local file while the path is correct. #25

Closed hugolpz closed 3 years ago

hugolpz commented 3 years ago

commons-upload.js:

// PURPOSE: Script uploads file 丁-red.png and similar to Commons.
const Wikiapi= require('wikiapi');
const fetch  = require('node-fetch');
const fs     = require('fs');
const logins = require('./logins-ShufaBot.js');
const files  = require('./data/zi-reds.js');     // PROVIDES THE CHARACTERS

// Edit login credentials
var USER = logins.commons.user,
    PASS = logins.commons.pass,
    API  = logins.commons.api;
    wikicode = '',
    zi='',
    path='';
    console.log('Username:', USER);

(async () => {
    // Connects
    const targetWiki = new Wikiapi;
    await targetWiki.login(USER, PASS, API);
    console.log('Connected!');
    // upload file / media
    for(i=0;i<files.length;i++){
        zi = files[i].zi,
        wikicode=`{{SOlicense|${zi}||red.png||license=PD}}`,
        path = `/home/yug/Documents/DragonsBot/reds/png/${zi}-red.png`;
        let result = await targetWiki.upload({ 
            file_path: path,
            comment: `Upload red stroke order for Chinese character ${zi}.`, 
            text: wikicode 
        });
    }
})();

Then:

$ node ./create-Commons-upload.js --unhandled-rejections=strict
Username: ShufaBot@ShufaBot
get_API_parameters: Set enwiki: path=query+siteinfo
get_API_parameters: Set commonswiki: path=query+siteinfo
Connected!
to_form_data: Failed to get file: [/home/yug/Documents/DragonsBot/reds/png/a-red.png]
(node:50492) UnhandledPromiseRejectionWarning: #<Object>
(Use `node --trace-warnings ...` to show where the warning was created)
(node:50492) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:50492) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

I copy-pasted that url into my Chrome and boom ! The images shows up. So the path is correct.

Example file, to save as 丁-red.png : 丁-red.png

ASCII file, to save as a-red.png : a-red.png

Both 丁-red.png and a-red.png returns the same error message, so it's not encoding.

hugolpz commented 3 years ago

After uploading the 2 files above to Github... Just for fun, I tried to change the file_path value from local path into the github url path https://user-images.githubusercontent.com/1420189/112379664-b9a79100-8ce8-11eb-981d-727884b32993.png . And.... it works ! See On Commons (if no admin delete it). Since the local path is correct, it's very likely an issue with local file reading.

I provided my whole code so you may easily test it if you wish.

EDIT: please note i am importing

//const fetch  = require('node-fetch'); // url
//const fs     = require('fs');   // local path

Which may mess up some of your code ?? EDIT: I shifted these two lines into comments and same error messages occurs.

kanasimi commented 3 years ago

The two situations are not the same. You may try{...}catch(e){console.error(e);} to see what happens.

hugolpz commented 3 years ago

Oh. I. See..... 🤩

1) commons-upload.js now returns :

to_form_data: Failed to get file: [./reds/png/a-red.png]
{
  code: 'missingparam',
  info: 'One of the parameters "filekey", "file" and "url" is required.',
  '*': 'See https://commons.wikimedia.org/w/api.php for API usage. Subscribe to the mediawiki-api-announce mailing list at &lt;https://lists.wikimedia.org/mailman/listinfo/mediawiki-api-announce&gt; for notice of API deprecations and breaking changes.'
}

On Commons's API > Upload we can read:

It tried some stuffs with fs.read() but doesn't work. So I'am lost. 😬 Your help is welcome to interpret the error above.

EDIT: move distinct rename error to distinct issue.

kanasimi commented 3 years ago

It should works now.

hugolpz commented 3 years ago

It does better but there is still a bump. If you are in Taiwan as I believe I prefer to report you this issue tomorrow. 👍🏼 🌃

kanasimi commented 3 years ago

OK. 😺 Please open a new issue.

hugolpz commented 3 years ago

I cleaned up the issue and split it. Done for today. I will open other issues later this week.

See #26 #27.