Signbank / Global-signbank

An online sign dictionary and sign database management system for research purposes. Developed originally by Steve Cassidy/ This repo is a fork for the Dutch version, previously called 'NGT-Signbank'.
http://signbank.cls.ru.nl
BSD 3-Clause "New" or "Revised" License
19 stars 12 forks source link

Add API for NME Elements #1297

Closed rem0g closed 2 weeks ago

rem0g commented 1 month ago

For https://github.com/Signbank/Global-signbank/pull/1263 we would need to have new API connection to add and upload NME elements.

susanodd commented 1 month ago

[IGNORE THIS, ADDRESSED LATER]

The NME objects have an "offset" field (the order index of display) and multilingual descriptions (GlossVideoDescription objects related to the GlossVideoNME object)

The creation is implemented via the gloss update method, then an update function to update the offset and the description. Currently, you need to delete the video in Gloss Edit. Or you can update the fields. Or you can upload a new one.

So for the api urls there can be separate urls for each of these different edit functions. Or you could think of them as a set and then update the nme videos as a set?

Analogous to updating "Senses" (for all the senses) or updating the different keywords / translations in them. (We didn't offer the latter because their IDs are not visible to users.) But for the NME videos, the names of the videos follow a pattern for the gloss. With "_nme_N.mp4" as the suffix/extension

I have no idea yet what would be simpler.

Somehow if you want to update the description or offset, then you will need to be able to reference the NME video. So probably need them in the package / gloss retrieval API in order to have the respective file names. Although if you know how many there are for a gloss, or know the offset that would suffice to identify the objects. Analogous to how the sense numbers are used.

In Gloss Edit, it's not necessary to replace a video, you just upload a new one (it gets the offset automatically) and then can delete them too.

What form does the data (collection of NME videos) take that you want to use the API for? How do you see doing this? The dataset ID and the gloss ID are needed. Then if you're updating previous ones or uploading new ones.

susanodd commented 1 month ago

This is proving difficult to do.

I am having trouble with the video file in the "client side".

In spite of following instructions for doing so, when Django obtains the file, it thinks it's a dict instead of a file.

I need some help here. @Woseseltops I don't know how to pass an actual file inside of the json data.

susanodd commented 1 month ago

The examples I find are all using PHP with ajax. The FILES and form constructs won't work here because this isn't a template and it's not a form on the signbank side. Only html elements.

Okay, probably a Blob.

I'm struggling with the client-side "NON-Django" ajax call of the url inside of a Django-free html file with javascript, jquery and json. This seems totally primitive and annoying. Django gives us lots of stuff for free in the templates. But now, none of that is available on the client side.

We also had trouble when the API tokens were introduced because a header was needed in the AJAX client side. And to accomplished that, it needed to be disassembled on the server side to separate the header from the body. (Undocumented Django) So now, what to do if there is also a blob video inside the json on the client side? To decode the header/body on the Django side, it needed to be JSON.stringified and then decoded. But what if a blob is inside?

susanodd commented 1 month ago

I added the NME Videos to the package as a list of dictionaries. Here the ID of the GlossVideoNME object is exposed as it is needed by the API in order to update the index and description fields.

        "NME Videos": [
            {
                "ID": "21216",
                "Volgorde": "2",
                "Omschrijving: Nederlands": "pompoen",
                "Omschrijving: Engels": "pumpkin",
                "Link": "http://localhost:8000//dictionary/protected_media/glossvideo/tstMH/PO/POMPOEN-42087_nme_2.mp4"
            }
        ]

The Index/Volgorde is used to sequence them in the Gloss Detail. It also appears in the filename. So when updating it, the file will also be moved.

The other object IDs that are visible are the Gloss ID and the Lemma ID (in CSV for lemmas).

The above are shown inside the json of a particular gloss. The NME video has a link to its gloss. The gloss ID is on its filename.

Do we see any adverse side effects of exposing the ID?

The respective API operations on NME videos will need to include the ID.

Woseseltops commented 1 month ago

For completeness @susanodd , an example script to upload files via the API

import requests

# The URL to which you want to upload the file
url = 'https://signbank.cls.ru.nl/dictionary/ENDPOINT'

# The path to the file you want to upload
file_path = 'path/to/your/video.mp4'

# Open the file in binary mode
with open(file_path, 'rb') as file:
    # Define the files parameter
    files = {'file': (file_path, file, 'application/octet-stream')}

    # Send the POST request
    response = requests.post(url, files=files)

# Check the response
print(response.status_code)
print(response.text)
susanodd commented 1 month ago

For completeness @susanodd , an example script to upload files via the API

import requests

# The URL to which you want to upload the file
url = 'https://signbank.cls.ru.nl/dictionary/ENDPOINT'

# The path to the file you want to upload
file_path = 'path/to/your/video.mp4'

# Open the file in binary mode
with open(file_path, 'rb') as file:
    # Define the files parameter
    files = {'file': (file_path, file, 'application/octet-stream')}

    # Send the POST request
    response = requests.post(url, files=files)

# Check the response
print(response.status_code)
print(response.text)

I"m failing to see how an entire "json" dictionary of fields to update can include a file field in it.

The above looks like how the server already does this inside other places where files are uploaded.

The API in this case needs more data that just a file. It also needs the gloss and dataset, albeit in the url. Specific to the video, the index is needed and possibly descriptions. Otherwise there will need to be two operations sequentially to split the other fields off into a create followed by an update.

susanodd commented 1 month ago

@Woseseltops are you assuming with the above code that the user is going to upload a video to some upload location? Not the final destination? (i.e., that no object will be created yet)

As the saying goes, the left hand does not know what the right hand is doing.

susanodd commented 1 month ago

@rem0g this is working now for creation!!

I need to revise it some more so the video ID is used for the update. (The NME Video ID shown above retrieved in Package.)

I did not use the code of @Woseseltops I figured out how to do it with an ajax call.

rem0g commented 1 month ago

I will need instructions how to communiate via API to upload, adjust and remove NME elements. Can you fill them in Wiki?

susanodd commented 2 weeks ago

Added to Wiki.