Community Wishlist Survey 2023/Multimedia and Commons/Provide example code for adding depict statements with a command line script

From Meta, a Wikimedia project coordination wiki

Provide example code for adding depict statements with a command line script

Discussion

Did some digging, and the page we'll probably want to improve is mw:Wikibase/API. It appears that pywikibot is also capable of adding structured data: phab:T213904, phab:T223796, release notes, proof of concept -FASTILY 22:48, 28 January 2023 (UTC)[reply]

Example for python3: commons:User:SchlurcherBot/commonsapiaddclaimsoauth (please read the wikitext, the page does not render well). Let me know if you need another working example. --Schlurcher (talk) 18:07, 29 January 2023 (UTC)[reply]
After a quick look: It adds copyright and license. But I am after an expample for getting the ID for a category and then adding the depict statement. For license and copyright the P-number are known and do not change, so they can be hard coded in the script. C.Suthorn (talk) 00:10, 30 January 2023 (UTC)[reply]
What does the ID for a category exactly mean? --Matěj Suchánek (talk) 16:52, 30 January 2023 (UTC)[reply]
Upload a picture of a blue-red stripped cat that is hunting an Uber car. While uploading you assign "Category:Images of blue-red cats hunting Uber cars". As uploader you have the knowledge, what depicts are useful. You can derive it (programmatically supported) from the category. A script would be able to fetch the Q-numbers and add the depicts to the image. Adding statements, if the Q-numbers are already known. C.Suthorn (talk) 22:50, 30 January 2023 (UTC)[reply]
Is there actually a way to do that? I.e., is the relation between the category and suitable entity IDs for depicts stored somewhere? Because I believe that's the key, and I believe there is not at the moment. Maybe bringing this up to c:Commons talk:Structured data can make it clear. By coincidence, a similar topic is being discussed there right now: c:Commons talk:Structured data#Using categories as a proxy for P180 depicts values. --Matěj Suchánek (talk) 09:13, 31 January 2023 (UTC)[reply]

@C.Suthorn: Could you elaborate a bit more on the requirements? I could provide an compliled "command line tool that adds depict statements" in case the above code would not work. The call would look like this:

APISchlurcherBot.exe "Username" "Password" "M66593822" "Q68"

This code would perform this edit: [1] Questions:

  • Windows or Linux (the code I have is Windows)
  • Is password provided in command line ok (only ok for non-shared PCs, but easier)
  • Is M id ok, or title of page better (M id easier, but conversion can be done as well)

If that fulfills the need, I can share an executable for this task. If more complex is envisioned, then not. --Schlurcher (talk) 10:46, 4 February 2023 (UTC)[reply]

While I could integrate such a tool (on Linux, and User,Passsword is no problem), I think, it would be better not to have a complete working software, but example code (which could be in a pseudo language, or basically any language like php, java, perl, python, c, whatever - as this is not about high performance operations like computing raytracing images, or implementing social scoring algorihms) for these basic tasks:
1 computing Q, M, and P numbers from a text string (like page name to Q number, ...)
2 setting an SDC value for a file
3 testing for values already set
From such examples other people with other tools could profit too C.Suthorn (talk) 12:47, 4 February 2023 (UTC)[reply]

Voting