Skip to content

Add a JSON export option #114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions scholar.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
# ChangeLog
# ---------
#
# 2.12 Added a JSON export option (author: <[email protected]>).
#
# 2.11 The Scholar site seems to have become more picky about the
# number of results requested. The default of 20 in scholar.py
# could cause HTTP 503 responses. scholar.py now doesn't request
Expand Down Expand Up @@ -166,6 +168,7 @@
import re
import sys
import warnings
import json

try:
# Try importing for Python 3
Expand Down Expand Up @@ -347,6 +350,9 @@ def as_csv(self, header=False, sep='|'):
res.append(sep.join([unicode(self.attrs[key][0]) for key in keys]))
return '\n'.join(res)

def as_json(self):
return json.dumps({key:self.attrs[key][0] for key in self.attrs})

def as_citation(self):
"""
Reports the article in a standard citation format. This works only
Expand Down Expand Up @@ -1139,6 +1145,14 @@ def csv(querier, header=False, sep='|'):
print(encode(result))
header = False

def jsonq(querier):
articles = querier.articles
res=[]
for art in articles:
result = art.as_json()
res.append(encode(result))
print("[\n",",\n".join(res),"\n]")

def citation_export(querier):
articles = querier.articles
for art in articles:
Expand Down Expand Up @@ -1203,6 +1217,8 @@ def main():
help='Print article data in CSV form (separator is "|")')
group.add_option('--csv-header', action='store_true',
help='Like --csv, but print header with column names')
group.add_option('--json', action='store_true',
help='Print article data in JSON form')
group.add_option('--citation', metavar='FORMAT', default=None,
help='Print article details in standard citation format. Argument Must be one of "bt" (BibTeX), "en" (EndNote), "rm" (RefMan), or "rw" (RefWorks).')
parser.add_option_group(group)
Expand Down Expand Up @@ -1294,6 +1310,8 @@ def main():

if options.csv:
csv(querier)
elif options.json:
jsonq(querier)
elif options.csv_header:
csv(querier, header=True)
elif options.citation is not None:
Expand Down