user778806
user778806

Reputation: 77

(Python) Google Drive API: get mime type of file from id in a non-awkward way

I need to download Google Drive files that are attached to Google Classroom submissions.
From the Google Class "Submission" I get some information that does not include the mime type:

{
   "driveFile":{
       "id":"10NDXAyz8gkWzkXPS[removed]",
       "title":"git bash.PNG",
       "alternateLink":"https://drive.google.com/file/d/10NDXAyz8gkWzkX[removed]/view?usp=drive_web",
       "thumbnailUrl":"https://lh3.googleusercontent.com/nf4i[removed]=s200"
   }
}

If I understand correctly the mime type is needed to know which one is the right method to invoke for download, ie.

service.files().export_media(fileId=file_id, mimeType=export_mime_type

versus

service.files().get_media(fileId=file_id))

So far I have found only a very awkward way to get the mime type (code at the bottom).

This is to ask if there is, using the API, a less awkward way, I searched but cannot find it.
In my code splitting the ...thing in 2 functions is intentional, the awkwardness is in having to query by name, and them check the id. I wonder if there is some more appropriate "method" than files.list() to invoke.
If there isn't whether it is possible to query by id with it. I did not find that in the doc, tried it anyway but it did not work

def get_file_from_id_name(file_id, file_name, gdrive_service):
    """it seems I can only query by name, not by id"""
    page_token = None
    while True:
        query = "name='{}'".format(file_name)
        response = gdrive_service.files().list(spaces='drive', q = query, 
            fields='nextPageToken, files({})'.format(ALL_FILE_FIELDS),
            pageToken=page_token).execute()

        for file in response.get('files', []):
            if file.get('id') == file_id:
                return file
        page_token = response.get('nextPageToken', None)
        if page_token is None:
            break

    log.info("breakpoint")
    return None

def get_mime_type_from_id_name(file_id, file_name, gdrive_service):
    file_d = get_file_from_id_name(file_id, file_name, gdrive_service)
    mime_type = file_d.get("mimeType")
    return mime_type

Sorry for the long detailed question, tried to make it as concise as possible

Upvotes: 0

Views: 1584

Answers (2)

user778806
user778806

Reputation: 77

After some further unfruitful research it seems that there isn't a way to get the mime type from the API.
This inference is also supported by the fact that the API does not cover all the feature and data available, see for instance the current lack of API support for a very important feature like private comments (currently approved but not yet available)

Upvotes: 0

Gabriel Carballo
Gabriel Carballo

Reputation: 1343

I have seen that in Python you can use mimetypes.guess_type to retrieve the mimetype of a file. You can check this example so you can have an idea. This is how it was done in that scenario:

mimetype = MimeTypes().guess_type(name)[0]

Upvotes: 1

Related Questions