示例#1
0
def create_api_session():
    try:
        api = DatasetAPI(config['dkan']['uri'], config['dkan']['user'],
                         config['dkan']['password'])
        return api
    except Exception as e:
        return e
示例#2
0
def run(args):
    api = DatasetAPI(args.DkanHost, args.Token)
    if args.Action == 'Create':
        data = {'title': args.title, 'type': args.type}
        print("Attempting to create a new {}".format(args.type))
        dataset = api.node('create', data=data)
        print("Process Completed: ", dataset.status_code, dataset.text)
    elif args.Action == 'Attach_File':
        print("Attempting to Upload the file to the Dataset...")
        r = api.attach_file_to_node(args.filename, args.node_id,
                                    'field_upload')
        print("Process Completed: ", r.status_code, r.text)
    elif args.Action == 'Delete':
        print("Attempting to Delete ...")
        op = api.node('delete', node_id=args.node_id)
        print("Process Completed: ", op.status_code, op.text)
    elif args.Action == 'Update':
        print("Attempting to Update ...")
        r = api.node('update',
                     node_id=int(args.node_id),
                     data=eval(args.parameters))
        print("Process Completed: ", r.status_code, r.text)
    else:
        print("We only support 'Create', 'Upload', 'Delete' and 'Update'\
              at the moment")
def connect():
    global api
    if api:
        return ""

    try:
        logging.debug(_("DKAN-Login: %s @ %s"), config.dkan_username,
                      config.dkan_url)
        # Last parameter is debug mode: True = Debugging ON
        api = DatasetAPI(config.dkan_url, config.dkan_username,
                         config.dkan_password, True)
        return ""
    except LoginError as err:
        logging.error(_("Fehler bei Verbindung zur DKAN-Instanz!"))
        logging.error(_("Fehlermeldung %s"), str(err))
        logging.error(
            _("Bitte prüfen Sie die angegebenen DKAN-Url und -Zugangsdaten."))
        return "Fehler: " + str(err)
示例#4
0
def connect(args):
    global api
    print("DKAN url:", args.dkan_url)

    # Last parameter is debug mode: True = Debugging ON
    api = DatasetAPI(args.dkan_url, args.user, args.passw, True)
示例#5
0
import os
import json
from dkan.client import DatasetAPI

uri = os.environ.get('DKAN_URI', False)
user = os.environ.get('DKAN_USER', 'admin')
password = os.environ.get('DKAN_PASSWORD', 'admin')
token = os.environ.get('TOKEN')

if uri:
    # api = DatasetAPI(uri, user, password, True)
    api = DatasetAPI(uri, token)
    payload = {'parameters[type]': 'resource'}
    nodes = api.node(params=payload).json()
    resource = nodes[0]
    print resource
    csv = os.path.join(os.path.dirname(os.path.abspath(__file__)), '.', 'data',
                       'tension_sample_data.csv')
    # Attach the file to the resource node
    r = api.attach_file_to_node(csv, resource['nid'], 'field_upload')
    print r.status_code
    print r.text
    resource = api.node('retrieve', node_id=resource['nid'])
    print resource.json()['field_upload']
else:
    print 'Please Set the dkan URL as an ENVIRONMENT variable'
示例#6
0
import os
import json
from dkan.client import DatasetAPI

uri = os.environ.get('DKAN_URI', False)
user = os.environ.get('DKAN_USER', 'admin')
password = os.environ.get('DKAN_PASSWORD', 'admin')

