Skip to content

Commit e51cdba

Browse files
author
Anusha Ranganathan
committed
Function to write all of the data for digitalbooks into csv
1 parent d4e474d commit e51cdba

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

docs/digitalbooksMetadataWrite.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
from solr import SolrConnection
2+
import json
3+
import codecs
4+
5+
solrhost = "http://localhost:8080/solr"
6+
s = SolrConnection(solrhost)
7+
8+
fieldnames = ['silo', 'id', 'uuid', 'aggregatedResource', 'created', 'creator', 'currentVersion', 'date', 'dateAccepted', 'dateCopyrighted', 'dateSubmitted', 'description', 'embargoStatus', 'embargoedUntilDate', 'mediator', 'isPartOf', 'isVersionOf', 'license', 'modified', 'publisher', 'rights', 'subject', 'timestamp', 'title', 'type']
9+
10+
solr_params = {}
11+
solr_params['q'] = "silo:digitalbooks"
12+
solr_params['wt'] = 'json'
13+
solr_params['fl'] = ','.join(fieldnames)
14+
solr_params['rows'] = 500000
15+
solr_params['start'] = 0
16+
17+
solr_response = s.raw_query(**solr_params)
18+
19+
numFound = 0
20+
docs = None
21+
fname = "digitalbooks.csv"
22+
delimiter = '$'
23+
24+
if solr_response:
25+
ans = json.loads(solr_response)
26+
numFound = ans['response'].get('numFound',None)
27+
try:
28+
numFound = int(numFound)
29+
except:
30+
numFound = 0
31+
docs = ans['response'].get('docs',None)
32+
if numfound > 0 and docs:
33+
out_f = codecs.open(fname, 'a', 'utf-8')
34+
for row in docs:
35+
row_val = []
36+
for name in fieldnames:
37+
if name in row and row[name] and isinstance(row[name], basestring):
38+
row_val.append(row[name])
39+
elif name in row and row[name] and isinstance(row[name], list):
40+
row_val.append(";".join(row[name]))
41+
else:
42+
row_val.append("")
43+
if row_val:
44+
out_f.write("%s\n" %delimiter.join(row_val)
45+
out_f.close()
46+
else:
47+
print 'The search resulted in no documents'
48+
else:
49+
print 'The search resulted in no matches'
50+

0 commit comments

Comments
 (0)