Last active
September 23, 2018 14:34
-
-
Save FrieseWoudloper/590d428afce5d2d873bd6b4b81acc1d9 to your computer and use it in GitHub Desktop.
Example Python code and XML-file for editing metadata in ArcGIS Online
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<metadata xml:lang="en"> | |
<dataIdInfo> | |
<idAbs>Testing if I can update the metadata of an item in AOL</idAbs> | |
</dataIdInfo> | |
</metadata> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Kudos to Thomas Maschler for the code! | |
# https://gist.github.com/thomas-maschler/388554ddb6deae6371db | |
import requests | |
import json | |
def request_token(user, password): | |
d = {"username": user, | |
"password": password, | |
"referer":"http://www.arcgis.com", | |
"f": "json"} | |
url = "https://www.arcgis.com/sharing/rest/generateToken" | |
r = requests.post(url, data = d) | |
response = json.loads(r.content) | |
if 'error' in response.keys(): | |
raise Exception(response['message'], response['details']) | |
return response | |
def update_metadata(user, item, token, metadata): | |
d = {"overwrite": "true", | |
"token" :token, | |
"f":"json"} | |
f = {'metadata': ('metadata.xml', open(metadata, 'rb'), 'text/xml', {'Expires': '0'})} | |
url = 'http://www.arcgis.com/sharing/rest/content/users/{0}/items/{1}/update'.format(user, item) | |
r = requests.post(url, data = d, files = f) | |
response = json.loads(r.content) | |
if 'error' in response.keys(): | |
raise Exception(response['message'], response['details']) | |
return response | |
if __name__ == "__main__": | |
metadata = "the path to your metadata XML-file should go here" | |
user = "your username should go here" | |
password = "your password should go here" | |
item = "your item id should go here" | |
token = request_token(user, password)['token'] | |
print(update_metadata(user, item, token, metadata)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment