-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoclcTitlePhraseBorrowDirect.py
153 lines (145 loc) · 6.33 KB
/
oclcTitlePhraseBorrowDirect.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import requests
from bs4 import BeautifulSoup
import csv
import secrets
import urllib
import re
import time
import datetime
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-f', '--fileName', help='the file of Borrow Direct data. \
optional - if not provided, the script will ask for input')
args = parser.parse_args()
if args.fileName:
fileName = args.fileName
else:
fileName = input('Enter the file of Borrow Direct data: ')
startTime = time.time()
fileNameWithoutExtension = fileName[:fileName.index('.')]
baseURL = 'http://www.worldcat.org/webservices/catalog/search/opensearch?q='
baseURL2 = 'http://www.worldcat.org/webservices/catalog/content/'
with open(fileName) as csvfile:
reader = csv.DictReader(csvfile)
rowCount = len(list(reader))
wskey = secrets.wskey
f = csv.writer(open(fileNameWithoutExtension + 'oclcSearchMatches.csv', 'w'))
f.writerow(['searchOclcNum'] + ['borrower'] + ['lender'] + ['status']
+ ['patronType'] + ['isbn'] + ['searchTitle'] + ['searchAuthor']
+ ['searchDate'] + ['oclcNum'] + ['oclcTitle'] + ['oclcAuthor']
+ ['oclcPublisher'] + ['callNumLetters'] + ['callNumFull']
+ ['physDesc'] + ['oclcDate'])
f2 = csv.writer(open(fileNameWithoutExtension + 'oclcSearchNonMatches.csv',
'w'))
f2.writerow(['searchOoclcNum'] + ['borrower'] + ['lender'] + ['status']
+ ['patronType'] + ['isbn'] + ['searchTitle'] + ['searchAuthor']
+ ['searchDate'])
with open(fileName) as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
rowCount -= 1
print('Items remaining: ', rowCount)
borrower = row['BORROWER']
lender = row['LENDER']
status = row['STATUS']
patronType = row['PATRON TYPE']
searchOclcNum = row['OCLC']
print(searchOclcNum)
isbn = row['ISBN']
searchAuthor = row['AUTHOR']
searchTitle = row['TITLE']
searchPublisher = row['PUBLISHER']
searchDate = row['PUBLICATION YEAR']
try:
response = requests.get(
'http://www.worldcat.org/webservices/catalog/content/'
+ searchOclcNum + '?format=rss&wskey='
+ wskey)
response = response.content
record = BeautifulSoup(response, "lxml").find('record')
oclcNum = record.find('controlfield', {'tag': '001'}).text
print('search oclc #')
except ValueError:
originalTitle = searchTitle
search = urllib.quote(searchTitle)
response = requests.get(baseURL + search.strip()
+ '&count=1&format=rss&wskey=' + wskey)
print('search title')
response = response.content
record = BeautifulSoup(response, "lxml").findAll('item')
if record != []:
record = record[0]
url = record.find('guid').text
oclcNum = url.replace('http://worldcat.org/oclc/', '')
oclcAuthor = record.find('author').find('name').text
serviceLevel = '?servicelevel=full'
classScheme = '&classificationScheme=LibraryOfCongress'
response2 = requests.get(baseURL2 + oclcNum
+ serviceLevel + classScheme
+ '&wskey=' + wskey)
print('search full record')
response2 = response2.content
try:
record2 = BeautifulSoup(response2, "lxml").find('record')
try:
titleA = record2.find('datafield', {'tag': '245'})
titleA = titleA.find('subfield', {'code': 'a'}).text
except ValueError:
titleA = ''
try:
titleB = record2.find('datafield', {'tag': '245'})
titleB = titleB.find('subfield', {'code': 'b'}).text
except ValueError:
titleB = ''
oclcTitle = titleA + ' ' + titleB
oclcDate = record2.find('controlfield', {'tag': '008'}).text[7:11]
try:
callNumFullA = record2.find('datafield', {'tag': '050'})
callNumFullA = callNumFullA.find('subfield', {'code': 'a'})
callNumFullA = callNumFullA.text
numStart = re.search('\\d', callNumFullA)
callNumLetters = callNumFullA[:numStart.start()]
except ValueError:
callNumFullA = ''
callNumLetters = ''
try:
callNumFullB = record2.find('datafield', {'tag': '050'})
callNumFullB = callNumFullB.find('subfield', {'code': 'b'})
callNumFullB = callNumFullB.text
except ValueError:
callNumFullB = ''
callNumFull = callNumFullA + ' ' + callNumFullB
try:
oclcPub = record2.find('datafield', {'tag': '260'})
oclcPub = oclcPub.find('subfield', {'code': 'b'})
oclcPub = oclcPub.text
except ValueError:
try:
oclcPub = record2.find('datafield', {'tag': '264'})
oclcPub = oclcPub.find('subfield', {'code': 'b'})
oclcPub = oclcPub.text
except ValueError:
oclcPub = ''
try:
physDesc = record2.find('datafield', {'tag': '300'})
physDesc = physDesc.find('subfield', {'code': 'a'}).text
except ValueError:
physDesc = ''
f.writerow([searchOclcNum] + [borrower] + [lender] + [status]
+ [patronType] + [isbn] + [searchTitle] + [searchAuthor]
+ [searchDate] + [oclcNum] + [oclcTitle] + [oclcAuthor]
+ [oclcPub] + [callNumLetters] + [callNumFull]
+ [physDesc] + [oclcDate])
oclcNum = ''
oclcTitle = ''
oclcAuthor = ''
callNumLetters = ''
callNumFull = ''
oclcPub = ''
oclcDate = ''
except ValueError:
f2.writerow([searchOclcNum] + [borrower] + [lender] + [status]
+ [patronType] + [isbn] + [searchTitle]
+ [searchAuthor] + [searchDate])
td = datetime.timedelta(seconds=time.time() - startTime)
print("Elapsed time: {}".format(td))