Python Sketchfab API Example not working

api
upload

(Poiu 1 2000) #1

Hi there,

I am trying to get an automated model upload via my own python tool (with python 3.6.4.).
I used the example from the data api python examples.

FYI: I have a basic account and tested the upload from blender via the exporter, which worked.

I added my API token and provided a zip file to the upload method.

I had to fix the code to work with python 3.
I added the () to the print statements and removed the u prefix.
e.g.
old: print u'An error occured: {}'.format(e)
new: print('An error occured: {}'.format(e))

line 105 gave me a hard time and confused me a lot.
I always got the debug error: json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
I think its because the response is not parsable as JSON, since it has html content.
I changed it from:
old: print('Upload failed with error: {}'.format(r.json()))
new: print('Upload failed with error: {}'.format(r.reason))
which then gave me at least the message:
Upload failed with error: Request Entity Too Large

But now I am stuck.
What’s the issue? The Zip file is only 7 MB.
What could be wrong?

Here is the full code:
(I removed all unneeded functions, comments and links (since this is my first post)

#!/usr/bin/python

# -*- coding: utf-8 -*-

##
# Sample script for uploading to Sketchfab
# using the V3 API and the requests library
##
import json
import os

from time import sleep

import requests


SKETCHFAB_DOMAIN = 'sketchfab[dot]com'
SKETCHFAB_API_URL = 'https[semicolon]//api.{}/v3'.format(SKETCHFAB_DOMAIN)

YOUR_API_TOKEN = ' - my token (removed for this post)- '

def _get_request_payload(data={}, files={}, json_payload=False):
    """Helper method that returns the authentication token and proper content
    type depending on whether or not we use JSON payload."""
    headers = {'Authorization': 'Token {}'.format(YOUR_API_TOKEN)}

    if json_payload:
        headers.update({'Content-Type': 'application/json'})
        data = json.dumps(data)

    return {'data': data, 'files': files, 'headers': headers}


def upload(model_file):
    """POST a model to sketchfab.
    This endpoint only accepts formData as we upload a file.
    Parameter: model_file - relative path string to the zip file to upload
    """
    model_endpoint = os.path.join(SKETCHFAB_API_URL, 'models')

    # Optional parameters
    name = 'test'
    description = 'test'
    #password = 'my-password'  # requires a pro account
    #private = 1  # requires a pro account
    tags = ['test']  # Array of tags
    #categories = ['gaming']  # Array of categories slugs
    #license = 'CC Attribution'  # License label
    isPublished = False, # Model will be on draft instead of published
    isInspectable = True, # Allow 2D view in model inspector

    data = {
        'name': name,
        'description': description,
        'tags': tags,
        'isPublished': isPublished,
        'isInspectable': isInspectable
    }

    f = open(model_file, 'rb')

    files = {'modelFile': f}

    print ('Uploading ...')

    try:    
        r = requests.post(
            model_endpoint, **_get_request_payload(
                data, files=files))
    except requests.exceptions.RequestException as e:
        print ('An error occured: {}'.format(e))
        return
    finally:
        f.close()

    if r.status_code != requests.codes['created']:
        print('Upload failed with error: {}'.format(r.reason))
        return

    model_url = r.headers['Location']
    print ('Upload successful. Your model is being processed.')
    print ('Once the processing is done, the model will be available at: {}'.format(model_url))

    return model_url



if __name__ == '__main__':
    modelFile = os.path.abspath('./data/Drone.zip')
    model_url = upload(modelFile)

    if model_url:
        print('Model URL: ' + model_url)

#2

Thanks for the report. We’re looking into it!


(Poiu 1 2000) #3

any news?


#4

Sorry, I didn’t get any information yet. I’ll ping our developers again.