CKAN API

De TheNets Wiki
Ir para: navegação, pesquisa

Command Line Interface

#TODO

Python

Como instalar

É necessário instalar a biblioteca do github.com/ckan/ckanapi.

pip install ckanapi

Como inserir um 'package' e um 'resource'

 1 # createPackage.py
 2 
 3 import datetime
 4 from ckanapi import RemoteCKAN
 5 ua = 'ckanapiexample/1.0 (+http://example.com/my/website)'
 6 
 7 # CKAN Portal and API key
 8 api_key = "PUT_YOUR_APIKEY_HERE"
 9 portal = RemoteCKAN('https://dados.inferno.br', user_agent=ua, apikey=api_key)
10 
11 # Add dataset
12 # ==========================
13 organization_name = 'unifei-institucional' # Main organization
14 dataset_metadata = {
15     # Main package data
16     'name':'example-unifei-api',
17     'title':'UNIFEI package example from API',
18     'notes':'Descrição aqui do package criado através da API.',
19     'version':'0.0.1',
20     'owner_org':organization_name,
21 
22     # Extra info
23     'extra':{
24         'departamento':'dsi'
25     },
26 
27     # Author data
28     'author':'Dante',
29     'author_email':'devil@maycry.com',
30 
31     # Maintainer data
32     'maintainer':'Kratos',
33     'maintainer_email':'godofwar@asgard.com',
34 
35     # Set private
36     'private':False,
37 
38     # Set as deleted
39     'state':'active'
40 }
41 
42 try: # Check if package exist
43     pkg = portal.action.package_show(id=dataset_metadata['name'])
44 except:
45     pkg = None
46 
47 if (pkg==None): # Create package
48     pkg = portal.action.package_create(**dataset_metadata)
49 else:
50     pkg = portal.action.package_update(**dataset_metadata)
51 
52 
53 # Add resources
54 # ==========================
55 resource_metadata = {
56     'package_id'    : dataset_metadata['name'],
57     'name'          : 'disciplinas-alto-indice-de-reprovacao',
58     'description'   : 'Relação das disciplinas com alto índice de reprovação.',
59     'format'        : 'CSV',
60     'url'           : 'https://cdn.dados.inferno.br/disciplinas-alto-indice-de-reprovacao.csv',
61     'created'       : datetime.datetime.now().isoformat(),
62     'last_modified' : datetime.datetime.now().isoformat()
63 }
64 
65 try: # Check if resource exist
66     resource = portal.action.resource_show(id=resource_metadata['name'])
67 except:
68     resource = None
69 
70 if (resource==None): # Create resource
71     resource = portal.action.resource_create(**resource_metadata)
72 else:
73     resource = portal.action.resource_update(**resource_metadata)