Created
January 30, 2013 18:08
-
-
Save lawlesst/4675289 to your computer and use it in GitHub Desktop.
Merge VIVO resoruces.
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
""" | |
Script to login to the VIVO interface as use the | |
'Merge Resources' tool under 'Ingest Tools' in the admin interface. | |
An incoming CSV file without headers is expected. | |
- the primary resource URI is in the first column | |
- the duplicate resource URI is in the second column | |
This script relies on the python requests package: | |
http://docs.python-requests.org/en/latest/user/install/ | |
Run as: | |
$python merge_resources.py duplicate_file.csv | |
Use with caution as it merges data in your system. | |
""" | |
import csv | |
import os | |
import sys | |
import requests | |
#Before running export the following environment variables | |
#or change the user, password, and base_url variables below. | |
#export VIVO_USER=user | |
#export VIVO_PASS=password | |
#export VIOV_USER=http://vivo.school.edu/ | |
user = os.getenv('VIVO_USER') | |
password = os.getenv('VIVO_PASS') | |
base_url = os.getenv('VIVO_URL') | |
if not base_url.endswith('/'): | |
base_url += '/' | |
if (user is None) or (password is None) or (base_url is None): | |
raise Exception(""" | |
VIVO credentials not found. | |
Environment variables need to be set. | |
$export VIVO_USER=user | |
$export VIVO_PASS=password | |
$export VIVO_URL=http://vivo.edu/ | |
""") | |
payload = { | |
'loginName': user, | |
'loginPassword': password, | |
'loginForm': 'Log in' | |
} | |
#VIVO session | |
session = requests.session() | |
#login | |
login = session.post( | |
base_url + 'authenticate', | |
data=payload, | |
verify=False | |
) | |
if login.status_code != 200: | |
raise Exception("VIVO login failed.") | |
#Open and loop through incoming csv file. | |
with open(sys.argv[1]) as infile: | |
#Not expecting headers in the file. | |
#Expecting primary uri in column 0 and secondary uri in column 1 | |
for index, row in enumerate(csv.reader(infile)): | |
uri1 = row[0] | |
uri2 = row[1] | |
print>>sys.stderr, "Merging", uri1, uri2 | |
params = {'action': 'mergeResources', | |
'uri1': uri1, | |
'uri2': uri2, | |
'usePrimaryLabelOnly': 'Use Primary Label Only', | |
'submit': 'Merge resources'} | |
#Call merge - it's a GET! | |
merge = session.get(base_url + 'ingest', params=params) | |
if merge.url == base_url + 'authenticate': | |
raise Exception("Failed to login to VIVO.") | |
if merge.status_code != 200: | |
raise Exception("Merge failed; requests output: %s" % merge.content) | |
logout = session.get(base_url + 'logout') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment