Exemplo n.º 1
0
    def download_media(self, download_url, output_file=None):
        """
        Download a media and write it to a file if necessary.
        Otherwise returns a stream to it
        """

        # convert URL from unicode to ascii if needed
        if type(download_url).__name__ == 'unicode':
            download_url = str(download_url)

        curl = self._get_curl(download_url)

        if output_file == None:
            buf = StringIO.StringIO()
        else:
            buf = open(output_file, 'w')

        curl.setopt(pycurl.WRITEFUNCTION, buf.write)
        curl.perform()
        http_code = curl.getinfo(pycurl.HTTP_CODE)
        content_type = curl.getinfo(pycurl.CONTENT_TYPE)

        if not content_type:
            raise fotolia_api.ApiError(
                'Invalid response, no content type returned')

        if ';' in content_type:
            splitted_content_type = content_type.split(';')
            content_type = splitted_content_type[0]

        mime_type = content_type.split('/')
        if mime_type[0] == 'application':
            if mime_type[1] == 'json':
                if type(buf).__name__ == 'file':
                    res = json.loads(buf.read())
                    buf.close()
                    os.unlink(output_file)
                else:
                    res = json.loads(buf.getvalue())

                if 'error' in res:
                    error_code = 0
                    if 'code' in res:
                        error_code = res['code']

                    raise fotolia_api.ApiError(res['error'], error_code)

            raise fotolia_api.ApiError('Unknown API error')
        elif http_code != 200:
            raise fotolia_api.ApiError('Invalid response HTTP code: ' +
                                       str(http_code))

        if type(buf).__name__ == 'file':
            buf.close()
        else:
            return buf
Exemplo n.º 2
0
    def _api(self, method, args = {}, auto_refresh_token = True):
        """
        Generic handler for Fotolia REST API
        """

        if self._is_post_method(method):
            query = None
            post_data = args
        else:
            query = args
            post_data = None

        uri = self._get_full_uri(method, query)
        curl = self._get_curl(uri, post_data, auto_refresh_token)

        buf = StringIO.StringIO()
        curl.setopt(pycurl.WRITEFUNCTION, buf.write)

        curl.perform()
        http_code = curl.getinfo(pycurl.HTTP_CODE)

        res = json.loads(buf.getvalue())

        if 'error' in res or http_code != 200:
            error_code = 0
            if 'error' in res:
                error_msg = res['error']
                if 'code' in res:
                    error_code = int(res['code'])
            else:
                error_msg = 'Invalid response HTTP code: ' + str(http_code)

            raise fotolia_api.ApiError(error_msg, error_code)

        return res
Exemplo n.º 3
0
    def create_user(self, properties):
        """
        Create a user
        """

        required_properties = ['login', 'password', 'email', 'language_id']

        for required_property in required_properties:
            if required_property not in properties.keys():
                fotolia_api.ApiError('Missing required property: ' +
                                     required_property)

        return self._api('createUser', properties)
Exemplo n.º 4
0
    def get_sales_data(self, sales_type = 'all', offset = 0, limit = 50, id = None, sales_day = None):
        """
        This method returns sales data for logged user.
        """

        valid_sales_types = [
            'all',
            'subscription',
            'standard',
            'extended'
        ]

        if not sales_type in valid_sales_types:
            fotolia_api.ApiError('Undefined sales type: ' + sales_type)

        params = {
            'sales_type': sales_type,
            'offset': offset,
            'limit': limit,
            'sales_day': sales_day,
            'id': id
            }

        return self._api('getSalesData', params)