1 | NO CONTENT: modified file, binary diff hidden |
|
NO CONTENT: modified file, binary diff hidden |
@@ -1,916 +1,935 | |||||
1 | from ckanapi import RemoteCKAN |
|
1 | from ckanapi import RemoteCKAN | |
2 | from datetime import datetime |
|
2 | from datetime import datetime | |
3 | from tqdm import tqdm |
|
3 | from tqdm import tqdm | |
4 | #from ckanapi.errors import NotAuthorized, NotFound, ValidationError, SearchQueryError, SearchError, CKANAPIError, ServerIncompatibleError |
|
4 | #from ckanapi.errors import NotAuthorized, NotFound, ValidationError, SearchQueryError, SearchError, CKANAPIError, ServerIncompatibleError | |
5 | import sys |
|
5 | import sys | |
6 | import platform |
|
6 | import platform | |
7 | import os |
|
7 | import os | |
8 | import tempfile |
|
8 | import tempfile | |
9 | import shutil |
|
9 | import shutil | |
10 | import zipfile |
|
10 | import zipfile | |
11 | import concurrent.futures |
|
11 | import concurrent.futures | |
12 | import requests |
|
12 | import requests | |
13 | import json |
|
13 | import json | |
14 | #import pathlib |
|
14 | #import pathlib | |
15 | import uuid |
|
15 | import uuid | |
16 |
|
16 | |||
|
17 | if sys.version_info.major == 3: | |||
|
18 | from urllib.parse import urlparse | |||
|
19 | else: | |||
|
20 | import urlparse | |||
|
21 | ||||
17 | class JROAPI(): |
|
22 | class JROAPI(): | |
18 | """ |
|
23 | """ | |
19 | FINALIDAD: |
|
24 | FINALIDAD: | |
20 | Script para administrar y obtener la data del repositorio por medio de APIs. |
|
25 | Script para administrar y obtener la data del repositorio por medio de APIs. | |
21 |
|
26 | |||
22 | REQUISITIOS PREVIOS: |
|
27 | REQUISITIOS PREVIOS: | |
23 | - Paso 1: Tener "pip [Python 2]" o "pip3 [Python 3]" instalado: |
|
28 | - Paso 1: Tener "pip [Python 2]" o "pip3 [Python 3]" instalado: | |
24 | - Paso 2: Instalar lo siguiente como admininstrador: |
|
29 | - Paso 2: Instalar lo siguiente como admininstrador: | |
25 | En Python 2 |
|
30 | En Python 2 | |
26 | - pip install ckanapi==4.5 |
|
31 | - pip install ckanapi==4.5 | |
27 | - pip install requests |
|
32 | - pip install requests | |
28 | - pip install futures |
|
33 | - pip install futures | |
29 | - pip install tqdm |
|
34 | - pip install tqdm | |
30 | En Python > 3 |
|
35 | En Python > 3 | |
31 | - pip3 install ckanapi==4.5 |
|
36 | - pip3 install ckanapi==4.5 | |
32 | - pip3 install requests |
|
37 | - pip3 install requests | |
33 | - pip3 install tqdm |
|
38 | - pip3 install tqdm | |
34 |
|
39 | |||
35 | FUNCIONES DISPONIBLES: |
|
40 | FUNCIONES DISPONIBLES: | |
36 | - action |
|
41 | - action | |
37 | - upload_file |
|
42 | - upload_file | |
38 | - upload_multiple_files |
|
43 | - upload_multiple_files | |
39 | - upload_multiple_files_advance |
|
44 | - upload_multiple_files_advance | |
40 | - show |
|
45 | - show | |
41 | - search |
|
46 | - search | |
42 | - create |
|
47 | - create | |
43 | - patch |
|
48 | - patch | |
44 | - delete |
|
49 | - delete | |
45 | - download_files |
|
50 | - download_files | |
46 |
|
51 | |||
47 | EJEMPLOS: |
|
52 | EJEMPLOS: | |
48 | #1: |
|
53 | #1: | |
49 | with JROAPI('http://demo.example.com', Authorization='#########') as <access_name>: |
|
54 | with JROAPI('http://demo.example.com', Authorization='#########') as <access_name>: | |
50 | ... some operation(s) ... |
|
55 | ... some operation(s) ... | |
51 | #2: |
|
56 | #2: | |
52 | <access_name> = JROAPI('http://example.com', Authorization='#########') |
|
57 | <access_name> = JROAPI('http://example.com', Authorization='#########') | |
53 | ... some operation(s) ... |
|
58 | ... some operation(s) ... | |
54 | <access_name>.ckan.close() |
|
59 | <access_name>.ckan.close() | |
55 |
|
60 | |||
56 | REPORTAR ALGUN PROBLEMA: |
|
61 | REPORTAR ALGUN PROBLEMA: | |
57 | Debe enviar un correo a eynilupu@igp.gob.pe detallando los siguientes pasos: |
|
62 | Debe enviar un correo a eynilupu@igp.gob.pe detallando los siguientes pasos: | |
58 | 1) Correo para contactarlo |
|
63 | 1) Correo para contactarlo | |
59 | 2) Descripcion del problema |
|
64 | 2) Descripcion del problema | |
60 | 3) ¿En que paso o seccion encontro el problema? |
|
65 | 3) ¿En que paso o seccion encontro el problema? | |
61 | 4) ¿Cual era el resultado que usted esperaba? |
|
66 | 4) ¿Cual era el resultado que usted esperaba? | |
62 | """ |
|
67 | """ | |
63 | def __init__(self, url, Authorization=None): |
|
68 | def __init__(self, url, Authorization=None): | |
64 | ua = 'CKAN_JRO/1.1 (+'+str(url)+')' |
|
69 | #-------- Insecure -------# | |
|
70 | self.verify = None | |||
|
71 | session = None | |||
|
72 | if urlparse(url).scheme == 'https': | |||
|
73 | session = requests.Session() | |||
|
74 | session.verify = False | |||
|
75 | self.verify = False | |||
|
76 | #--------------------------# | |||
|
77 | self.url = url | |||
|
78 | ua = 'CKAN_JRO/2.9.2 (+'+str(self.url)+')' | |||
65 | #ua = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36' |
|
79 | #ua = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36' | |
66 | self.ckan = RemoteCKAN(url, apikey=Authorization, user_agent=ua) |
|
80 | self.ckan = RemoteCKAN(self.url, apikey=Authorization, user_agent=ua, session=session) | |
67 | #self.ckan = RemoteCKAN(url, apikey=Authorization) |
|
81 | #self.ckan = RemoteCKAN(self.url, apikey=Authorization) | |
68 | self.Authorization = Authorization |
|
82 | self.Authorization = Authorization | |
69 | # Change for --> self.separator = os.sep |
|
83 | # Change for --> self.separator = os.sep | |
70 | if platform.system() == 'Windows': |
|
84 | if platform.system() == 'Windows': | |
71 | self.separator = '\\' |
|
85 | self.separator = '\\' | |
72 | else: |
|
86 | else: | |
73 | self.separator = '/' |
|
87 | self.separator = '/' | |
74 |
|
88 | |||
75 | self.chunk_size = 1024 |
|
89 | self.chunk_size = 1024 | |
76 | self.list = [] |
|
90 | self.list = [] | |
77 | self.dict = {} |
|
91 | self.dict = {} | |
78 | self.str = '' |
|
92 | self.str = '' | |
79 | self.check = 1 |
|
93 | self.check = 1 | |
80 | self.cont = 0 |
|
94 | self.cont = 0 | |
81 |
|
95 | |||
82 | def __enter__(self): |
|
96 | def __enter__(self): | |
83 | return self |
|
97 | return self | |
84 |
|
98 | |||
85 | def __exit__(self, *args): |
|
99 | def __exit__(self, *args): | |
86 | self.ckan.close() |
|
100 | self.ckan.close() | |
87 |
|
101 | |||
88 | def action(self, action, **kwargs): |
|
102 | def action(self, action, **kwargs): | |
89 | """ |
|
103 | """ | |
90 | FINALIDAD: |
|
104 | FINALIDAD: | |
91 | Funcion para llamar a las APIs disponibles |
|
105 | Funcion para llamar a las APIs disponibles | |
92 |
|
106 | |||
93 | APIs DISPONIBLES: |
|
107 | APIs DISPONIBLES: | |
94 | CONSULTAR: "GUIA DE SCRIPT.pdf" |
|
108 | CONSULTAR: "GUIA DE SCRIPT.pdf" | |
95 |
|
109 | |||
96 | EJEMPLO: |
|
110 | EJEMPLO: | |
97 | <access_name>.action(<consuming API>, param_1 = <class 'param_1'>, ...) |
|
111 | <access_name>.action(<consuming API>, param_1 = <class 'param_1'>, ...) | |
98 | """ |
|
112 | """ | |
99 | #--------------- CASE: PACKAGE SEARCH ---------------# |
|
113 | #--------------- CASE: PACKAGE SEARCH ---------------# | |
100 | if kwargs is not None: |
|
114 | if kwargs is not None: | |
101 | if action == 'package_search': |
|
115 | if action == 'package_search': | |
102 | self.list = ['facet_mincount', 'facet_limit', 'facet_field'] |
|
116 | self.list = ['facet_mincount', 'facet_limit', 'facet_field'] | |
103 | for facet in self.list: |
|
117 | for facet in self.list: | |
104 | if facet in kwargs: |
|
118 | if facet in kwargs: | |
105 | kwargs[facet.replace('_', '.')] = kwargs[facet] |
|
119 | kwargs[facet.replace('_', '.')] = kwargs[facet] | |
106 | kwargs.pop(facet) |
|
120 | kwargs.pop(facet) | |
107 | #----------------------------------------------------# |
|
121 | #----------------------------------------------------# | |
108 | try: |
|
122 | try: | |
109 | return getattr(self.ckan.action, action)(**kwargs) |
|
123 | return getattr(self.ckan.action, action)(**kwargs) | |
110 | except: |
|
124 | except: | |
111 | _, exc_value, _ = sys.exc_info() |
|
125 | _, exc_value, _ = sys.exc_info() | |
112 | return exc_value |
|
126 | return exc_value | |
113 |
|
127 | |||
114 | def upload_file(self, dataset_id, file_path, file_date, file_type, **kwargs): |
|
128 | def upload_file(self, dataset_id, file_path, file_date, file_type, **kwargs): | |
115 | # Agregar si es interruptido por teclado |
|
129 | # Agregar si es interruptido por teclado | |
116 | ''' |
|
130 | ''' | |
117 | FINALIDAD: |
|
131 | FINALIDAD: | |
118 | Funcion para subir un unico archivo al repositorio del ROJ. |
|
132 | Funcion para subir un unico archivo al repositorio del ROJ. | |
119 |
|
133 | |||
120 | PARAMETROS DISPONIBLES: |
|
134 | PARAMETROS DISPONIBLES: | |
121 | CONSULTAR: "GUIA DE SCRIPT.pdf" |
|
135 | CONSULTAR: "GUIA DE SCRIPT.pdf" | |
122 |
|
136 | |||
123 | ESTRUCTURA: |
|
137 | ESTRUCTURA: | |
124 | <access_name>.upload_file(dataset_id = <class 'str'>, file_date = <class 'str'>, file_path = <class 'str'>, file_type = <class 'str'>, param_1 = <class 'param_1'>, ...) |
|
138 | <access_name>.upload_file(dataset_id = <class 'str'>, file_date = <class 'str'>, file_path = <class 'str'>, file_type = <class 'str'>, param_1 = <class 'param_1'>, ...) | |
125 | ''' |
|
139 | ''' | |
126 | self.list = ['package_id', 'upload', 'voc_file_type', 'name'] #file_date |
|
140 | self.list = ['package_id', 'upload', 'voc_file_type', 'name'] #file_date | |
127 | for key1, value1 in kwargs.items(): |
|
141 | for key1, value1 in kwargs.items(): | |
128 | if not key1 in self.list: |
|
142 | if not key1 in self.list: | |
129 | self.dict[key1] = value1 |
|
143 | self.dict[key1] = value1 | |
130 |
|
144 | |||
131 | #---------------------------# |
|
145 | #---------------------------# | |
132 | if not 'others' in kwargs: |
|
146 | if not 'others' in kwargs: | |
133 | self.dict['others'] = '' |
|
147 | self.dict['others'] = '' | |
134 | else: |
|
148 | else: | |
135 | if isinstance(kwargs['others'], list): |
|
149 | if isinstance(kwargs['others'], list): | |
136 | self.dict['others'] = json.dumps(kwargs['others']) |
|
150 | self.dict['others'] = json.dumps(kwargs['others']) | |
137 | #---------------------------# |
|
151 | #---------------------------# | |
138 |
|
152 | |||
139 | if not os.path.isfile(file_path): |
|
153 | if not os.path.isfile(file_path): | |
140 | return 'File "%s" not exist' % (file_path) |
|
154 | return 'File "%s" not exist' % (file_path) | |
141 |
|
155 | |||
142 | #if not 'format' in self.dict: |
|
156 | #if not 'format' in self.dict: | |
143 | # self.str = ''.join(pathlib.Path(file_path).suffixes) |
|
157 | # self.str = ''.join(pathlib.Path(file_path).suffixes) | |
144 | # if len(self.str) > 0: |
|
158 | # if len(self.str) > 0: | |
145 | # self.dict['format'] = self.str.upper()[1:] |
|
159 | # self.dict['format'] = self.str.upper()[1:] | |
146 |
|
160 | |||
147 | #-------------------------PACKAGE SHOW-----------------------# |
|
161 | #-------------------------PACKAGE SHOW-----------------------# | |
148 | try: |
|
162 | try: | |
149 | dataset_show = getattr(self.ckan.action, 'package_show')(id=dataset_id)['resources'] |
|
163 | dataset_show = getattr(self.ckan.action, 'package_show')(id=dataset_id)['resources'] | |
150 | except: |
|
164 | except: | |
151 | _, exc_value, _ = sys.exc_info() |
|
165 | _, exc_value, _ = sys.exc_info() | |
152 | print('ERROR obtaining metadata dataset:: Use the "print" for more information') |
|
166 | print('ERROR obtaining metadata dataset:: Use the "print" for more information') | |
153 | return exc_value |
|
167 | return exc_value | |
154 |
|
168 | |||
155 | resources_name = [] |
|
169 | resources_name = [] | |
156 | for u in dataset_show: |
|
170 | for u in dataset_show: | |
157 | resources_name.append(u['name'].lower()) |
|
171 | resources_name.append(u['name'].lower()) | |
158 |
|
172 | |||
159 | if os.path.basename(file_path).lower() in resources_name: |
|
173 | if os.path.basename(file_path).lower() in resources_name: | |
160 | return 'ERROR:: "%s" file already exist in this dataset' % (os.path.basename(file_path)) |
|
174 | return 'ERROR:: "%s" file already exist in this dataset' % (os.path.basename(file_path)) | |
161 | #------------------------------------------------------------# |
|
175 | #------------------------------------------------------------# | |
162 |
|
176 | |||
163 | try: |
|
177 | try: | |
164 | return getattr(self.ckan.action, 'resource_create')(package_id=dataset_id, file_date=file_date, upload=open(file_path, 'rb'), voc_file_type=file_type, name=os.path.basename(file_path), **self.dict) |
|
178 | return getattr(self.ckan.action, 'resource_create')(package_id=dataset_id, file_date=file_date, upload=open(file_path, 'rb'), voc_file_type=file_type, name=os.path.basename(file_path), **self.dict) | |
165 | except: |
|
179 | except: | |
166 | _, exc_value, _ = sys.exc_info() |
|
180 | _, exc_value, _ = sys.exc_info() | |
167 | return exc_value |
|
181 | return exc_value | |
168 |
|
182 | |||
169 | def upload_multiple_files_advance(self, dataset_id, path_files, file_date, file_type, max_size=100, max_count=500, ignore_repetition=False, **kwargs): |
|
183 | def upload_multiple_files_advance(self, dataset_id, path_files, file_date, file_type, max_size=100, max_count=500, ignore_repetition=False, **kwargs): | |
170 | # Agregar si es interruptido por teclado |
|
184 | # Agregar si es interruptido por teclado | |
171 | ''' |
|
185 | ''' | |
172 | FINALIDAD: |
|
186 | FINALIDAD: | |
173 | Funcion para subir multiples archivos al repositorio del ROJ. |
|
187 | Funcion para subir multiples archivos al repositorio del ROJ. | |
174 |
|
188 | |||
175 | PARAMETROS DISPONIBLES: |
|
189 | PARAMETROS DISPONIBLES: | |
176 | CONSULTAR: "GUIA DE SCRIPT.pdf" |
|
190 | CONSULTAR: "GUIA DE SCRIPT.pdf" | |
177 |
|
191 | |||
178 | ESTRUCTURA: |
|
192 | ESTRUCTURA: | |
179 | <access_name>.upload_multiple_files_advance(dataset_id = <class 'str'>, path_files = <class 'list of strings'>, file_date = <class 'str'>, file_type = <class 'str'>, param_1 = <class 'param_1'>, ...) |
|
193 | <access_name>.upload_multiple_files_advance(dataset_id = <class 'str'>, path_files = <class 'list of strings'>, file_date = <class 'str'>, file_type = <class 'str'>, param_1 = <class 'param_1'>, ...) | |
180 | ''' |
|
194 | ''' | |
181 | #-------------------------PACKAGE SHOW-----------------------# |
|
195 | #-------------------------PACKAGE SHOW-----------------------# | |
182 | try: |
|
196 | try: | |
183 | dataset_show = getattr(self.ckan.action, 'package_show')(id=dataset_id)['resources'] |
|
197 | dataset_show = getattr(self.ckan.action, 'package_show')(id=dataset_id)['resources'] | |
184 | except: |
|
198 | except: | |
185 | _, exc_value, _ = sys.exc_info() |
|
199 | _, exc_value, _ = sys.exc_info() | |
186 | print('ERROR obtaining metadata dataset:: Use the "print" for more information') |
|
200 | print('ERROR obtaining metadata dataset:: Use the "print" for more information') | |
187 | return exc_value |
|
201 | return exc_value | |
188 | #------------------------------------------------------------# |
|
202 | #------------------------------------------------------------# | |
189 | resources_name = [] |
|
203 | resources_name = [] | |
190 | for u in dataset_show: |
|
204 | for u in dataset_show: | |
191 | resources_name.append(u['name'].lower()) |
|
205 | resources_name.append(u['name'].lower()) | |
192 | #------------------------------------------------------------# |
|
206 | #------------------------------------------------------------# | |
193 | self.list = ['package_id', 'upload', 'voc_file_type', 'name'] |
|
207 | self.list = ['package_id', 'upload', 'voc_file_type', 'name'] | |
194 | for key1, value1 in kwargs.items(): |
|
208 | for key1, value1 in kwargs.items(): | |
195 | if not key1 in self.list: |
|
209 | if not key1 in self.list: | |
196 | self.dict[key1] = value1 |
|
210 | self.dict[key1] = value1 | |
197 | #------------------------------------------------------------# |
|
211 | #------------------------------------------------------------# | |
198 | if not 'others' in kwargs: |
|
212 | if not 'others' in kwargs: | |
199 | self.dict['others'] = '' |
|
213 | self.dict['others'] = '' | |
200 | else: |
|
214 | else: | |
201 | if isinstance(kwargs['others'], list): |
|
215 | if isinstance(kwargs['others'], list): | |
202 | self.dict['others'] = json.dumps(kwargs['others']) |
|
216 | self.dict['others'] = json.dumps(kwargs['others']) | |
203 | #------------------------------------------------------------# |
|
217 | #------------------------------------------------------------# | |
204 | total_list = [] |
|
218 | total_list = [] | |
205 | #---------------CASO : "path" or "path_list"-----------------# |
|
219 | #---------------CASO : "path" or "path_list"-----------------# | |
206 | if type(path_files) is list: |
|
220 | if type(path_files) is list: | |
207 | if len(path_files) != 0: |
|
221 | if len(path_files) != 0: | |
208 | path_files.sort() |
|
222 | path_files.sort() | |
209 | for u in path_files: |
|
223 | for u in path_files: | |
210 | if os.path.isfile(u): |
|
224 | if os.path.isfile(u): | |
211 | if os.path.basename(u).lower() in resources_name: |
|
225 | if os.path.basename(u).lower() in resources_name: | |
212 | if not ignore_repetition: |
|
226 | if not ignore_repetition: | |
213 | return 'ERROR:: "%s" file already exist in this dataset' % (os.path.basename(u)) |
|
227 | return 'ERROR:: "%s" file already exist in this dataset' % (os.path.basename(u)) | |
214 | print('WARRING:: "'+ str(os.path.basename(u)) +'" file was ignored because already exist in this dataset') |
|
228 | print('WARRING:: "'+ str(os.path.basename(u)) +'" file was ignored because already exist in this dataset') | |
215 | else: |
|
229 | else: | |
216 | total_list.append({'name':os.path.basename(u), 'size': os.stat(u).st_size, 'upload':open(u, 'rb')}) |
|
230 | total_list.append({'name':os.path.basename(u), 'size': os.stat(u).st_size, 'upload':open(u, 'rb')}) | |
217 | else: |
|
231 | else: | |
218 | return 'File "%s" does not exist' % (u) |
|
232 | return 'File "%s" does not exist' % (u) | |
219 | else: |
|
233 | else: | |
220 | return 'ERROR:: "path_list is empty"' |
|
234 | return 'ERROR:: "path_list is empty"' | |
221 |
|
235 | |||
222 | elif type(path_files) is str: |
|
236 | elif type(path_files) is str: | |
223 | if os.path.isdir(path_files): |
|
237 | if os.path.isdir(path_files): | |
224 | path_order = [f for f in os.listdir(path_files) if os.path.isfile(os.path.join(path_files, f))] |
|
238 | path_order = [f for f in os.listdir(path_files) if os.path.isfile(os.path.join(path_files, f))] | |
225 | path_order.sort() |
|
239 | path_order.sort() | |
226 | if path_order: |
|
240 | if path_order: | |
227 | for name in path_order: |
|
241 | for name in path_order: | |
228 | if name.lower() in resources_name: |
|
242 | if name.lower() in resources_name: | |
229 | if not ignore_repetition: |
|
243 | if not ignore_repetition: | |
230 | return 'ERROR:: "%s" file already exist in this dataset' % (name) |
|
244 | return 'ERROR:: "%s" file already exist in this dataset' % (name) | |
231 | print('WARRING:: "'+ name +'" file was ignored because already exist in this dataset') |
|
245 | print('WARRING:: "'+ name +'" file was ignored because already exist in this dataset') | |
232 | else: |
|
246 | else: | |
233 | total_list.append({'name':name, 'size': os.stat(os.path.join(path_files, name)).st_size, 'upload':open(os.path.join(path_files, name), 'rb')}) |
|
247 | total_list.append({'name':name, 'size': os.stat(os.path.join(path_files, name)).st_size, 'upload':open(os.path.join(path_files, name), 'rb')}) | |
234 | else: |
|
248 | else: | |
235 | return "ERROR:: There aren't files in this directory" |
|
249 | return "ERROR:: There aren't files in this directory" | |
236 | else: |
|
250 | else: | |
237 | return 'ERROR:: Directory "%s" does not exist' % (path_files) |
|
251 | return 'ERROR:: Directory "%s" does not exist' % (path_files) | |
238 | else: |
|
252 | else: | |
239 | return 'ERROR:: "path_files" must be a str or list' |
|
253 | return 'ERROR:: "path_files" must be a str or list' | |
240 | #------------------------------------------------------------# |
|
254 | #------------------------------------------------------------# | |
241 | try: |
|
255 | try: | |
242 | uuid.UUID(str(dataset_id), version=4) |
|
256 | uuid.UUID(str(dataset_id), version=4) | |
243 | package_id_or_name = '"id": "' + str(dataset_id) + '"' |
|
257 | package_id_or_name = '"id": "' + str(dataset_id) + '"' | |
244 | except ValueError: |
|
258 | except ValueError: | |
245 | package_id_or_name = '"name": "' + str(dataset_id) + '"' |
|
259 | package_id_or_name = '"name": "' + str(dataset_id) + '"' | |
246 | #------------------------------------------------------------# |
|
260 | #------------------------------------------------------------# | |
247 | blocks = [[]] |
|
261 | blocks = [[]] | |
248 | size_file = 0 |
|
262 | size_file = 0 | |
249 | count_file = 0 |
|
263 | count_file = 0 | |
250 | inter_num = 0 |
|
264 | inter_num = 0 | |
251 | for value in total_list: |
|
265 | for value in total_list: | |
252 | if value['size'] > 1024 * 1024 * float(max_size): |
|
266 | if value['size'] > 1024 * 1024 * float(max_size): | |
253 | return 'ERROR:: The size of the "%s" file is %sMB aprox, please change "max_size" value' % (value['name'], str(round(value['size']/(1024 * 1024), 2))) |
|
267 | return 'ERROR:: The size of the "%s" file is %sMB aprox, please change "max_size" value' % (value['name'], str(round(value['size']/(1024 * 1024), 2))) | |
254 | if not 1 <= int(max_count) <= 999: |
|
268 | if not 1 <= int(max_count) <= 999: | |
255 | return 'ERROR:: The count of the number of files must be between 1 and 999, please change "max_count" value' |
|
269 | return 'ERROR:: The count of the number of files must be between 1 and 999, please change "max_count" value' | |
256 |
|
270 | |||
257 | size_file = size_file + value['size'] |
|
271 | size_file = size_file + value['size'] | |
258 | count_file = count_file + 1 |
|
272 | count_file = count_file + 1 | |
259 | if size_file <= 1024 * 1024 * float(max_size) and count_file <= int(max_count): |
|
273 | if size_file <= 1024 * 1024 * float(max_size) and count_file <= int(max_count): | |
260 | del value['size'] |
|
274 | del value['size'] | |
261 | blocks[inter_num].append(value) |
|
275 | blocks[inter_num].append(value) | |
262 | else: |
|
276 | else: | |
263 | inter_num = inter_num + 1 |
|
277 | inter_num = inter_num + 1 | |
264 | size_file = value['size'] |
|
278 | size_file = value['size'] | |
265 | count_file = 1 |
|
279 | count_file = 1 | |
266 | blocks.append([]) |
|
280 | blocks.append([]) | |
267 | del value['size'] |
|
281 | del value['size'] | |
268 | blocks[inter_num].append(value) |
|
282 | blocks[inter_num].append(value) | |
269 | #------------------------------------------------------------# |
|
283 | #------------------------------------------------------------# | |
270 | if len(blocks[0]) > 0: |
|
284 | if len(blocks[0]) > 0: | |
271 | print('BLOCK(S) IN TOTAL:: {}'.format(len(blocks))) |
|
285 | print('BLOCK(S) IN TOTAL:: {}'.format(len(blocks))) | |
272 | for count1, block in enumerate(blocks): |
|
286 | for count1, block in enumerate(blocks): | |
273 | print('---- BLOCK N°{} ----'.format(count1 + 1)) |
|
287 | print('---- BLOCK N°{} ----'.format(count1 + 1)) | |
274 | resource_extend = [] |
|
288 | resource_extend = [] | |
275 | files_dict = {} |
|
289 | files_dict = {} | |
276 | for count2, value2 in enumerate(block): |
|
290 | for count2, value2 in enumerate(block): | |
277 | value2['file_date'] = file_date |
|
291 | value2['file_date'] = file_date | |
278 | value2['voc_file_type'] = file_type |
|
292 | value2['voc_file_type'] = file_type | |
279 | value2.update(self.dict) |
|
293 | value2.update(self.dict) | |
280 |
|
294 | |||
281 | #if not 'format' in value2: |
|
295 | #if not 'format' in value2: | |
282 | # format = ''.join(pathlib.Path(value2['name']).suffixes) |
|
296 | # format = ''.join(pathlib.Path(value2['name']).suffixes) | |
283 | # if len(format) > 0: |
|
297 | # if len(format) > 0: | |
284 | # value2['format'] = format.upper()[1:] |
|
298 | # value2['format'] = format.upper()[1:] | |
285 |
|
299 | |||
286 | files_dict['update__resources__-'+ str(len(block)-count2) +'__upload'] = (value2['name'], value2['upload']) |
|
300 | files_dict['update__resources__-'+ str(len(block)-count2) +'__upload'] = (value2['name'], value2['upload']) | |
287 | del value2['upload'] |
|
301 | del value2['upload'] | |
288 | resource_extend.append(value2) |
|
302 | resource_extend.append(value2) | |
289 |
|
303 | |||
290 | print('BLOCK N°{} :: "{}" file(s) found >> uploading'.format(count1 + 1, len(block))) |
|
304 | print('BLOCK N°{} :: "{}" file(s) found >> uploading'.format(count1 + 1, len(block))) | |
291 | try: |
|
305 | try: | |
292 | result = self.ckan.call_action( |
|
306 | result = self.ckan.call_action( | |
293 | 'package_revise', |
|
307 | 'package_revise', | |
294 | {'match': '{'+ str(package_id_or_name) +'}', 'update__resources__extend': json.dumps(resource_extend)}, |
|
308 | {'match': '{'+ str(package_id_or_name) +'}', 'update__resources__extend': json.dumps(resource_extend)}, | |
295 | files=files_dict |
|
309 | files=files_dict | |
296 | ) |
|
310 | ) | |
297 | print('BLOCK N°{} :: Uploaded file(s) successfully'.format(count1 + 1)) |
|
311 | print('BLOCK N°{} :: Uploaded file(s) successfully'.format(count1 + 1)) | |
298 | if len(blocks) == count1 + 1: |
|
312 | if len(blocks) == count1 + 1: | |
299 | return result |
|
313 | return result | |
300 | except: |
|
314 | except: | |
301 | print('ERROR :: Use the "print" for more information') |
|
315 | print('ERROR :: Use the "print" for more information') | |
302 | _, exc_value, _ = sys.exc_info() |
|
316 | _, exc_value, _ = sys.exc_info() | |
303 | return exc_value |
|
317 | return exc_value | |
304 | else: |
|
318 | else: | |
305 | return "ERROR:: No file(s) found to upload" |
|
319 | return "ERROR:: No file(s) found to upload" | |
306 |
|
320 | |||
307 | def upload_multiple_files(self, dataset_id, path_files, date_files, type_files, ignore_repetition=False, **kwargs): |
|
321 | def upload_multiple_files(self, dataset_id, path_files, date_files, type_files, ignore_repetition=False, **kwargs): | |
308 | # Agregar si es interruptido por teclado |
|
322 | # Agregar si es interruptido por teclado | |
309 | ''' |
|
323 | ''' | |
310 | FINALIDAD: |
|
324 | FINALIDAD: | |
311 | Funcion para subir multiples archivos al repositorio del ROJ. |
|
325 | Funcion para subir multiples archivos al repositorio del ROJ. | |
312 |
|
326 | |||
313 | PARAMETROS DISPONIBLES: |
|
327 | PARAMETROS DISPONIBLES: | |
314 | CONSULTAR: "GUIA DE SCRIPT.pdf" |
|
328 | CONSULTAR: "GUIA DE SCRIPT.pdf" | |
315 |
|
329 | |||
316 | ESTRUCTURA: |
|
330 | ESTRUCTURA: | |
317 | <access_name>.upload_multiple_files(dataset_id = <class 'str'>, path_files = <class 'str'> or <class 'list of strings'>, date_files = <class 'str'> or <class 'list of strings'>, type_files = <class 'str'> or <class 'list of strings'>, param_1 = <class 'param_1'>, ...) |
|
331 | <access_name>.upload_multiple_files(dataset_id = <class 'str'>, path_files = <class 'str'> or <class 'list of strings'>, date_files = <class 'str'> or <class 'list of strings'>, type_files = <class 'str'> or <class 'list of strings'>, param_1 = <class 'param_1'>, ...) | |
318 | ''' |
|
332 | ''' | |
319 | #-------------------------PACKAGE SHOW-----------------------# |
|
333 | #-------------------------PACKAGE SHOW-----------------------# | |
320 | try: |
|
334 | try: | |
321 | dataset_show = getattr(self.ckan.action, 'package_show')(id=dataset_id)['resources'] |
|
335 | dataset_show = getattr(self.ckan.action, 'package_show')(id=dataset_id)['resources'] | |
322 | except: |
|
336 | except: | |
323 | _, exc_value, _ = sys.exc_info() |
|
337 | _, exc_value, _ = sys.exc_info() | |
324 | print('ERROR obtaining metadata dataset:: Use the "print" for more information') |
|
338 | print('ERROR obtaining metadata dataset:: Use the "print" for more information') | |
325 | return exc_value |
|
339 | return exc_value | |
326 | #------------------------------------------------------------# |
|
340 | #------------------------------------------------------------# | |
327 | resources_name = [] |
|
341 | resources_name = [] | |
328 | for u in dataset_show: |
|
342 | for u in dataset_show: | |
329 | resources_name.append(u['name'].lower()) |
|
343 | resources_name.append(u['name'].lower()) | |
330 | #------------------------------------------------------------# |
|
344 | #------------------------------------------------------------# | |
331 |
|
345 | |||
332 | params_dict = {'upload':[], 'name':[]} |
|
346 | params_dict = {'upload':[], 'name':[]} | |
333 | #if not 'format' in kwargs: |
|
347 | #if not 'format' in kwargs: | |
334 | # params_dict.update({'format':[]}) |
|
348 | # params_dict.update({'format':[]}) | |
335 | #---------------CASO : "path" or "path_list"-----------------# |
|
349 | #---------------CASO : "path" or "path_list"-----------------# | |
336 | if type(path_files) is list: |
|
350 | if type(path_files) is list: | |
337 | if len(path_files) != 0: |
|
351 | if len(path_files) != 0: | |
338 | path_files.sort() |
|
352 | path_files.sort() | |
339 | for u in path_files: |
|
353 | for u in path_files: | |
340 | if os.path.isfile(u): |
|
354 | if os.path.isfile(u): | |
341 | if os.path.basename(u).lower() in resources_name: |
|
355 | if os.path.basename(u).lower() in resources_name: | |
342 | if not ignore_repetition: |
|
356 | if not ignore_repetition: | |
343 | return 'ERROR:: "%s" file already exist in this dataset' % (os.path.basename(u)) |
|
357 | return 'ERROR:: "%s" file already exist in this dataset' % (os.path.basename(u)) | |
344 | print('WARRING:: "'+ str(os.path.basename(u)) +'" file was ignored because already exist in this dataset') |
|
358 | print('WARRING:: "'+ str(os.path.basename(u)) +'" file was ignored because already exist in this dataset') | |
345 | else: |
|
359 | else: | |
346 | params_dict['upload'].append(open(u, 'rb')) |
|
360 | params_dict['upload'].append(open(u, 'rb')) | |
347 | params_dict['name'].append(os.path.basename(u)) |
|
361 | params_dict['name'].append(os.path.basename(u)) | |
348 | #if not 'format' in kwargs: |
|
362 | #if not 'format' in kwargs: | |
349 | # format = ''.join(pathlib.Path(u).suffixes) |
|
363 | # format = ''.join(pathlib.Path(u).suffixes) | |
350 | # if len(format) > 0: |
|
364 | # if len(format) > 0: | |
351 | # params_dict['format'].append(format.upper()[1:]) |
|
365 | # params_dict['format'].append(format.upper()[1:]) | |
352 | # else: |
|
366 | # else: | |
353 | # params_dict['format'].append('') |
|
367 | # params_dict['format'].append('') | |
354 | else: |
|
368 | else: | |
355 | return 'File "%s" does not exist' % (u) |
|
369 | return 'File "%s" does not exist' % (u) | |
356 | else: |
|
370 | else: | |
357 | return 'ERROR:: "path_list is empty"' |
|
371 | return 'ERROR:: "path_list is empty"' | |
358 | elif type(path_files) is str: |
|
372 | elif type(path_files) is str: | |
359 | if os.path.isdir(path_files): |
|
373 | if os.path.isdir(path_files): | |
360 | path_order = [f for f in os.listdir(path_files) if os.path.isfile(os.path.join(path_files, f))] |
|
374 | path_order = [f for f in os.listdir(path_files) if os.path.isfile(os.path.join(path_files, f))] | |
361 | path_order.sort() |
|
375 | path_order.sort() | |
362 | if path_order: |
|
376 | if path_order: | |
363 | for name in path_order: |
|
377 | for name in path_order: | |
364 | if name.lower() in resources_name: |
|
378 | if name.lower() in resources_name: | |
365 | if not ignore_repetition: |
|
379 | if not ignore_repetition: | |
366 | return 'ERROR:: "%s" file already exist in this dataset' % (name) |
|
380 | return 'ERROR:: "%s" file already exist in this dataset' % (name) | |
367 | print('WARRING:: "'+ str(name) +'" file was ignored because already exist in this dataset') |
|
381 | print('WARRING:: "'+ str(name) +'" file was ignored because already exist in this dataset') | |
368 | else: |
|
382 | else: | |
369 | params_dict['upload'].append(open(os.path.join(path_files, name), 'rb')) |
|
383 | params_dict['upload'].append(open(os.path.join(path_files, name), 'rb')) | |
370 | params_dict['name'].append(name) |
|
384 | params_dict['name'].append(name) | |
371 | #if not 'format' in kwargs: |
|
385 | #if not 'format' in kwargs: | |
372 | # format = ''.join(pathlib.Path(name).suffixes) |
|
386 | # format = ''.join(pathlib.Path(name).suffixes) | |
373 | # if len(format) > 0: |
|
387 | # if len(format) > 0: | |
374 | # params_dict['format'].append(format.upper()[1:]) |
|
388 | # params_dict['format'].append(format.upper()[1:]) | |
375 | # else: |
|
389 | # else: | |
376 | # params_dict['format'].append('') |
|
390 | # params_dict['format'].append('') | |
377 | else: |
|
391 | else: | |
378 | return "ERROR:: There aren't files in this directory" |
|
392 | return "ERROR:: There aren't files in this directory" | |
379 | else: |
|
393 | else: | |
380 | return 'ERROR:: Directory "%s" does not exist' % (path_files) |
|
394 | return 'ERROR:: Directory "%s" does not exist' % (path_files) | |
381 | else: |
|
395 | else: | |
382 | return 'ERROR:: "path_files" must be a str or list' |
|
396 | return 'ERROR:: "path_files" must be a str or list' | |
383 | #------------------------------------------------------------# |
|
397 | #------------------------------------------------------------# | |
384 | params_no_dict = {'package_id': dataset_id} |
|
398 | params_no_dict = {'package_id': dataset_id} | |
385 | if type(date_files) is list: |
|
399 | if type(date_files) is list: | |
386 | params_dict['file_date'] = date_files |
|
400 | params_dict['file_date'] = date_files | |
387 | else: |
|
401 | else: | |
388 | params_no_dict['file_date'] = date_files |
|
402 | params_no_dict['file_date'] = date_files | |
389 |
|
403 | |||
390 | if type(type_files) is list: |
|
404 | if type(type_files) is list: | |
391 | params_dict['voc_file_type'] = type_files |
|
405 | params_dict['voc_file_type'] = type_files | |
392 | else: |
|
406 | else: | |
393 | params_no_dict['voc_file_type'] = type_files |
|
407 | params_no_dict['voc_file_type'] = type_files | |
394 |
|
408 | |||
395 | for key1, value1 in kwargs.items(): |
|
409 | for key1, value1 in kwargs.items(): | |
396 | if not key1 in params_dict and not key1 in params_no_dict and key1 != 'others': |
|
410 | if not key1 in params_dict and not key1 in params_no_dict and key1 != 'others': | |
397 | if type(value1) is list: |
|
411 | if type(value1) is list: | |
398 | params_dict[key1] = value1 |
|
412 | params_dict[key1] = value1 | |
399 | else: |
|
413 | else: | |
400 | params_no_dict[key1] = value1 |
|
414 | params_no_dict[key1] = value1 | |
401 | #------------------------------------------# |
|
415 | #------------------------------------------# | |
402 | if not 'others' in kwargs: |
|
416 | if not 'others' in kwargs: | |
403 | params_no_dict['others'] = '' |
|
417 | params_no_dict['others'] = '' | |
404 | else: |
|
418 | else: | |
405 | if isinstance(kwargs['others'], tuple): |
|
419 | if isinstance(kwargs['others'], tuple): | |
406 | params_dict['others'] = [json.dumps(w) for w in kwargs['others']] |
|
420 | params_dict['others'] = [json.dumps(w) for w in kwargs['others']] | |
407 | elif isinstance(kwargs['others'], list): |
|
421 | elif isinstance(kwargs['others'], list): | |
408 | params_no_dict['others'] = json.dumps(kwargs['others']) |
|
422 | params_no_dict['others'] = json.dumps(kwargs['others']) | |
409 | elif isinstance(kwargs['others'], str): |
|
423 | elif isinstance(kwargs['others'], str): | |
410 | params_no_dict['others'] = kwargs['others'] |
|
424 | params_no_dict['others'] = kwargs['others'] | |
411 | else: |
|
425 | else: | |
412 | return 'ERROR:: "others" must be a tuple, list or str' |
|
426 | return 'ERROR:: "others" must be a tuple, list or str' | |
413 | #------------------------------------------# |
|
427 | #------------------------------------------# | |
414 | len_params_dict = [] |
|
428 | len_params_dict = [] | |
415 | for value2 in params_dict.values(): |
|
429 | for value2 in params_dict.values(): | |
416 | len_params_dict.append(len(value2)) |
|
430 | len_params_dict.append(len(value2)) | |
417 |
|
431 | |||
418 | if len(list(set(len_params_dict))) > 1: |
|
432 | if len(list(set(len_params_dict))) > 1: | |
419 | return 'ERROR:: All lists must be the same length: %s' % (len(params_dict['name'])) |
|
433 | return 'ERROR:: All lists must be the same length: %s' % (len(params_dict['name'])) | |
420 | #------------------------------------------------------------# |
|
434 | #------------------------------------------------------------# | |
421 | print('"{}" file(s) found >> uploading'.format(len(params_dict['name']))) |
|
435 | print('"{}" file(s) found >> uploading'.format(len(params_dict['name']))) | |
422 | for v in range(len(params_dict['name'])): |
|
436 | for v in range(len(params_dict['name'])): | |
423 | try: |
|
437 | try: | |
424 | send = {} |
|
438 | send = {} | |
425 | for key_dict, value_dict in params_dict.items(): |
|
439 | for key_dict, value_dict in params_dict.items(): | |
426 | send[key_dict] = value_dict[v] |
|
440 | send[key_dict] = value_dict[v] | |
427 | for key_no_dict, value_no_dict in params_no_dict.items(): |
|
441 | for key_no_dict, value_no_dict in params_no_dict.items(): | |
428 | send[key_no_dict] = value_no_dict |
|
442 | send[key_no_dict] = value_no_dict | |
429 |
|
443 | |||
430 | self.list.append(getattr(self.ckan.action, 'resource_create')(**send)) |
|
444 | self.list.append(getattr(self.ckan.action, 'resource_create')(**send)) | |
431 | print('File #{} :: "{}" was uploaded successfully'.format(v+1, params_dict['name'][v])) |
|
445 | print('File #{} :: "{}" was uploaded successfully'.format(v+1, params_dict['name'][v])) | |
432 | except: |
|
446 | except: | |
433 | _, exc_value, _ = sys.exc_info() |
|
447 | _, exc_value, _ = sys.exc_info() | |
434 | self.list.append(exc_value) |
|
448 | self.list.append(exc_value) | |
435 | print('File #{} :: Error uploading "{}" file'.format(v+1, params_dict['name'][v])) |
|
449 | print('File #{} :: Error uploading "{}" file'.format(v+1, params_dict['name'][v])) | |
436 | return self.list |
|
450 | return self.list | |
437 | #------------------------------------------------------------# |
|
451 | #------------------------------------------------------------# | |
438 |
|
452 | |||
439 | def show(self, type_option, id, **kwargs): |
|
453 | def show(self, type_option, id, **kwargs): | |
440 | ''' |
|
454 | ''' | |
441 | FINALIDAD: |
|
455 | FINALIDAD: | |
442 | Funcion personalizada para una busqueda en especifico. |
|
456 | Funcion personalizada para una busqueda en especifico. | |
443 |
|
457 | |||
444 | PARAMETROS DISPONIBLES: |
|
458 | PARAMETROS DISPONIBLES: | |
445 | CONSULTAR: "GUIA DE SCRIPT.pdf" |
|
459 | CONSULTAR: "GUIA DE SCRIPT.pdf" | |
446 |
|
460 | |||
447 | ESTRUCTURA: |
|
461 | ESTRUCTURA: | |
448 | <access_name>.show(type_option = <class 'str'>, id = <class 'str'>, param_1 = <class 'param_1'>, ...) |
|
462 | <access_name>.show(type_option = <class 'str'>, id = <class 'str'>, param_1 = <class 'param_1'>, ...) | |
449 | ''' |
|
463 | ''' | |
450 | if type(type_option) is str: |
|
464 | if type(type_option) is str: | |
451 | try: |
|
465 | try: | |
452 | if type_option == 'dataset': |
|
466 | if type_option == 'dataset': | |
453 | return getattr(self.ckan.action, 'package_show')(id=id, **kwargs) |
|
467 | return getattr(self.ckan.action, 'package_show')(id=id, **kwargs) | |
454 | elif type_option == 'resource': |
|
468 | elif type_option == 'resource': | |
455 | return getattr(self.ckan.action, 'resource_show')(id=id, **kwargs) |
|
469 | return getattr(self.ckan.action, 'resource_show')(id=id, **kwargs) | |
456 | elif type_option == 'project': |
|
470 | elif type_option == 'project': | |
457 | return getattr(self.ckan.action, 'organization_show')(id=id, **kwargs) |
|
471 | return getattr(self.ckan.action, 'organization_show')(id=id, **kwargs) | |
458 | elif type_option == 'collaborator': |
|
472 | elif type_option == 'collaborator': | |
459 | return getattr(self.ckan.action, 'package_collaborator_list_for_user')(id=id, **kwargs) |
|
473 | return getattr(self.ckan.action, 'package_collaborator_list_for_user')(id=id, **kwargs) | |
460 | elif type_option == 'member': |
|
474 | elif type_option == 'member': | |
461 | return getattr(self.ckan.action, 'organization_list_for_user')(id=id, **kwargs) |
|
475 | return getattr(self.ckan.action, 'organization_list_for_user')(id=id, **kwargs) | |
462 | elif type_option == 'vocabulary': |
|
476 | elif type_option == 'vocabulary': | |
463 | return getattr(self.ckan.action, 'vocabulary_show')(id=id, **kwargs) |
|
477 | return getattr(self.ckan.action, 'vocabulary_show')(id=id, **kwargs) | |
464 | elif type_option == 'tag': |
|
478 | elif type_option == 'tag': | |
465 | if not 'vocabulary_id' in kwargs: |
|
479 | if not 'vocabulary_id' in kwargs: | |
466 | print('Missing "vocabulary_id" value: assume it is a free tag') |
|
480 | print('Missing "vocabulary_id" value: assume it is a free tag') | |
467 | return getattr(self.ckan.action, 'tag_show')(id=id, **kwargs) |
|
481 | return getattr(self.ckan.action, 'tag_show')(id=id, **kwargs) | |
468 | elif type_option == 'user': |
|
482 | elif type_option == 'user': | |
469 | return getattr(self.ckan.action, 'user_show')(id=id, **kwargs) |
|
483 | return getattr(self.ckan.action, 'user_show')(id=id, **kwargs) | |
470 | elif type_option == 'job': |
|
484 | elif type_option == 'job': | |
471 | return getattr(self.ckan.action, 'job_show')(id=id, **kwargs) |
|
485 | return getattr(self.ckan.action, 'job_show')(id=id, **kwargs) | |
472 | else: |
|
486 | else: | |
473 | return 'ERROR:: "type_option = %s" is not accepted' % (type_option) |
|
487 | return 'ERROR:: "type_option = %s" is not accepted' % (type_option) | |
474 | except: |
|
488 | except: | |
475 | _, exc_value, _ = sys.exc_info() |
|
489 | _, exc_value, _ = sys.exc_info() | |
476 | return exc_value |
|
490 | return exc_value | |
477 | else: |
|
491 | else: | |
478 | return 'ERROR:: "type_option" must be a str' |
|
492 | return 'ERROR:: "type_option" must be a str' | |
479 |
|
493 | |||
480 | def search(self, type_option, query=None, **kwargs): |
|
494 | def search(self, type_option, query=None, **kwargs): | |
481 | ''' |
|
495 | ''' | |
482 | FINALIDAD: |
|
496 | FINALIDAD: | |
483 | Funcion personalizada para busquedas que satisfagan algun criterio. |
|
497 | Funcion personalizada para busquedas que satisfagan algun criterio. | |
484 |
|
498 | |||
485 | PARAMETROS DISPONIBLES: |
|
499 | PARAMETROS DISPONIBLES: | |
486 | CONSULTAR: "GUIA DE SCRIPT.pdf" |
|
500 | CONSULTAR: "GUIA DE SCRIPT.pdf" | |
487 |
|
501 | |||
488 | ESTRUCTURA: |
|
502 | ESTRUCTURA: | |
489 | <access_name>.search(type_option = <class 'str'>, query = <class 'dict'>, param_1 = <class 'param_1'>, ...) |
|
503 | <access_name>.search(type_option = <class 'str'>, query = <class 'dict'>, param_1 = <class 'param_1'>, ...) | |
490 | ''' |
|
504 | ''' | |
491 | if type(type_option) is str: |
|
505 | if type(type_option) is str: | |
492 | try: |
|
506 | try: | |
493 | if type_option == 'dataset': |
|
507 | if type_option == 'dataset': | |
494 | key_replace = ['fq', 'fq_list', 'include_private'] |
|
508 | key_replace = ['fq', 'fq_list', 'include_private'] | |
495 | key_point = ['facet_mincount', 'facet_limit', 'facet_field'] |
|
509 | key_point = ['facet_mincount', 'facet_limit', 'facet_field'] | |
496 | for key1, value1 in kwargs.items(): |
|
510 | for key1, value1 in kwargs.items(): | |
497 | if not key1 in key_replace: |
|
511 | if not key1 in key_replace: | |
498 | if key1 in key_point: |
|
512 | if key1 in key_point: | |
499 | self.dict[key1.replace('_', '.')] = value1 |
|
513 | self.dict[key1.replace('_', '.')] = value1 | |
500 | else: |
|
514 | else: | |
501 | self.dict[key1] = value1 |
|
515 | self.dict[key1] = value1 | |
502 |
|
516 | |||
503 | if query is not None: |
|
517 | if query is not None: | |
504 | if type(query) is dict: |
|
518 | if type(query) is dict: | |
505 | self.dict['fq_list'] = [] |
|
519 | self.dict['fq_list'] = [] | |
506 | #NUM_RESOURCES_MIN / NUM_RESOURCES_MAX |
|
520 | #NUM_RESOURCES_MIN / NUM_RESOURCES_MAX | |
507 | #----------------------------------------------------# |
|
521 | #----------------------------------------------------# | |
508 | if 'dataset_start_date' in query: |
|
522 | if 'dataset_start_date' in query: | |
509 | if type(query['dataset_start_date']) is str: |
|
523 | if type(query['dataset_start_date']) is str: | |
510 | try: |
|
524 | try: | |
511 | datetime.strptime(query['dataset_start_date'], '%Y-%m-%d') |
|
525 | datetime.strptime(query['dataset_start_date'], '%Y-%m-%d') | |
512 | if len(query['dataset_start_date']) != 10: |
|
526 | if len(query['dataset_start_date']) != 10: | |
513 | return '"dataset_start_date", must be: <YYYY-MM-DD>' |
|
527 | return '"dataset_start_date", must be: <YYYY-MM-DD>' | |
514 | self.dict['fq_list'].append('dataset_start_date:"'+query['dataset_start_date']+'"') |
|
528 | self.dict['fq_list'].append('dataset_start_date:"'+query['dataset_start_date']+'"') | |
515 | self.list.append('dataset_start_date') |
|
529 | self.list.append('dataset_start_date') | |
516 | except: |
|
530 | except: | |
517 | return '"dataset_start_date" incorrect: "%s"' % (query['dataset_start_date']) |
|
531 | return '"dataset_start_date" incorrect: "%s"' % (query['dataset_start_date']) | |
518 | else: |
|
532 | else: | |
519 | return '"dataset_start_date" must be <str>' |
|
533 | return '"dataset_start_date" must be <str>' | |
520 | #----------------------------------------------------# |
|
534 | #----------------------------------------------------# | |
521 | if 'dataset_end_date' in query: |
|
535 | if 'dataset_end_date' in query: | |
522 | if type(query['dataset_end_date']) is str: |
|
536 | if type(query['dataset_end_date']) is str: | |
523 | try: |
|
537 | try: | |
524 | datetime.strptime(query['dataset_end_date'], '%Y-%m-%d') |
|
538 | datetime.strptime(query['dataset_end_date'], '%Y-%m-%d') | |
525 | if len(query['dataset_end_date']) != 10: |
|
539 | if len(query['dataset_end_date']) != 10: | |
526 | return '"dataset_end_date", must be: <YYYY-MM-DD>' |
|
540 | return '"dataset_end_date", must be: <YYYY-MM-DD>' | |
527 |
|
541 | |||
528 | if 'dataset_start_date' in query: |
|
542 | if 'dataset_start_date' in query: | |
529 | if query['dataset_start_date'] > query['dataset_end_date']: |
|
543 | if query['dataset_start_date'] > query['dataset_end_date']: | |
530 | return '"dataset_end_date" must be greater than "dataset_start_date"' |
|
544 | return '"dataset_end_date" must be greater than "dataset_start_date"' | |
531 |
|
545 | |||
532 | self.dict['fq_list'].append('dataset_end_date:"'+query['dataset_end_date']+'"') |
|
546 | self.dict['fq_list'].append('dataset_end_date:"'+query['dataset_end_date']+'"') | |
533 | self.list.append('dataset_end_date') |
|
547 | self.list.append('dataset_end_date') | |
534 | except: |
|
548 | except: | |
535 | return '"dataset_end_date" incorrect: "%s"' % (query['dataset_end_date']) |
|
549 | return '"dataset_end_date" incorrect: "%s"' % (query['dataset_end_date']) | |
536 | else: |
|
550 | else: | |
537 | return '"dataset_end_date" must be <str>' |
|
551 | return '"dataset_end_date" must be <str>' | |
538 | #----------------------------------------------------# |
|
552 | #----------------------------------------------------# | |
539 | for key, value in query.items(): |
|
553 | for key, value in query.items(): | |
540 | if value is not None and not key in self.list: |
|
554 | if value is not None and not key in self.list: | |
541 | self.dict['fq_list'].append(str(key)+':"'+str(value)+'"') |
|
555 | self.dict['fq_list'].append(str(key)+':"'+str(value)+'"') | |
542 | else: |
|
556 | else: | |
543 | return '"query" must be <dict>' |
|
557 | return '"query" must be <dict>' | |
544 |
|
558 | |||
545 | return getattr(self.ckan.action, 'package_search')(include_private=True, **self.dict) |
|
559 | return getattr(self.ckan.action, 'package_search')(include_private=True, **self.dict) | |
546 |
|
560 | |||
547 | elif type_option == 'resource': |
|
561 | elif type_option == 'resource': | |
548 | for key1, value1 in kwargs.items(): |
|
562 | for key1, value1 in kwargs.items(): | |
549 | if key1 != 'fields': |
|
563 | if key1 != 'fields': | |
550 | self.dict[key1] = value1 |
|
564 | self.dict[key1] = value1 | |
551 |
|
565 | |||
552 | if query is not None: |
|
566 | if query is not None: | |
553 | if type(query) is dict: |
|
567 | if type(query) is dict: | |
554 | #----------------------------------------------------# |
|
568 | #----------------------------------------------------# | |
555 | if 'file_date_min' in query: |
|
569 | if 'file_date_min' in query: | |
556 | if type(query['file_date_min']) is str: |
|
570 | if type(query['file_date_min']) is str: | |
557 | try: |
|
571 | try: | |
558 | datetime.strptime(query['file_date_min'], '%Y-%m-%d') |
|
572 | datetime.strptime(query['file_date_min'], '%Y-%m-%d') | |
559 | if len(query['file_date_min']) != 10: |
|
573 | if len(query['file_date_min']) != 10: | |
560 | return '"file_date_min", must be: <YYYY-MM-DD>' |
|
574 | return '"file_date_min", must be: <YYYY-MM-DD>' | |
561 | except: |
|
575 | except: | |
562 | return '"file_date_min" incorrect: "%s"' % (query['file_date_min']) |
|
576 | return '"file_date_min" incorrect: "%s"' % (query['file_date_min']) | |
563 | else: |
|
577 | else: | |
564 | return '"file_date_min" must be <str>' |
|
578 | return '"file_date_min" must be <str>' | |
565 | #----------------------------------------------------# |
|
579 | #----------------------------------------------------# | |
566 | if 'file_date_max' in query: |
|
580 | if 'file_date_max' in query: | |
567 | if type(query['file_date_max']) is str: |
|
581 | if type(query['file_date_max']) is str: | |
568 | try: |
|
582 | try: | |
569 | datetime.strptime(query['file_date_max'], '%Y-%m-%d') |
|
583 | datetime.strptime(query['file_date_max'], '%Y-%m-%d') | |
570 | if len(query['file_date_max']) != 10: |
|
584 | if len(query['file_date_max']) != 10: | |
571 | return '"file_date_max", must be: <YYYY-MM-DD>' |
|
585 | return '"file_date_max", must be: <YYYY-MM-DD>' | |
572 |
|
586 | |||
573 | if 'file_date_min' in query: |
|
587 | if 'file_date_min' in query: | |
574 | if query['file_date_min'] > query['file_date_max']: |
|
588 | if query['file_date_min'] > query['file_date_max']: | |
575 | return '"file_date_max" must be greater than "file_date_min"' |
|
589 | return '"file_date_max" must be greater than "file_date_min"' | |
576 | except: |
|
590 | except: | |
577 | return '"file_date_max" incorrect: "%s"' % (query['file_date_max']) |
|
591 | return '"file_date_max" incorrect: "%s"' % (query['file_date_max']) | |
578 | else: |
|
592 | else: | |
579 | return '"file_date_max" must be <str>' |
|
593 | return '"file_date_max" must be <str>' | |
580 | #----------------------------------------------------# |
|
594 | #----------------------------------------------------# | |
581 | self.dict['query'] = query |
|
595 | self.dict['query'] = query | |
582 | else: |
|
596 | else: | |
583 | return '"query" must be <dict>' |
|
597 | return '"query" must be <dict>' | |
584 | return getattr(self.ckan.action, 'resources_search')(**self.dict) |
|
598 | return getattr(self.ckan.action, 'resources_search')(**self.dict) | |
585 |
|
599 | |||
586 | elif type_option == 'tag': |
|
600 | elif type_option == 'tag': | |
587 | for key1, value1 in kwargs.items(): |
|
601 | for key1, value1 in kwargs.items(): | |
588 | if key1 != 'fields': |
|
602 | if key1 != 'fields': | |
589 | self.dict[key1] = value1 |
|
603 | self.dict[key1] = value1 | |
590 |
|
604 | |||
591 | if not 'vocabulary_id' in kwargs: |
|
605 | if not 'vocabulary_id' in kwargs: | |
592 | print('Missing "vocabulary_id" value: tags that don’t belong to any vocabulary') |
|
606 | print('Missing "vocabulary_id" value: tags that don’t belong to any vocabulary') | |
593 | else: |
|
607 | else: | |
594 | print('Only tags that belong to "{}" vocabulary'.format(kwargs['vocabulary_id'])) |
|
608 | print('Only tags that belong to "{}" vocabulary'.format(kwargs['vocabulary_id'])) | |
595 |
|
609 | |||
596 | if query is not None: |
|
610 | if query is not None: | |
597 | if type(query) is dict: |
|
611 | if type(query) is dict: | |
598 | if 'search' in query: |
|
612 | if 'search' in query: | |
599 | if type(query['search']) is list or type(query['search']) is str: |
|
613 | if type(query['search']) is list or type(query['search']) is str: | |
600 | self.dict['query'] = query['search'] |
|
614 | self.dict['query'] = query['search'] | |
601 | else: |
|
615 | else: | |
602 | return '"search" must be <list> or <str>' |
|
616 | return '"search" must be <list> or <str>' | |
603 | else: |
|
617 | else: | |
604 | return '"query" must be <dict>' |
|
618 | return '"query" must be <dict>' | |
605 | return getattr(self.ckan.action, 'tag_search')(**self.dict) |
|
619 | return getattr(self.ckan.action, 'tag_search')(**self.dict) | |
606 |
|
620 | |||
607 | else: |
|
621 | else: | |
608 | return 'ERROR:: "type_option = %s" is not accepted' % (type_option) |
|
622 | return 'ERROR:: "type_option = %s" is not accepted' % (type_option) | |
609 |
|
623 | |||
610 | except: |
|
624 | except: | |
611 | _, exc_value, _ = sys.exc_info() |
|
625 | _, exc_value, _ = sys.exc_info() | |
612 | return exc_value |
|
626 | return exc_value | |
613 | else: |
|
627 | else: | |
614 | return 'ERROR:: "type_option" must be <str>' |
|
628 | return 'ERROR:: "type_option" must be <str>' | |
615 |
|
629 | |||
616 | def create(self, type_option, select=None, **kwargs): |
|
630 | def create(self, type_option, select=None, **kwargs): | |
617 | ''' |
|
631 | ''' | |
618 | FINALIDAD: |
|
632 | FINALIDAD: | |
619 | Funcion personalizada para crear. |
|
633 | Funcion personalizada para crear. | |
620 |
|
634 | |||
621 | PARAMETROS DISPONIBLES: |
|
635 | PARAMETROS DISPONIBLES: | |
622 | CONSULTAR: "GUIA DE SCRIPT.pdf" |
|
636 | CONSULTAR: "GUIA DE SCRIPT.pdf" | |
623 |
|
637 | |||
624 | ESTRUCTURA: |
|
638 | ESTRUCTURA: | |
625 | <access_name>.create(type_option = <class 'str'>, param_1 = <class 'param_1'>, ...) |
|
639 | <access_name>.create(type_option = <class 'str'>, param_1 = <class 'param_1'>, ...) | |
626 | ''' |
|
640 | ''' | |
627 | if type(type_option) is str: |
|
641 | if type(type_option) is str: | |
628 | try: |
|
642 | try: | |
629 | if type_option == 'dataset': |
|
643 | if type_option == 'dataset': | |
630 | return getattr(self.ckan.action, 'package_create')(**kwargs) |
|
644 | return getattr(self.ckan.action, 'package_create')(**kwargs) | |
631 | elif type_option == 'project': |
|
645 | elif type_option == 'project': | |
632 | return getattr(self.ckan.action, 'organization_create')(**kwargs) |
|
646 | return getattr(self.ckan.action, 'organization_create')(**kwargs) | |
633 | elif type_option == 'member': |
|
647 | elif type_option == 'member': | |
634 | return getattr(self.ckan.action, 'organization_member_create')(**kwargs) |
|
648 | return getattr(self.ckan.action, 'organization_member_create')(**kwargs) | |
635 | elif type_option == 'collaborator': |
|
649 | elif type_option == 'collaborator': | |
636 | return getattr(self.ckan.action, 'package_collaborator_create')(**kwargs) |
|
650 | return getattr(self.ckan.action, 'package_collaborator_create')(**kwargs) | |
637 | elif type_option == 'vocabulary': |
|
651 | elif type_option == 'vocabulary': | |
638 | return getattr(self.ckan.action, 'vocabulary_create')(**kwargs) |
|
652 | return getattr(self.ckan.action, 'vocabulary_create')(**kwargs) | |
639 | elif type_option == 'tag': |
|
653 | elif type_option == 'tag': | |
640 | return getattr(self.ckan.action, 'tag_create')(**kwargs) |
|
654 | return getattr(self.ckan.action, 'tag_create')(**kwargs) | |
641 | elif type_option == 'user': |
|
655 | elif type_option == 'user': | |
642 | return getattr(self.ckan.action, 'user_create')(**kwargs) |
|
656 | return getattr(self.ckan.action, 'user_create')(**kwargs) | |
643 | elif type_option == 'views': |
|
657 | elif type_option == 'views': | |
644 | if 'resource' == select: |
|
658 | if 'resource' == select: | |
645 | self.list = ['package'] |
|
659 | self.list = ['package'] | |
646 | for key1, value1 in kwargs.items(): |
|
660 | for key1, value1 in kwargs.items(): | |
647 | if not key1 in self.list: |
|
661 | if not key1 in self.list: | |
648 | self.dict[key1] = value1 |
|
662 | self.dict[key1] = value1 | |
649 | return getattr(self.ckan.action, 'resource_create_default_resource_views')(**self.dict) |
|
663 | return getattr(self.ckan.action, 'resource_create_default_resource_views')(**self.dict) | |
650 | elif 'dataset' == select: |
|
664 | elif 'dataset' == select: | |
651 | return getattr(self.ckan.action, 'package_create_default_resource_views')(**kwargs) |
|
665 | return getattr(self.ckan.action, 'package_create_default_resource_views')(**kwargs) | |
652 | else: |
|
666 | else: | |
653 | return 'ERROR:: "select = %s" is not accepted' % (select) |
|
667 | return 'ERROR:: "select = %s" is not accepted' % (select) | |
654 | else: |
|
668 | else: | |
655 | return 'ERROR:: "type_option = %s" is not accepted' % (type_option) |
|
669 | return 'ERROR:: "type_option = %s" is not accepted' % (type_option) | |
656 | except: |
|
670 | except: | |
657 | _, exc_value, _ = sys.exc_info() |
|
671 | _, exc_value, _ = sys.exc_info() | |
658 | return exc_value |
|
672 | return exc_value | |
659 | else: |
|
673 | else: | |
660 | return 'ERROR:: "type_option" must be <str>' |
|
674 | return 'ERROR:: "type_option" must be <str>' | |
661 |
|
675 | |||
662 | def patch(self, type_option, **kwargs): |
|
676 | def patch(self, type_option, **kwargs): | |
663 | ''' |
|
677 | ''' | |
664 | FINALIDAD: |
|
678 | FINALIDAD: | |
665 | Funciones personalizadas para actualizar |
|
679 | Funciones personalizadas para actualizar | |
666 |
|
680 | |||
667 | PARAMETROS DISPONIBLES: |
|
681 | PARAMETROS DISPONIBLES: | |
668 | CONSULTAR: "GUIA DE SCRIPT.pdf" |
|
682 | CONSULTAR: "GUIA DE SCRIPT.pdf" | |
669 |
|
683 | |||
670 | ESTRUCTURA: |
|
684 | ESTRUCTURA: | |
671 | <access_name>.patch(type_option = <class 'str'>, param_1 = <class 'param_1'>, ...) |
|
685 | <access_name>.patch(type_option = <class 'str'>, param_1 = <class 'param_1'>, ...) | |
672 | ''' |
|
686 | ''' | |
673 | if type(type_option) is str: |
|
687 | if type(type_option) is str: | |
674 | try: |
|
688 | try: | |
675 | if type_option == 'dataset': |
|
689 | if type_option == 'dataset': | |
676 | return getattr(self.ckan.action, 'package_patch')(**kwargs) |
|
690 | return getattr(self.ckan.action, 'package_patch')(**kwargs) | |
677 | elif type_option == 'project': |
|
691 | elif type_option == 'project': | |
678 | return getattr(self.ckan.action, 'organization_patch')(**kwargs) |
|
692 | return getattr(self.ckan.action, 'organization_patch')(**kwargs) | |
679 | elif type_option == 'resource': |
|
693 | elif type_option == 'resource': | |
680 | return getattr(self.ckan.action, 'resource_patch')(**kwargs) |
|
694 | return getattr(self.ckan.action, 'resource_patch')(**kwargs) | |
681 | elif type_option == 'member': |
|
695 | elif type_option == 'member': | |
682 | return getattr(self.ckan.action, 'organization_member_create')(**kwargs) |
|
696 | return getattr(self.ckan.action, 'organization_member_create')(**kwargs) | |
683 | elif type_option == 'collaborator': |
|
697 | elif type_option == 'collaborator': | |
684 | return getattr(self.ckan.action, 'package_collaborator_create')(**kwargs) |
|
698 | return getattr(self.ckan.action, 'package_collaborator_create')(**kwargs) | |
685 | else: |
|
699 | else: | |
686 | return 'ERROR:: "type_option = %s" is not accepted' % (type_option) |
|
700 | return 'ERROR:: "type_option = %s" is not accepted' % (type_option) | |
687 | except: |
|
701 | except: | |
688 | _, exc_value, _ = sys.exc_info() |
|
702 | _, exc_value, _ = sys.exc_info() | |
689 | return exc_value |
|
703 | return exc_value | |
690 | else: |
|
704 | else: | |
691 | return 'ERROR:: "type_option" must be <str>' |
|
705 | return 'ERROR:: "type_option" must be <str>' | |
692 |
|
706 | |||
693 | def delete(self, type_option, select=None, **kwargs): |
|
707 | def delete(self, type_option, select=None, **kwargs): | |
694 | ''' |
|
708 | ''' | |
695 | FINALIDAD: |
|
709 | FINALIDAD: | |
696 | Función personalizada para eliminar y/o purgar. |
|
710 | Función personalizada para eliminar y/o purgar. | |
697 |
|
711 | |||
698 | PARAMETROS DISPONIBLES: |
|
712 | PARAMETROS DISPONIBLES: | |
699 | CONSULTAR: "GUIA DE SCRIPT.pdf" |
|
713 | CONSULTAR: "GUIA DE SCRIPT.pdf" | |
700 |
|
714 | |||
701 | ESTRUCTURA: |
|
715 | ESTRUCTURA: | |
702 | <access_name>.delete(type_option = <class 'str'>, param_1 = <class 'param_1'>, ...) |
|
716 | <access_name>.delete(type_option = <class 'str'>, param_1 = <class 'param_1'>, ...) | |
703 | ''' |
|
717 | ''' | |
704 | if type(type_option) is str: |
|
718 | if type(type_option) is str: | |
705 | try: |
|
719 | try: | |
706 | if type_option == 'dataset': |
|
720 | if type_option == 'dataset': | |
707 | if select is None: |
|
721 | if select is None: | |
708 | return 'ERROR:: "select" must not be "None"' |
|
722 | return 'ERROR:: "select" must not be "None"' | |
709 | else: |
|
723 | else: | |
710 | if 'delete' == select: |
|
724 | if 'delete' == select: | |
711 | return getattr(self.ckan.action, 'package_delete')(**kwargs) |
|
725 | return getattr(self.ckan.action, 'package_delete')(**kwargs) | |
712 | elif 'purge' == select: |
|
726 | elif 'purge' == select: | |
713 | return getattr(self.ckan.action, 'dataset_purge')(**kwargs) |
|
727 | return getattr(self.ckan.action, 'dataset_purge')(**kwargs) | |
714 | else: |
|
728 | else: | |
715 | return 'ERROR:: "select = %s" is not accepted' % (select) |
|
729 | return 'ERROR:: "select = %s" is not accepted' % (select) | |
716 | elif type_option == 'project': |
|
730 | elif type_option == 'project': | |
717 | if select is None: |
|
731 | if select is None: | |
718 | return 'ERROR:: "select" must not be "None"' |
|
732 | return 'ERROR:: "select" must not be "None"' | |
719 | else: |
|
733 | else: | |
720 | if 'delete' == select: |
|
734 | if 'delete' == select: | |
721 | return getattr(self.ckan.action, 'organization_delete')(**kwargs) |
|
735 | return getattr(self.ckan.action, 'organization_delete')(**kwargs) | |
722 | elif 'purge' == select: |
|
736 | elif 'purge' == select: | |
723 | return getattr(self.ckan.action, 'organization_purge')(**kwargs) |
|
737 | return getattr(self.ckan.action, 'organization_purge')(**kwargs) | |
724 | else: |
|
738 | else: | |
725 | return 'ERROR:: "select = %s" is not accepted' % (select) |
|
739 | return 'ERROR:: "select = %s" is not accepted' % (select) | |
726 | elif type_option == 'resource': |
|
740 | elif type_option == 'resource': | |
727 | return getattr(self.ckan.action, 'resource_delete')(**kwargs) |
|
741 | return getattr(self.ckan.action, 'resource_delete')(**kwargs) | |
728 | elif type_option == 'vocabulary': |
|
742 | elif type_option == 'vocabulary': | |
729 | return getattr(self.ckan.action, 'vocabulary_delete')(**kwargs) |
|
743 | return getattr(self.ckan.action, 'vocabulary_delete')(**kwargs) | |
730 | elif type_option == 'tag': |
|
744 | elif type_option == 'tag': | |
731 | return getattr(self.ckan.action, 'tag_delete')(**kwargs) |
|
745 | return getattr(self.ckan.action, 'tag_delete')(**kwargs) | |
732 | elif type_option == 'user': |
|
746 | elif type_option == 'user': | |
733 | return getattr(self.ckan.action, 'user_delete')(**kwargs) |
|
747 | return getattr(self.ckan.action, 'user_delete')(**kwargs) | |
734 | else: |
|
748 | else: | |
735 | return 'ERROR:: "type_option = %s" is not accepted' % (type_option) |
|
749 | return 'ERROR:: "type_option = %s" is not accepted' % (type_option) | |
736 | except: |
|
750 | except: | |
737 | _, exc_value, _ = sys.exc_info() |
|
751 | _, exc_value, _ = sys.exc_info() | |
738 | return exc_value |
|
752 | return exc_value | |
739 | else: |
|
753 | else: | |
740 | return 'ERROR:: "type_option" must be <str>' |
|
754 | return 'ERROR:: "type_option" must be <str>' | |
741 |
|
755 | |||
742 | def f_status_note(self, total, result, path): |
|
756 | def f_status_note(self, total, result, path): | |
743 | file_txt = open(path+'status_note.txt', 'w') |
|
757 | file_txt = open(path+'status_note.txt', 'w') | |
744 | file_txt = open(path+'status_note.txt', 'a') |
|
758 | file_txt = open(path+'status_note.txt', 'a') | |
745 |
|
759 | |||
746 | file_txt.write('DOWNLOADED FILE(S): "%s"' % (len(result['name']))) |
|
760 | file_txt.write('DOWNLOADED FILE(S): "%s"' % (len(result['name']))) | |
747 | file_txt.write(''+ os.linesep) |
|
761 | file_txt.write(''+ os.linesep) | |
748 | for u in result['name']: |
|
762 | for u in result['name']: | |
749 | file_txt.write(' - '+ u + os.linesep) |
|
763 | file_txt.write(' - '+ u + os.linesep) | |
750 | file_txt.write(''+ os.linesep) |
|
764 | file_txt.write(''+ os.linesep) | |
751 |
|
765 | |||
752 | file_txt.write('FAILED FILE(S): "%s"' % (len(total['name'])-len(result['name']))) |
|
766 | file_txt.write('FAILED FILE(S): "%s"' % (len(total['name'])-len(result['name']))) | |
753 | file_txt.write(''+ os.linesep) |
|
767 | file_txt.write(''+ os.linesep) | |
754 | if len(total['name'])-len(result['name']) != 0: |
|
768 | if len(total['name'])-len(result['name']) != 0: | |
755 | for u in total['name']: |
|
769 | for u in total['name']: | |
756 | if not u in result['name']: |
|
770 | if not u in result['name']: | |
757 | file_txt.write(' - '+ u + os.linesep) |
|
771 | file_txt.write(' - '+ u + os.linesep) | |
758 | else: |
|
772 | else: | |
759 | file_txt.write(' "None"'+ os.linesep) |
|
773 | file_txt.write(' "None"'+ os.linesep) | |
760 |
|
774 | |||
761 | def f_name(self, name_dataset, ext, tempdir): |
|
775 | def f_name(self, name_dataset, ext, tempdir): | |
762 | while self.check: |
|
776 | while self.check: | |
763 | self.str = '' |
|
777 | self.str = '' | |
764 | if self.cont == 0: |
|
778 | if self.cont == 0: | |
765 | if os.path.exists(tempdir + name_dataset + ext): |
|
779 | if os.path.exists(tempdir + name_dataset + ext): | |
766 | self.str = name_dataset+'('+str(self.cont+1)+')'+ext |
|
780 | self.str = name_dataset+'('+str(self.cont+1)+')'+ext | |
767 | else: |
|
781 | else: | |
768 | self.check = self.check * 0 |
|
782 | self.check = self.check * 0 | |
769 | self.str = name_dataset + ext |
|
783 | self.str = name_dataset + ext | |
770 | else: |
|
784 | else: | |
771 | if not os.path.exists(tempdir + name_dataset+'('+str(self.cont)+')'+ext): |
|
785 | if not os.path.exists(tempdir + name_dataset+'('+str(self.cont)+')'+ext): | |
772 | self.check = self.check * 0 |
|
786 | self.check = self.check * 0 | |
773 | self.str = name_dataset+'('+str(self.cont)+')'+ ext |
|
787 | self.str = name_dataset+'('+str(self.cont)+')'+ ext | |
774 | self.cont = self.cont+1 |
|
788 | self.cont = self.cont+1 | |
775 | return self.str |
|
789 | return self.str | |
776 |
|
790 | |||
777 | def f_zipdir(self, path, ziph, zip_name): |
|
791 | def f_zipdir(self, path, ziph, zip_name): | |
778 | for root, _, files in os.walk(path): |
|
792 | for root, _, files in os.walk(path): | |
779 | print('.....') |
|
793 | print('.....') | |
780 | print('Creating: "{}" >>'.format(zip_name)) |
|
794 | print('Creating: "{}" >>'.format(zip_name)) | |
781 | for __file in tqdm(iterable=files, total=len(files)): |
|
795 | for __file in tqdm(iterable=files, total=len(files)): | |
782 | new_dir = os.path.relpath(os.path.join(root, __file), os.path.join(path, '..')) |
|
796 | new_dir = os.path.relpath(os.path.join(root, __file), os.path.join(path, '..')) | |
783 | ziph.write(os.path.join(root, __file), new_dir) |
|
797 | ziph.write(os.path.join(root, __file), new_dir) | |
784 | print('Created >>') |
|
798 | print('Created >>') | |
785 |
|
799 | |||
786 | def download_by_step(self, response, tempdir_name): |
|
800 | def download_by_step(self, response, tempdir_name): | |
787 | try: |
|
801 | try: | |
788 | with requests.get(response['url'], stream=True, headers={'Authorization': self.Authorization}) as resp: |
|
802 | # ---------- REPLACE URL --------- # | |
|
803 | if urlparse(self.url).netloc != 'www.igp.gob.pe' and urlparse(response['url']).netloc == 'www.igp.gob.pe': | |||
|
804 | response['url'] = response['url'].replace(urlparse(response['url']).scheme + '://' + urlparse(response['url']).netloc, | |||
|
805 | urlparse(self.url).scheme + '://' + urlparse(self.url).netloc) | |||
|
806 | #----------------------------------# | |||
|
807 | with requests.get(response['url'], stream=True, headers={'Authorization': self.Authorization}, verify=self.verify) as resp: | |||
789 | if resp.status_code == 200: |
|
808 | if resp.status_code == 200: | |
790 | with open(tempdir_name+response['name'], 'wb') as file: |
|
809 | with open(tempdir_name+response['name'], 'wb') as file: | |
791 | for chunk in resp.iter_content(chunk_size = self.chunk_size): |
|
810 | for chunk in resp.iter_content(chunk_size = self.chunk_size): | |
792 | if chunk: |
|
811 | if chunk: | |
793 | file.write(chunk) |
|
812 | file.write(chunk) | |
794 | except requests.exceptions.RequestException: |
|
813 | except requests.exceptions.RequestException: | |
795 | pass |
|
814 | pass | |
796 |
|
815 | |||
797 | def download_files(self, **kwargs): |
|
816 | def download_files(self, **kwargs): | |
798 | ''' |
|
817 | ''' | |
799 | FINALIDAD: |
|
818 | FINALIDAD: | |
800 | Funcion personalizada para la descarga de archivos existentes de un dataset. |
|
819 | Funcion personalizada para la descarga de archivos existentes de un dataset. | |
801 |
|
820 | |||
802 | PARAMETROS DISPONIBLES: |
|
821 | PARAMETROS DISPONIBLES: | |
803 | CONSULTAR: "GUIA DE SCRIPT.pdf" |
|
822 | CONSULTAR: "GUIA DE SCRIPT.pdf" | |
804 |
|
823 | |||
805 | ESTRUCTURA: |
|
824 | ESTRUCTURA: | |
806 | <access_name>.download_files(id = <class 'str'>, param_1 = <class 'param_1'>, ...) |
|
825 | <access_name>.download_files(id = <class 'str'>, param_1 = <class 'param_1'>, ...) | |
807 | ''' |
|
826 | ''' | |
808 | dict_local = {} |
|
827 | dict_local = {} | |
809 | #----------------------------------------------# |
|
828 | #----------------------------------------------# | |
810 | if 'zip' in kwargs: |
|
829 | if 'zip' in kwargs: | |
811 | if type(kwargs['zip']) is not bool: |
|
830 | if type(kwargs['zip']) is not bool: | |
812 | return 'ERROR:: "zip" must be: <class "bool">' |
|
831 | return 'ERROR:: "zip" must be: <class "bool">' | |
813 | else: |
|
832 | else: | |
814 | dict_local['zip'] = kwargs['zip'] |
|
833 | dict_local['zip'] = kwargs['zip'] | |
815 | else: |
|
834 | else: | |
816 | dict_local['zip'] = False |
|
835 | dict_local['zip'] = False | |
817 | #----------------------------------------------# |
|
836 | #----------------------------------------------# | |
818 | if 'status_note' in kwargs: |
|
837 | if 'status_note' in kwargs: | |
819 | if type(kwargs['status_note']) is not bool: |
|
838 | if type(kwargs['status_note']) is not bool: | |
820 | return 'ERROR:: "status_note" must be: <class "bool">' |
|
839 | return 'ERROR:: "status_note" must be: <class "bool">' | |
821 | else: |
|
840 | else: | |
822 | dict_local['status_note'] = kwargs['status_note'] |
|
841 | dict_local['status_note'] = kwargs['status_note'] | |
823 | else: |
|
842 | else: | |
824 | dict_local['status_note'] = False |
|
843 | dict_local['status_note'] = False | |
825 | #----------------------------------------------# |
|
844 | #----------------------------------------------# | |
826 | if 'path' in kwargs: |
|
845 | if 'path' in kwargs: | |
827 | if type(kwargs['path']) is str: |
|
846 | if type(kwargs['path']) is str: | |
828 | if os.path.isdir(kwargs['path']) == False: |
|
847 | if os.path.isdir(kwargs['path']) == False: | |
829 | return 'ERROR:: "path" does not exist' |
|
848 | return 'ERROR:: "path" does not exist' | |
830 | else: |
|
849 | else: | |
831 | if kwargs['path'][-1:] != self.separator: |
|
850 | if kwargs['path'][-1:] != self.separator: | |
832 | dict_local['path'] = kwargs['path']+self.separator |
|
851 | dict_local['path'] = kwargs['path']+self.separator | |
833 | else: |
|
852 | else: | |
834 | dict_local['path'] = kwargs['path'] |
|
853 | dict_local['path'] = kwargs['path'] | |
835 |
|
854 | |||
836 | txt = dict_local['path']+datetime.now().strftime("%Y_%m_%d_%H_%M_%S_%f")+'.txt' |
|
855 | txt = dict_local['path']+datetime.now().strftime("%Y_%m_%d_%H_%M_%S_%f")+'.txt' | |
837 | if int(platform.python_version()[0]) == 3: |
|
856 | if int(platform.python_version()[0]) == 3: | |
838 | try: |
|
857 | try: | |
839 | file_txt = open(txt, 'w') |
|
858 | file_txt = open(txt, 'w') | |
840 | file_txt.close() |
|
859 | file_txt.close() | |
841 | os.remove(txt) |
|
860 | os.remove(txt) | |
842 | except PermissionError: |
|
861 | except PermissionError: | |
843 | return 'ERROR:: Access denied, you are not authorized to write files: "%s"' % (dict_local['path']) |
|
862 | return 'ERROR:: Access denied, you are not authorized to write files: "%s"' % (dict_local['path']) | |
844 | else: |
|
863 | else: | |
845 | try: |
|
864 | try: | |
846 | file_txt = open(txt, 'w') |
|
865 | file_txt = open(txt, 'w') | |
847 | file_txt.close() |
|
866 | file_txt.close() | |
848 | os.remove(txt) |
|
867 | os.remove(txt) | |
849 | except: |
|
868 | except: | |
850 | return 'ERROR:: Access denied, you are not authorized to write files: "%s"' % (dict_local['path']) |
|
869 | return 'ERROR:: Access denied, you are not authorized to write files: "%s"' % (dict_local['path']) | |
851 | else: |
|
870 | else: | |
852 | return 'ERROR:: "path" must be: <class "str">' |
|
871 | return 'ERROR:: "path" must be: <class "str">' | |
853 | else: |
|
872 | else: | |
854 | dict_local['path'] = '' |
|
873 | dict_local['path'] = '' | |
855 | #----------------------------------------------# |
|
874 | #----------------------------------------------# | |
856 | for key, value in kwargs.items(): |
|
875 | for key, value in kwargs.items(): | |
857 | if not key in dict_local: |
|
876 | if not key in dict_local: | |
858 | self.dict[key] = value |
|
877 | self.dict[key] = value | |
859 | try: |
|
878 | try: | |
860 | response = getattr(self.ckan.action, 'url_resources')(**self.dict) |
|
879 | response = getattr(self.ckan.action, 'url_resources')(**self.dict) | |
861 | except: |
|
880 | except: | |
862 | _, exc_value, _ = sys.exc_info() |
|
881 | _, exc_value, _ = sys.exc_info() | |
863 | return exc_value |
|
882 | return exc_value | |
864 |
|
883 | |||
865 | if len(response) != 0: |
|
884 | if len(response) != 0: | |
866 | #--------------TEMP PATH---------------# |
|
885 | #--------------TEMP PATH---------------# | |
867 | if dict_local['zip']: |
|
886 | if dict_local['zip']: | |
868 | tempdir = tempfile.mkdtemp(prefix=kwargs['id']+'-')+self.separator |
|
887 | tempdir = tempfile.mkdtemp(prefix=kwargs['id']+'-')+self.separator | |
869 | os.mkdir(tempdir+kwargs['id']) |
|
888 | os.mkdir(tempdir+kwargs['id']) | |
870 | dir_name = tempdir + kwargs['id'] + self.separator |
|
889 | dir_name = tempdir + kwargs['id'] + self.separator | |
871 | else: |
|
890 | else: | |
872 | dir = self.f_name(kwargs['id'], '', dict_local['path']) |
|
891 | dir = self.f_name(kwargs['id'], '', dict_local['path']) | |
873 | os.mkdir(dict_local['path'] + dir) |
|
892 | os.mkdir(dict_local['path'] + dir) | |
874 | dir_name = dict_local['path'] + dir + self.separator |
|
893 | dir_name = dict_local['path'] + dir + self.separator | |
875 | #-----------DOWNLOAD FILES-------------# |
|
894 | #-----------DOWNLOAD FILES-------------# | |
876 | print('.....') |
|
895 | print('.....') | |
877 | print('Downloading "{}" file(s) >>'.format(len(response))) |
|
896 | print('Downloading "{}" file(s) >>'.format(len(response))) | |
878 | name_total = {'name': []} |
|
897 | name_total = {'name': []} | |
879 | with concurrent.futures.ThreadPoolExecutor() as executor: |
|
898 | with concurrent.futures.ThreadPoolExecutor() as executor: | |
880 | for u in tqdm(iterable=response, total=len(response)): |
|
899 | for u in tqdm(iterable=response, total=len(response)): | |
881 | name_total['name'].append(u['name']) |
|
900 | name_total['name'].append(u['name']) | |
882 | executor.submit(self.download_by_step, u, dir_name) |
|
901 | executor.submit(self.download_by_step, u, dir_name) | |
883 | name_check = {} |
|
902 | name_check = {} | |
884 | name_check['name'] = [f for f in os.listdir(dir_name) if os.path.isfile(os.path.join(dir_name, f))] |
|
903 | name_check['name'] = [f for f in os.listdir(dir_name) if os.path.isfile(os.path.join(dir_name, f))] | |
885 | print('"{}" downloaded file(s) successfully >>'.format(len(name_check['name']))) |
|
904 | print('"{}" downloaded file(s) successfully >>'.format(len(name_check['name']))) | |
886 | #--------------------------------------# |
|
905 | #--------------------------------------# | |
887 | if len(name_check['name']) != 0: |
|
906 | if len(name_check['name']) != 0: | |
888 | #----------Status Note---------# |
|
907 | #----------Status Note---------# | |
889 | if dict_local['status_note']: |
|
908 | if dict_local['status_note']: | |
890 | print('.....') |
|
909 | print('.....') | |
891 | print('Creating: "status_note.txt" >>') |
|
910 | print('Creating: "status_note.txt" >>') | |
892 | self.f_status_note(name_total, name_check, dir_name) |
|
911 | self.f_status_note(name_total, name_check, dir_name) | |
893 | print('Created>>') |
|
912 | print('Created>>') | |
894 | #----------ZIP CREATE----------# |
|
913 | #----------ZIP CREATE----------# | |
895 | if dict_local['zip']: |
|
914 | if dict_local['zip']: | |
896 | zip_name = self.f_name(kwargs['id'], '.zip', dict_local['path']) |
|
915 | zip_name = self.f_name(kwargs['id'], '.zip', dict_local['path']) | |
897 | ziph = zipfile.ZipFile(dict_local['path'] + zip_name, 'w', zipfile.ZIP_DEFLATED, allowZip64=True) |
|
916 | ziph = zipfile.ZipFile(dict_local['path'] + zip_name, 'w', zipfile.ZIP_DEFLATED, allowZip64=True) | |
898 | self.f_zipdir(dir_name, ziph, zip_name) |
|
917 | self.f_zipdir(dir_name, ziph, zip_name) | |
899 | ziph.close() |
|
918 | ziph.close() | |
900 | #Delete Temporal Path |
|
919 | #Delete Temporal Path | |
901 | if os.path.exists(tempdir[:-1]): |
|
920 | if os.path.exists(tempdir[:-1]): | |
902 | shutil.rmtree(tempdir[:-1]) |
|
921 | shutil.rmtree(tempdir[:-1]) | |
903 | #------------------------------# |
|
922 | #------------------------------# | |
904 | print('.....') |
|
923 | print('.....') | |
905 | return 'DOWNLOAD FINISHED' |
|
924 | return 'DOWNLOAD FINISHED' | |
906 | else: |
|
925 | else: | |
907 | #Delete Temporal Path |
|
926 | #Delete Temporal Path | |
908 | if dict_local['zip']: |
|
927 | if dict_local['zip']: | |
909 | if os.path.exists(tempdir[:-1]): |
|
928 | if os.path.exists(tempdir[:-1]): | |
910 | shutil.rmtree(tempdir[:-1]) |
|
929 | shutil.rmtree(tempdir[:-1]) | |
911 | else: |
|
930 | else: | |
912 | if os.path.exists(dir_name[:-1]): |
|
931 | if os.path.exists(dir_name[:-1]): | |
913 | shutil.rmtree(dir_name[:-1]) |
|
932 | shutil.rmtree(dir_name[:-1]) | |
914 | return 'NO FILES WERE DOWNLOADED' |
|
933 | return 'NO FILES WERE DOWNLOADED' | |
915 | else: |
|
934 | else: | |
916 | return 'FILES NOT FOUND' No newline at end of file |
|
935 | return 'FILES NOT FOUND' |
General Comments 0
You need to be logged in to leave comments.
Login now