Skip to content

Instantly share code, notes, and snippets.

@jeffgerhard
Last active May 24, 2016 19:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffgerhard/4b55a69127aecf861c7a05a7e0b8f5be to your computer and use it in GitHub Desktop.
Save jeffgerhard/4b55a69127aecf861c7a05a7e0b8f5be to your computer and use it in GitHub Desktop.
batch run z39.50 query and rename results per spreadsheet values
# adapted from https://github.com/asl2/PyZ3950/blob/master/example/zmarc_example.py
# for python2 [i couldn't get PyZ3950 to work in 3]
# this is a basic code to:
# - read bib record numbers from a csv spreadsheet 'worksheet.csv';
# - run z39.50 query and save MARC records locally; and
# - rename them according to the second column in spreadsheet
#
# PyZ3950 found at https://github.com/asl2/PyZ3950
# and requires PLY found at http://www.dabeaz.com/ply/
#
# sample csv rows:
# aiddushin_xxxx_1990_000_7835746,b459650
# aaasprofessi_chal_1980_000_6647977,b148064
# .... etc.
from PyZ3950 import zoom
# it's an older code sir but it checks out
import time
import csv
def RunQuery(id,b):
"""Run a Z39.50 query & save MARC results to files"""
zquery = '@attr 1=12 "' + b + '"'
# zoom specs are here http://www.indexdata.com/yaz/doc/zoom.records.html
#open connection
conn = zoom.Connection ('gull.georgetown.edu', 210)
# note that GULL server requires whitelisted IP address to connect
conn.databaseName = 'innopac'
conn.preferredRecordSyntax = 'USMARC'
#setup query
query = zoom.Query('PQF', zquery)
#run query
res = conn.search(query)
filename = "C:\\Users\\gerhardj\\Documents\\MARCtests\\" + id + ".mrc"
fx = open(filename, "wb")
fx.write(res[0].data)
fx.close()
conn.close()
if __name__ == '__main__':
with open('worksheet.csv', 'r') as f:
reader = csv.reader(f)
for row in reader:
RunQuery(row[0],row[1])
time.sleep(3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment