Skip to content

Commit

Permalink
Merge pull request #5 from keitaroinc/scripts
Browse files Browse the repository at this point in the history
Change api scripts
  • Loading branch information
blagojabozinovski authored Apr 16, 2024
2 parents 69dbd32 + 89db70f commit 344d530
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 81 deletions.
108 changes: 51 additions & 57 deletions ckanext/bulkupload/scripts/bulk_upload_xcaret.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,83 +2,77 @@
import requests
import ssl
from os import walk
from datetime import datetime
import configparser

ssl.match_hostname = lambda cert, hostname: True

config = configparser.ConfigParser()
config.read("config.ini")
FOLDER_PATH = config.get("configvars", "FOLDER_PATH")
API_TOKEN = config.get("configvars", "API_TOKEN")
DATASET_NAME = config.get("configvars", "DATASET_NAME")
DESCRIPTION = config.get("configvars", "DESCRIPTION")
SUBJECT = config.get("configvars", "SUBJECT")
CREATOR = config.get("configvars", "CREATOR")
PUBLISHER = config.get("configvars", "PUBLISHER")
CONTRIBUTOR = config.get("configvars", "CONTRIBUTOR")
DATE = config.get("configvars", "DATE")
TYPE = config.get("configvars", "TYPE")
IDENTIFIER = config.get("configvars", "IDENTIFIER")
SOURCE = config.get("configvars", "SOURCE")
LANGUAGE = config.get("configvars", "LANGUAGE")
RELATION = config.get("configvars", "RELATION")
COVERAGE = config.get("configvars", "COVERAGE")
RIGHTS = config.get("configvars", "RIGHTS")
MEDIUM = config.get("configvars", "MEDIUM")
SOURCE_OF_ACQUISITION = config.get("configvars", "SOURCE_OF_ACQUISITION")
ORGANIZATIONAL_DESCRIPTION = config.get("configvars", "ORGANIZATIONAL_DESCRIPTION")
PHYSICAL_TECHNICAL = config.get("configvars", "PHYSICAL_TECHNICAL")
LOCATION = config.get("configvars", "LOCATION")
RULES = config.get("configvars", "RULES")
imported_vars = configparser.RawConfigParser()
imported_vars.read('/home/blagoja/Projects/CKAN/2.9-test/scripts/config.ini')

folder_path = imported_vars.get('my_vars', "FOLDER_PATH")
api_token = imported_vars.get('my_vars', "API_TOKEN")
dataset_name = imported_vars.get("my_vars", "DATASET_NAME")
description = imported_vars.get("my_vars", "DESCRIPTION")
subject = imported_vars.get("my_vars", "SUBJECT")
creator = imported_vars.get("my_vars", "CREATOR")
publisher = imported_vars.get("my_vars", "PUBLISHER")
contributor = imported_vars.get("my_vars", "CONTRIBUTOR")
date = imported_vars.get("my_vars", "DATE")
type_field = imported_vars.get("my_vars", "TYPE")
identifier = imported_vars.get("my_vars", "IDENTIFIER")
source = imported_vars.get("my_vars", "SOURCE")
language = imported_vars.get("my_vars", "LANGUAGE")
relation = imported_vars.get("my_vars", "RELATION")
coverage = imported_vars.get("my_vars", "COVERAGE")
rights = imported_vars.get("my_vars", "RIGHTS")
medium = imported_vars.get("my_vars", "MEDIUM")
source_of_acquisition = imported_vars.get("my_vars", "SOURCE_OF_ACQUISITION")
organizational_description = imported_vars.get("my_vars", "ORGANIZATIONAL_DESCRIPTION")
physical_technical = imported_vars.get("my_vars", "PHYSICAL_TECHNICAL")
location = imported_vars.get("my_vars", "LOCATION")
rules = imported_vars.get("my_vars", "RULES")
date_modified = imported_vars.get("my_vars", "DATE_MODIFIED")

now = datetime.now()
now_date = str(now.date())
now_date = now_date.replace("-", "/")

f = []
mypath = FOLDER_PATH
mypath = folder_path
for (dirpath, dirnames, filenames) in walk(mypath):
list_of_files = [x for x in filenames]
break


ssl.match_hostname = lambda cert, hostname: True

url = "http://localhost:5000/api/3/action/resource_create"
url = "https://xtoria.grupoxcaret.com/api/3/action/resource_create"

headers = {
'Authorization': API_TOKEN,
'Authorization': api_token,
}

for y in list_of_files:
files = {
'package_id': (None, DATASET_NAME),
'package_id': (None, dataset_name),
'name': (None, y),
'url': (None, 'upload'),
'subject': (None, SUBJECT),
'description': (None, DESCRIPTION),
'author': (None, CREATOR),
'publisher': (None, PUBLISHER),
'contributor': (None, CONTRIBUTOR),
'date': (None, DATE),
'type': (None, TYPE),
'identifier': (None, IDENTIFIER),
'source': (None, SOURCE),
'language': (None, LANGUAGE),
'relation': (None, RELATION),
'coverage': (None, COVERAGE),
'rights': (None, RIGHTS),
'medium': (None, MEDIUM),
'source_of_acquisition': (None, SOURCE_OF_ACQUISITION),
'organization_description': (None, ORGANIZATIONAL_DESCRIPTION),
'physical_technical': (None, PHYSICAL_TECHNICAL),
'location': (None, LOCATION),
'rules': (None, RULES),
'date_of_descriptions': (None, now_date),
'subject': (None, subject),
'description': (None, description),
'author': (None, creator),
'publisher': (None, publisher),
'contributor': (None, contributor),
'date': (None, date),
'type': (None, type_field),
'identifier': (None, identifier),
'source': (None, source),
'language': (None, language),
'relation': (None, relation),
'coverage': (None, coverage),
'rights': (None, rights),
'medium': (None, medium),
'source_of_acquisition': (None, source_of_acquisition),
'organization_description': (None, organizational_description),
'physical_technical': (None, physical_technical),
'location': (None, location),
'rules': (None, rules),
'date_of_descriptions': (None, date_modified),
'upload': open(dirpath + "/" + y, 'rb'),
}

response = requests.post(url, headers=headers, files=files, verify=False)
print(response)

Expand Down
48 changes: 24 additions & 24 deletions ckanext/bulkupload/scripts/config.ini
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
[configvars]
FOLDER_PATH :'/path/to/upoad/files'
API_TOKEN : 'abc123454321cba'
DATASET_NAME : 'Dataset-name'
DESCRIPTION : 'Some description'
SUBJECT : 'Test Subject'
CREATOR : 'Joe Blogs'
PUBLISHER : 'Joe Publisher'
CONTRIBUTOR : 'Joe Contributor'
DATE : '01/01/2024' # Date format MM/DD/YYYY
TYPE : 'Resource'
IDENTIFIER : 'Value Identifier'
SOURCE : 'http://www.bing.com'
LANGUAGE : 'English'
RELATION : 'Value Relation'
COVERAGE : 'International'
RIGHTS : 'Value rights'
MEDIUM : 'Value Medium'
SOURCE_OF_ACQUISITION : 'Value Source of Aquisition'
ORGANIZATIONAL_DESCRIPTION : 'alphabetically'
PHYSICAL_TECHNICAL : 'Value Relevant physical conditions'
LOCATION : 'Value Location'
RULES : 'Value Rules'

[my_vars]
FOLDER_PATH = /upload/files/folder/path
API_TOKEN = valid.token.123
DATASET_NAME = dataset-name
DESCRIPTION = Value description
SUBJECT = Value Subject
CREATOR = Joe Blogs
PUBLISHER = Joe Publisher
CONTRIBUTOR = Joe Contributor
DATE = 2024-03-23
TYPE = Value Resource
IDENTIFIER = Value Identifier
SOURCE = Value Source
LANGUAGE = English
RELATION = Value Relation
COVERAGE = International
RIGHTS = Value rights
MEDIUM = Value Medium
SOURCE_OF_ACQUISITION = Value Source of Aquisition
ORGANIZATIONAL_DESCRIPTION = alphabetically
PHYSICAL_TECHNICAL = Value Relevant physical conditions
LOCATION = Value Location
RULES = Value Rules
DATE_MODIFIED = 2024-03-23

0 comments on commit 344d530

Please sign in to comment.