Skip to content

Commit 1c9ca54

Browse files
committed
add argparse
1 parent 4fd7fbd commit 1c9ca54

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

oclcTitlePhraseBorrowDirect.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,28 @@
55
import urllib
66
import re
77
import time
8+
import argparse
9+
10+
parser = argparse.ArgumentParser()
11+
parser.add_argument('-f', '--fileName', help='the file of Borrow Direct data. optional - if not provided, the script will ask for input')
12+
args = parser.parse_args()
13+
14+
if args.fileName:
15+
fileName = args.fileName
16+
else:
17+
fileName = raw_input('Enter the file of Borrow Direct data: ')
818

919
startTime = time.time()
1020

11-
fileName = raw_input('Enter file name: ')
1221
fileNameWithoutExtension = fileName[:fileName.index('.')]
1322

1423
baseURL = 'http://www.worldcat.org/webservices/catalog/search/opensearch?q='
1524
baseURL2 = 'http://www.worldcat.org/webservices/catalog/content/'
1625

26+
with open(fileName) as csvfile:
27+
reader = csv.DictReader(csvfile)
28+
rowCount = len(list(reader))
29+
1730
wskey = secrets.wskey
1831
f=csv.writer(open(fileNameWithoutExtension+'oclcSearchMatches.csv', 'wb'))
1932
f.writerow(['searchOclcNum']+['borrower']+['lender']+['status']+['patronType']+['isbn']+['searchTitle']+['searchAuthor']+['searchDate']+['oclcNum']+['oclcTitle']+['oclcAuthor']+['oclcPublisher']+['callNumLetters']+['callNumFull']+['physDesc']+['oclcDate'])
@@ -22,6 +35,8 @@
2235
with open(fileName) as csvfile:
2336
reader = csv.DictReader(csvfile)
2437
for row in reader:
38+
rowCount -= 1
39+
print 'Items remaining: ', rowCount
2540
borrower = row['BORROWER']
2641
lender = row['LENDER']
2742
status = row['STATUS']
@@ -34,22 +49,27 @@
3449
searchPublisher = row['PUBLISHER']
3550
searchDate = row['PUBLICATION YEAR']
3651
try:
37-
response = requests.get('http://www.worldcat.org/webservices/catalog/content/'+searchOclcNum+'?format=rss&wskey='+wskey).content
52+
response = requests.get('http://www.worldcat.org/webservices/catalog/content/'+searchOclcNum+'?format=rss&wskey='+wskey)
53+
response = response.content
3854
record = BeautifulSoup(response, "lxml").find('record')
3955
oclcNum = record.find('controlfield', {'tag' : '001'}).text
56+
print 'search oclc #'
4057
except:
4158
originalTitle = searchTitle
4259
search = urllib.quote(searchTitle)
43-
print search
44-
response = requests.get(baseURL+search.strip()+'&count=1&format=rss&wskey='+wskey).content
60+
response = requests.get(baseURL+search.strip()+'&count=1&format=rss&wskey='+wskey)
61+
print 'search title'
62+
response = response.content
4563
record = BeautifulSoup(response, "lxml").findAll('item')
4664
if record != []:
4765
record = record[0]
4866
url = record.find('guid').text.encode('utf-8')
4967
oclcNum = url.replace('http://worldcat.org/oclc/','')
5068
oclcAuthor = record.find('author').find('name').text.encode('utf-8')
5169

52-
response2 = requests.get(baseURL2+oclcNum+'?servicelevel=full&classificationScheme=LibraryOfCongress&wskey='+wskey).content
70+
response2 = requests.get(baseURL2+oclcNum+'?servicelevel=full&classificationScheme=LibraryOfCongress&wskey='+wskey)
71+
print 'search full record'
72+
response2 = response2.content
5373
try:
5474
record2 = BeautifulSoup(response2, "lxml").find('record')
5575
try:

0 commit comments

Comments
 (0)