Python使用Ckan API创建CKAN的package

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/coffee_cream/article/details/52062848
#-*- coding: utf-8 -*-
__author__ = 'LILI'
import requests
import json
import pprint

CKAN_URL = 'http://default.yourckan.com'
APIkey = 'API Key'
OwnerOrg = 'Organization'
PackageName = 'test'
PackageTitle = 'test'

def create_package(APIkey, OwnerOrg, PackageName,PackageTitle):
    url = '{ckan}/api/3/action/package_create'.format(
            ckan=CKAN_URL)

    headers = {}
    headers['Content-Type'] = 'application/json'
    headers['X-CKAN-API-Key'] = APIkey
    headers['Authorization'] = APIkey
    headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0'

    dict = {}
    dict['name'] = PackageName
    dict['title'] = PackageTitle
    dict['owner_org'] = OwnerOrg
    dict = json.dumps(dict).encode('ascii')

    r = requests.post(url, headers=headers, data = dict)
    return r.status_code, r.text

if __name__ == '__main__':
    status_code, text = create_package(APIkey, OwnerOrg, PackageName,PackageTitle)
    pprint.pprint(status_code)
    pprint.pprint(text)

将代码最前面的CKAN_URLAPIkeyOwnerOrgPackageNamePackageTitle分别置换为自己的CKAN网址、自己的API key、package属于的organization、package的名字和标题。

猜你喜欢

转载自blog.csdn.net/coffee_cream/article/details/52062848