【问题标题】:Update resource in CKAN using python使用python更新CKAN中的资源
【发布时间】:2016-12-21 18:56:18
【问题描述】:

我正在尝试在 CKAN 的一个实例中更新资源。我正在使用 demo.ckan.org 进行一些测试。

我可以使用 curl 为数据集创建和更新资源,但使用 python 我无法做到这一点。

我的起点是这个链接: 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'))])

我的 curl 代码可以正常工作,所以我尝试对其进行调整:

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中的代码:

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"})

我找到了这个旧链接: Create CKAN dataset using CKAN API and Python Requests library

最后建议添加一些信息,但我可以想办法。

有什么建议吗???

【问题讨论】:

    标签: python curl python-requests ckan


    【解决方案1】:

    不要为 python 请求而烦恼 - 在 python 中使用优秀的 ckanapi 库是最简单的。例如

    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)
    

    【讨论】:

    • 效果很好!!,谢谢
    • 好的,请使用up-vote,然后选择最佳答案。
    • 我在计算时间,看看哪个选项更快,但两者都一样
    【解决方案2】:

    这似乎有效:

    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'}
    requests.post('http://demo.ckan.org/api/3/action/resource_update',
              json=resource_dict,
              headers={"Authorization": "97caad21-8632-4372-98fe-a24cdcaa90dc"})
    

    至少state_code200,我得到了"success": true 的回复。

    请注意,您的代码中的{"Authorization: 97caad21-8632-4372-98fe-a24cdcaa90dc"} 是数据类型class 'set',而headers 应该获取class 'dict' 的数据,例如"Authorization": "97caad21-8632-4372-98fe-a24cdcaa90dc"}

    【讨论】:

    • 太棒了!!,谢谢安德森
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-25
    • 1970-01-01
    • 1970-01-01
    • 2014-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多