if uri:
    api = DatasetAPI(uri, user, password, True)
    # Create dataset
    print 'CREATE DATASET'
    data = {
        'title': 'Test Dataset',
        'type': 'dataset'
    }
    r = api.node('create', data=data)
    print 'Response: %s\n\n' % r.text
    dataset_nid = r.json()['nid']
    # Create resource
    print 'CREATE RESOURCE'
    data = {
        'title': 'Test Resource',
        'type': 'resource',
        'field_dataset_ref': {
            'und': {
                '0': {
                    'target_id': dataset_nid,
                },
            },
            
示例#7
0
import os
import json
from dkan.client import DatasetAPI

uri = os.environ.get('DKAN_URI', False)
user = os.environ.get('DKAN_USER', 'admin')
password = os.environ.get('DKAN_PASSWORD', 'admin')

if uri:
  api = DatasetAPI(uri, user, password, True)
  data = {
      'title': 'Test Dataset',
      'type': 'dataset'
  }
  dataset = api.node('create', data=data)
  print dataset.status_code
  print dataset.text
  nid = dataset.json()['nid']
  op = api.node('delete', node_id=nid)
  print op.status_code
  print op.text
else:
  print 'Please Set the dkan URL as an ENVIRONMENT variable'
示例#8
0
#uri = os.environ.get('URI', False)
#uri = 'https://test.cadepttech.nucivicdata.com/node/1826/api'
#uri = 'https://test.cadepttech.nucivicdata.com/dataset/flow-targets-southern-california-streams/resource/d2f2657c-1fb4-4ae2-a31b-d4b80ccee3e8'
#uri = 'https://test.cadepttech.nucivicdata.com/api/action/datastore/search.json?resource_id=d2f2657c-1fb4-4ae2-a31b-d4b80ccee3e8'
#uri = 'https://data.ca.gov/node/1691/api'
#URI = 'https://data.ca.gov/'
#uriNode1721Test = 'https://data.ca.gov/api/dataset/node/1721'
#uri = 'https://test.cadepttech.nucivicdata.com/'

#with DatasetAPI(uri, user, password , True) as api:
#	print(api.node('index'))

# gives a list of anything available but on 20....!!!!
onA = "on"
if onA:
    api = DatasetAPI(URI, user, password, True)
    r = api.node('index')
    #print(r.json())
    columns = [desc for desc in r.json()[0]]
    rStack = np.vstack(r.json())
    df2 = pd.DataFrame(data=r.json(), columns=columns)
    print(df2)
    #df.to_csv("outTestTrash.csv", sep=',', encoding='utf-8')
else:
    print("BooHoo")

# only use to list dataset from
on = "on"
if on:
    api = DatasetAPI(URI, user, password, True)
示例#9
0
import os
import json
from dkan.client import DatasetAPI

uri = os.environ.get('DKAN_URI', False)
user = os.environ.get('DKAN_USER', 'admin')
password = os.environ.get('DKAN_PASSWORD', 'admin')
token = os.environ.get('TOKEN')

if uri:
  # api = DatasetAPI(uri, user, password, True)
  api = DatasetAPI(uri, token)
  data = {
      'title': 'Test Dataset',
      'type': 'dataset'
  }
  dataset = api.node('create', data=data)
  print dataset.status_code
  print dataset.text
  dataset = dataset.json()
  update = {
    'title': 'Test Dataset Updated',
  }
  r = api.node('update', node_id=dataset['nid'], data=update)
  print r.status_code
  print r.text
else:
  print 'Please Set the dkan URL as an ENVIRONMENT variable'
示例#10
0
import os
import json
from dkan.client import DatasetAPI

uri = os.environ.get('DKAN_URI', False)
user = os.environ.get('DKAN_USER', 'admin')
password = os.environ.get('DKAN_PASSWORD', 'admin')
token = os.environ.get('TOKEN')

print uri, token

if uri:
    api = DatasetAPI(uri, token)
    data = {'title': 'Test Dataset', 'type': 'dataset'}
    dataset = api.node('create', data=data)
    print dataset.status_code
    print dataset.text
else:
    print 'Please Set the dkan URL as an ENVIRONMENT variable'
示例#11
0
import os
import json

try:
    from dkan.client import DatasetAPI
except ImportError:
    import sys
    sys.path.append('../')
    from dkan.client import DatasetAPI

uri = os.environ.get('DKAN_URI', False)
user = os.environ.get('DKAN_USER', 'admin')
password = os.environ.get('DKAN_PASSWORD', 'admin')

if uri:
    api = DatasetAPI(uri, user, password, True)
    # Create dataset
    print('CREATE DATASET')
    data = {
        'title': 'Test Dataset',
        'type': 'dataset'
    }
    r = api.node('create', data=data)
    print('Response: %s\n\n' % r.text)
    dataset_nid = r.json()['nid']
    # Create resource
    print('CREATE RESOURCE')
    data = {
        'title': 'Test Resource',
        'type': 'resource',
        'field_dataset_ref': {
示例#12
0
import os
import json
from dkan.client import DatasetAPI

uri = os.environ.get('DKAN_URI', False)
user = os.environ.get('DKAN_USER', 'admin')
password = os.environ.get('DKAN_PASSWORD', 'admin')
token = os.environ.get('TOKEN')

if uri:
  # Make the api authenticate properly
  # api = DatasetAPI(uri, user, password, True)
  api = DatasetAPI(uri, token)

  # List datasets
  params = {
    'parameters[type]': 'dataset',
  }
  r = api.node(params=params)

  print r.json()
else:
  print 'Please Set the dkan URL as an ENVIRONMENT variable'
示例#13
0
import os
import json

try:
    from dkan.client import DatasetAPI
except ImportError:
    import sys
    sys.path.append('../')
    from dkan.client import DatasetAPI

uri = os.environ.get('DKAN_URI', False)
user = os.environ.get('DKAN_USER', 'admin')
password = os.environ.get('DKAN_PASSWORD', 'admin')

if uri:
    api = DatasetAPI(uri, user, password, True)
    data = {'title': 'Test Dataset', 'type': 'dataset'}
    dataset = api.node('create', data=data)
    print(dataset.status_code)
    print(dataset.text)
else:
    print('Please Set the dkan URL as an ENVIRONMENT variable')
示例#14
0
import os
import json

try:
    from dkan.client import DatasetAPI
except ImportError:
    import sys
    sys.path.append('../')
    from dkan.client import DatasetAPI

uri = os.environ.get('DKAN_URI', False)
user = os.environ.get('DKAN_USER', 'admin')
password = os.environ.get('DKAN_PASSWORD', 'admin')

if uri:
    api = DatasetAPI(uri, user, password, True)
    data = {'title': 'Test Dataset', 'type': 'dataset'}
    dataset = api.node('create', data=data)
    print(dataset.status_code)
    print(dataset.text)
    dataset = dataset.json()
    update = {
        'title': 'Test Dataset Updated',
    }
    r = api.node('update', node_id=dataset['nid'], data=update)
    print(r.status_code)
    print(r.text)
else:
    print('Please Set the dkan URL as an ENVIRONMENT variable')
示例#15
0
import os
import json
from dkan.client import DatasetAPI

uri = os.environ.get('DKAN_URI', False)
user = os.environ.get('DKAN_USER', 'admin')
password = os.environ.get('DKAN_PASSWORD', 'admin')

if uri:
  # Make the api authenticate properly
  api = DatasetAPI(uri, user, password, True)

  # List datasets
  params = {
    'parameters[type]': 'dataset',
  }
  r = api.node(params=params)

  print r.json()
else:
  print 'Please Set the dkan URL as an ENVIRONMENT variable'
示例#16
0
import os
import json
from dkan.client import DatasetAPI

uri = os.environ.get('DKAN_URI', False)
user = os.environ.get('DKAN_USER', 'admin')
password = os.environ.get('DKAN_PASSWORD', 'admin')

if uri:
  api = DatasetAPI(uri, user, password, True)
  payload = {'parameters[type]': 'resource'}
  nodes = api.node(params=payload).json()
  resource = nodes[0]
  print resource
  csv = os.path.join(os.path.dirname(os.path.abspath(__file__)), '.',
                     'data', 'tension_sample_data.csv')
  # Attach the file to the resource node
  r = api.attach_file_to_node(csv, resource['nid'], 'field_upload')
  print r.status_code
  print r. text
  resource = api.node('retrieve', node_id=resource['nid'])
  print resource.json()['field_upload']
else:
  print 'Please Set the dkan URL as an ENVIRONMENT variable'  
示例#17
0
    cursor.execute(sql)
    columns = [desc[0] for desc in cursor.description]
    with open(file, 'w', newline='', encoding='utf8') as writer:
        dw = csv.DictWriter(writer,
                            fieldnames=columns,
                            delimiter=sep,
                            lineterminator='\n')
        dw.writeheader()
        FHAB_writer = csv.writer(writer,
                                 csv.QUOTE_MINIMAL,
                                 delimiter=sep,
                                 lineterminator='\n')
        for row in cursor:
            row = [str(word) if word is not None else '' for word in row]
            filtered = [decodeAndStrip(t) for t in list(row)]
            newDict = dict(zip(columns, filtered))
            try:
                long = float(newDict['Longitude'])
                if long > 0:
                    newDict['Longitude'] = -long
            except ValueError:
                pass
            FHAB_writer.writerow(list(newDict.values()))
    # 2446 FHAB portal data (previously 2156)
    NODE = 2446
    api = DatasetAPI(URI, user, password, debug=False)
    r = api.attach_file_to_node(file=file,
                                node_id=NODE,
                                field='field_upload',
                                update=0)
示例#18
0
import os
import json
from dkan.client import DatasetAPI

uri = os.environ.get('DKAN_URI', False)
user = os.environ.get('DKAN_USER', 'admin')
password = os.environ.get('DKAN_PASSWORD', 'admin')

if uri:
  api = DatasetAPI(uri, user, password, True)
  data = {
      'title': 'Test Dataset',
      'type': 'dataset'
  }
  dataset = api.node('create', data=data)
  print dataset.status_code
  print dataset.text
  dataset = dataset.json()
  update = {
    'title': 'Test Dataset Updated',
  }
  r = api.node('update', node_id=dataset['nid'], data=update)
  print r.status_code
  print r.text
else:
  print 'Please Set the dkan URL as an ENVIRONMENT variable'
示例#19
0
import os
import json
from dkan.client import DatasetAPI

uri = os.environ.get('DKAN_URI', False)
user = os.environ.get('DKAN_USER', 'admin')
password = os.environ.get('DKAN_PASSWORD', 'admin')

if uri:
    api = DatasetAPI(uri, user, password, True)
    data = {'title': 'Test Dataset', 'type': 'dataset'}
    dataset = api.node('create', data=data)
    print dataset.status_code
    print dataset.text
    nid = dataset.json()['nid']
    op = api.node('delete', node_id=nid)
    print op.status_code
    print op.text
else:
    print 'Please Set the dkan URL as an ENVIRONMENT variable'
示例#20
0
import os
import json
from dkan.client import DatasetAPI

uri = os.environ.get('DKAN_URI', False)
user = os.environ.get('DKAN_USER', 'admin')
password = os.environ.get('DKAN_PASSWORD', 'admin')
token = os.environ.get('TOKEN')

if uri:
  # api = DatasetAPI(uri, user, password, True)
  api = DatasetAPI(uri, token)
  data = {
      'title': 'Test Dataset',
      'type': 'dataset'
  }
  dataset = api.node('create', data=data)
  print dataset.status_code
  print dataset.text
  nid = dataset.json()['nid']
  op = api.node('delete', node_id=nid)
  print op.status_code
  print op.text
else:
  print 'Please Set the dkan URL as an ENVIRONMENT variable'