Skip to content

Commit

Permalink
Merge pull request #71 from Coleridge-Initiative/el3220/69_Dimensions…
Browse files Browse the repository at this point in the history
…_API_QuerySyntaxErrors

Dimensions API: Escaping special characters
  • Loading branch information
ernestogimeno authored Aug 20, 2020
2 parents d291ec4 + 229f1bb commit 0ee6f2a
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion richcontext/scholapi/scholapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,31 @@ def _login (self):
self.api_obj = dimcli.Dsl(verbose=False)


def _clean_search_phrase(self, phrase):
# Special Chars: ^ " : ~ \ [ ] { } ( ) ! | & +
cleaned_phrase = phrase.translate(
str.maketrans(
{
"^": r"\^",
'"': r'\"',
":": r"\:",
"~": r"\~",
"\\": r"\\",
"[": r"\[",
"]": r"\]",
"{": r"\{",
"}": r"\}",
"(": r"\(",
")": r"\)",
"!": r"\!",
"|": r"\|",
"&": r"\&",
"+": r"\+"
}
)
)
return cleaned_phrase

def _run_query (self, query):
"""
run one Dimensions API query, and first login if needed
Expand All @@ -477,7 +502,7 @@ def title_search (self, title):
message = None

t0 = time.time()
enc_title = title.replace('"', '').replace(":", " ").strip()
enc_title = self._clean_search_phrase(title)
query = 'search publications in title_only for "\\"{}\\"" return publications[all]'.format(enc_title)

self._login()
Expand Down

0 comments on commit 0ee6f2a

Please sign in to comment.