What am I missing? (PowerShell script for image selections)

Started by RobiWan, March 02, 2024, 11:05:55 PM

Previous topic - Next topic

RobiWan

Hi,

I am trying to create a selection of images in Imatch using PowerShell. I want to pass a text file as a parameter to my script and the images should appear in IMatch as a selection (new window).
I can't figure out how to pass the images to searches/files. With idlist you can only pass the internal id, which I don't have at the moment.

$authenticate = Invoke-RestMethod -Uri "http://127.0.0.1:50519/v1/authenticate" -Method Post -Body @{'id'= $env:USERNAME;'password'='';'appid'=''}
$auth_token = $authenticate.auth_token
Invoke-RestMethod -Uri "http://127.0.0.1:50519/v1/search/filename" -Method GET -Body @{'idlist' = '@IMatch.idlist.fileWindowFilesTotal'; 'scope' = "database"; 'fields' = 'id';'auth_token'=$auth_token; "advancedmode" = $False ;"pattern"="ANY"}

Mario

This makes no sense. GET requests have no body, the parameters are in the URL.
The scope tells IMatch where to search. You set it to 'database' so IMatch searches the entire database. So your idlist parameter is ignored. fields tells IMatch which fields to return in the result.

If you want to search the entire database for files starting with the word beach in the file name and receive the ids of all matching files, this is the URL you need to build (assuming you use IMatch, an empty auth_token is OJK:

http://127.0.0.1:50519/v1/search/filename?auth_token=&scope=database&pattern=beach.*&fields=id
You can copy & paste this into your browser (while IMatch is running) to see it working.

This returns a JSON-formatted response like this:

{"files":[13,11],"hiddenByStacks":0}
Then you can use imatch/resultwindow/open to open the resulting files in a new result window, by providing the ids of the files to include. If there are too many files in the result returned by IMWS, create an idlist from them in memory using the appropriate endpoint and then open the result window by specifying this idlist.
-- Mario
IMatch Developer
Forum Administrator
http://www.photools.com  -  Contact & Support - Follow me on 𝕏 - Like photools.com on Facebook

RobiWan

Hi Mario,

thanks for your answer.
I must have expressed myself incorrectly. I have a file in the form:
C:\DSLR\DSLR-RAW\ExtHDD 2506\OMDEM1X\2019\03\23\_rr04995.orf
C:\DSLR\DSLR-RAW\ExtHDD 2506\OMDEM1X\2019\03\23\_rr04999.orf
C:\DSLR\DSLR-RAW\ExtHDD 2506\OMDEM1X\2019\03\23\_rr05000.orf
C:\DSLR\DSLR-RAW\ExtHDD 2506\OMDEM1X\2019\03\23\_rr05001.orf
C:\DSLR\DSLR-RAW\ExtHDD 2506\OMDEM1X\2019\03\23\_rr05002.orf
C:\DSLR\DSLR-RAW\ExtHDD 2506\OMDEM1X\2019\03\23\_rr05003.orf
C:\DSLR\DSLR-RAW\ExtHDD 2506\OMDEM1X\2019\03\23\_rr05004.orf

All Pictures are already in the database and cataloged.
And I want IMacth to show me these files as search results in a new window.
In other words, in a similar way to "File search", but as a file rather than the clipboard.
There are a lot of things in the documentation, but somehow I can't find anything that could help me with my problem. Maybe it doesn't work at all and that's why I can't find anything about it.

Robert

Mario

The File Finder app does what you want to do.

Basically it fetches the file names the user has entered into the input field and copies them into an array.
Then it retries the id and file name of all files in the current scope or database (depending on the selected option) via the corresponding idlist.

For the bulk match, it iterates over each file name returned and compares it to the file names entered by the user. If a match is found, the file information (id and file name) is added to an "matches" array.

After all files have been processed, the files in the matches are opened in a new result window with openResultWindow().

As usual, there is a lot of code that only deals with user interface updates, progress bar, user cancellation and whatnot. You can ignore all that.

The app processes files in batches of 5000, since asking IMWS to return information for all files for a database with 200,000 files would take very long, and probably break the browser.
So it fetches data about 5000 files using the smart paging provided by IMWS (see files endpoint), compares the file names of all 5000 files with the file names entered by the user (in your case, the contents of your text file) and adds matches to an array.
When all files have been processed, it finally opens the result window with the matches.

I've made a short example in JavaScript. Adapt to the programming language of your choice.


const PAGE_SIZE = 5000;

// The contents of your text file, converted to a string array.
let filenames = [
    'c:\\data\\imdata\\Market\\PTC-AM-891708.jpg',
    'c:\\data\\imdata\\Market\\PTC-AM-891709.jpg',
    'c:\\data\\imdata\\Travel\\Amsterdam\\flower-69490.jpg',
];
let matches = [];
let page = 0;
while (true) {
    const result = await IMWS.get('v1/files',{
        page: page,
        pagesize: PAGE_SIZE,
        fields: 'id,filename'
    });

    for (const file of result.files) {
        for (const name of filenames) {
            if (file.fileName.toLowerCase() == name.toLowerCase()) {
                matches.push(file.id);
                break;
            }
        }
    }

    // All files processed?
    if (result.files.length < PAGE_SIZE) {
        break;
    }
   
    // Else repeat with the next page of files.
    page++;
    console.log(`Page ${page}`);
}

console.log(JSON.stringify(matches, null, 2));

This code iterates over all files in the database in batches of 5000. It retries the file name and id, compares it to the list of file names to search for and adds the id of matches to the matches array.

When all is done, the matches are printed on the console. You would instead use the matches to open a result window.
-- Mario
IMatch Developer
Forum Administrator
http://www.photools.com  -  Contact & Support - Follow me on 𝕏 - Like photools.com on Facebook

RobiWan

Quote from: Mario on March 03, 2024, 11:22:43 AMI've made a short example in JavaScript.
Is there an advantage to using JavaScript with Imatch?
I don't know JavaScript at the moment, but it won't be that complicated.

Robert

Mario

All IMatch apps, the Event View, People View and the Dashboard are written in JavaScript/TypeScript.
You can use any language you like, from C++ to Python to PowerShell if you don't need an user interface for apps. Or node.js if you want to run JavaScript. That's the beauty of how IMatch Anywhere WebServices works - it is language agnostic. Sometimes even a web browser or curl on the command line is sufficient to "run" an URL and fetch the results.

I've used JavaScript because this allowed me to cobble together an example of what you want to do from stuff I already have.
It's short and easy to understand and you can translate it into any other programming language.
If you want to use a PowerShell script, that's fine.
Just adapt the code I've provided, e.g. run a loop to fetch 5000 file names at a time, match them, store the ids of matches in an array and finally call the endpoint to open the files in a result window.
-- Mario
IMatch Developer
Forum Administrator
http://www.photools.com  -  Contact & Support - Follow me on 𝕏 - Like photools.com on Facebook

RobiWan

I think I have to ask another question.

How do I call the function openResultWindow() via e.g. PowerShell? I don't see it in the documentation. If I have not overlooked anything, it is an Imatch and not IMWS function.


Mario

This is a function in the IMatchLib.js JavaScript library I include in IMatch to make things more comfortable for app developers.

If you look at the source code of this function (C:\ProgramData\photools.com\IMatch6\webroot\system\imatch\imatchlib.js), you see that it does a POST request to v1/imatch/resultwindow/open, providing the required parameters.

The documentation explains what each parameter means:

Image3.jpg

Since this is a POST, the parameters go into the body. I suggest you use one of the apps that uses this function (File Finder app, for example), open it in your web browser (from the App Manager) and after opening the developer tools in your browser with <F12> check the "Network" tab to see what the function send to IMatch to open a result windows.
Then mimic this in whatever programming language you use.

You'll see something like this:
name: Result Window Name
id: 17,18
auth_token:

as the "form data" or "payload" of a request that opens two files in a new result window with the title "Result Window Title". It's quite simple, actually.

-- Mario
IMatch Developer
Forum Administrator
http://www.photools.com  -  Contact & Support - Follow me on 𝕏 - Like photools.com on Facebook

RobiWan

That was easy. Sometimes you just don't come up with the simple things.

Thank you very much.

Mario

Very good.

I ship all IMatch system libraries and most apps with complete documented source code so IMatch app developers can look and learn. If you enable the Developer Mode under Edit > Preferences > Application, the App Manager shows additional sample scripts which are plain JavaScript and fully documented - demonstrating many IMWS endpoints.
And there are the code recipes, of course.

Given the dwindling number of IMatch users who can or are interested in programming apps, it makes no commercial sense anymore to document everything. My time is limited.
If a programmer has a specific questions, it is much cheaper and efficient for me to answer the question here in the community - compared to documenting everything and keeping the documentation up-to-date with every of the frequent changes.

Most users these days are just that - users. No offense ;)

The "older" generation usually had to learn a bit of programming out of necessity to get their hardware/software working. 
The "current" generation has grown up with apps and just works Windows PCs - which neither required a lot of tech know-how to get things to work nor programming know-how.

Also, IMatch has become a lot more functional and complete since the early days when writing scripts in Basic or in form of IMatch apps was required more often to get specific things done efficiently.

Still, a small PowerShell or Python script that accesses or manipulate IMatch database contents via IMWS can be "best" solution on many occasions. Or an IMatch app, if a user interface is required.
-- Mario
IMatch Developer
Forum Administrator
http://www.photools.com  -  Contact & Support - Follow me on 𝕏 - Like photools.com on Facebook

Jingo

Quote from: Mario on March 04, 2024, 07:01:22 PMAlso, IMatch has become a lot more functional and complete since the early days when writing scripts in Basic or in form of IMatch apps was required more often to get specific things done efficiently.
^^ This!  Often when I think of creating another APP, I stop to think is there a way to just do it within IMatch itself... and often - there sure is!