use tempfile for datagov resource upload
Former-commit-id: 80eae32f084aac5c6ff933def7e5cb072c21b8fc
--- a/documents/datagov-export.py
+++ b/documents/datagov-export.py
@@ -12,45 +12,17 @@
class LoaderError(Exception):
pass
-
+import tempfile
def add_package_resource_cachedurl(ckan, package_name, url, name, format, size, **kwargs):
- # fileupload
- ts = datetime.datetime.isoformat(datetime.datetime.now()).replace(':', '').split('.')[0]
-
- file_key = os.path.join(ts, name)
-
- auth_dict = ckan.storage_auth_get('/form/' + file_key, {})
-
- fields = [(kv['name'].encode('ascii'), kv['value'].encode('ascii'))
- for kv in auth_dict['fields']]
(url, mime_type, content) = scrape.fetchURL(scrape.docsdb,
url, "dataset_resource", "AGIMO", False)
-
- files = [('file', os.path.basename(file_key), content)]
-
- errcode, body = ckan._post_multipart(auth_dict['action'].encode('ascii'), fields, files)
-
- if errcode == 200:
- file_metadata = ckan.storage_metadata_get(file_key)
- (url, msg) = file_metadata['_location'], ''
- else:
- (url, msg) = '', body
- # fileupload done
-
- if url == '':
- raise CkanApiError(msg)
- m = hashlib.sha1(msg)
- #todo mime-type dectection based on content
- r = dict(name=name,
- mimetype=mime_type,
- hash=m.hexdigest(), size=size, url=url)
-
- r.update(kwargs)
- if not r.has_key('name'): r['name'] = url
-
- p = ckan.package_entity_get(package_name)
- p['resources'].append(r)
- return ckan.package_entity_put(p)
+ tf = tempfile.NamedTemporaryFile()
+ tfName = tf.name
+ tf.seek(0)
+ tf.write(content)
+ tf.flush()
+ add_package_resource (ckan, package_name, tfName, name=name, format=format, size=size)
+
# Instantiate the CKAN client.