使用python更新CKAN中的资源 [英] Update resource in CKAN using python

查看:239
本文介绍了使用python更新CKAN中的资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在CKAN的一个阶段更新资源。我使用demo.ckan.org进行一些测试。

I'm trying to update a resource in one instace of CKAN. I'm using demo.ckan.org to make some test.

我可以使用 curl 创建和更新数据集的资源>,但使用python我不能这样做。

I'm able to create and update a resource for a dataset using curl, but using python I'm not be able to do this.

我的起点是这个链接:
http://docs.ckan.org/en/latest/maintaining/filestore.html#filestore-api

My start point is this link: http://docs.ckan.org/en/latest/maintaining/filestore.html#filestore-api

这是代码:

import requests
requests.post('http://0.0.0.0:5000/api/action/resource_create',
              data={"package_id":"my_dataset"},
              headers={"X-CKAN-API-Key": "21a47217-6d7b-49c5-88f9-72ebd5a4d4bb"},
              files=[('upload', file('/path/to/file/to/upload.csv'))])

我的卷曲代码工作正常 strong>,所以我试着适应它:

My curl code works ok, so I have tried to adapt it:

curl -X POST http://demo.ckan.org/api/3/action/resource_update  -d '{"id":"5b75fdf2-df9c-4a4f-bb28-d78ea7bc4e48", "url": "http://82.98.156.2/ckan_api/public_html/restaurantes.geojson", "name": "Better Restaurants", "format":"GEOJSON", "description":"Description of the resource"}' -H "Authorization: 97caad21-8632-4372-98fe-a24cdcaa90dc"

这应该是python中的代码:

This should be the code in python:

resource_dict = {
    'id': '5b75fdf2-df9c-4a4f-bb28-d78ea7bc4e48',
    'name':'REstaurantes con PYTHON',
    'url':'http://82.98.156.2/ckan_api/public_html/restaurantes.geojson',
    'description':'Description in  PYTHON'
}
resource_dict = urllib.quote(json.dumps(resource_dict))
requests.post('http://demo.ckan.org/api/3/action/resource_update',
              data=resource_dict,
              headers={"Authorization: 97caad21-8632-4372-98fe-a24cdcaa90dc"})

我发现这个旧链接:
< a href =http://stackoverflow.com/questions/24642317/create-ckan-dataset-using-ckan-api-and-python-requests-library>使用CKAN API和Python请求库创建CKAN数据集

最后建议添加一些信息,但我可以明白这一点。

At the end it suggest to add some information, but I can figure out to do it.

任何建议

推荐答案

不要打扰python-请求 - 这是最简单的python使用优秀的 ckanapi 库。例如

Don't bother with python-requests - it's easiest in python to use the excellent ckanapi library. e.g.

import ckanapi
ckan = ckanapi.RemoteCKAN('http://demo.ckan.org/', apikey='97caad21-8632-4372-98fe-a24cdcaa90dc', user_agent='ckanapi so test')
resource_dict = {
    'id': '5b75fdf2-df9c-4a4f-bb28-d78ea7bc4e48',
    'package_id': 'cdcf576d-0b09-4df0-a506-61a7142d2b8f',
    'name':'Restaurantes con PYTHON',
    'url':'http://82.98.156.2/ckan_api/public_html/restaurantes.geojson',
    'description':'Description in  PYTHON',
    'format':'GEOJSON'
}
ckan.action.resource_update(**resource_dict)

这篇关于使用python更新CKAN中的资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