Skip to content

Instantly share code, notes, and snippets.

@fprieur
Created December 22, 2014 22:58
Show Gist options
  • Save fprieur/8e10ac4daf99366aac04 to your computer and use it in GitHub Desktop.
Save fprieur/8e10ac4daf99366aac04 to your computer and use it in GitHub Desktop.
Ckan resource updater
# -*- coding: utf8 -*-
import json
import requests
from ConfigParser import SafeConfigParser
from urlparse import urlparse, urlunparse
import urllib
config = SafeConfigParser()
config.read('field_uploader.ini')
params = {'limit': '1000'}
r = requests.get(config.get("local", "package_with_resource_url"),
params=params)
binary = r.content
output = json.loads(binary)
resources_ids_with_bad_url = []
for package in output["result"]:
for resource in package["resources"]:
resource_url = urlparse(resource["url"])
if resource_url.netloc == "ckanprod":
resources_ids_with_bad_url.append(
resource["id"])
for id in resources_ids_with_bad_url:
params = {'id': id}
current_resource_to_update = {}
current_resource_to_update = requests.get('http://localhost/api/3/action/resource_show',
params=params)
data = current_resource_to_update.json()
url_to_change = ""
url_to_change = data["result"]["url"]
a = urlparse(url_to_change)
url_modify = urlunparse((a.scheme,
"donnees.ville.montreal.qc.ca",
a.path,
a.params,
a.query,
a.fragment))
params = {
'id': id,
'url': url_modify,
'name': a.netloc,
}
params = urllib.quote(json.dumps(params).encode('utf-8'))
headers = {'Authorization': '8e901fde-8b3e-4782-bae0-7f462e814e92',
'Content-type': 'application/x-www-form-urlencoded'}
a = requests.post('http://localhost/api/3/action/resource_update',
data=params, headers=headers)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment