Skip to content

Commit

Permalink
tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
OllyButters committed Jul 5, 2024
1 parent 3d25d20 commit cdf708f
Showing 1 changed file with 28 additions and 19 deletions.
47 changes: 28 additions & 19 deletions source/get/getScopus.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
#!/usr/bin/env python3

import urllib.request, urllib.error, urllib.parse
import json
import logging

import config.config as config
from config import config
from . import papersCache as pc


Expand All @@ -23,37 +21,44 @@ def getScopus(zotero_ID, PMID, DOI):
logging.info(request_string)
try:
response = urllib.request.urlopen(request_string).read()
except Exception as e:
except urllib.error.HTTPError as e: # Catch HTTPError specifically
if e.code == 429:
print("Too many Scopus requests")
print(str(e))
logging.warn("Too many Scopus requests")
logging.warn(str(e))
logging.warning("Too many Scopus requests")
logging.warning(str(e))
return "QUOTAEXCEEDED"
else:
print("Uncaught error" + str(e))
logging.warn("Uncaught error" + str(e))
return

print("Uncaught HTTP error" + str(e))
logging.warning("Uncaught HTTP error %s", str(e))
return
except Exception as e: # Catch other exceptions
print("Error: " + str(e))
logging.warning("Error: %s", str(e))
return

scopus_object = json.loads(response)

# Check to see if this is just an errorlog
try:
error = scopus_object['search-results']['entry'][0]['error']
if error == 'Result set was empty':
logging.info('Result set empty for ' + str(PMID))
logging.warn(scopus_object)
logging.info('Result set empty for %s', str(PMID))
logging.warning(scopus_object)
del(scopus_object)
except:
pass

if scopus_object:
logging.info('Scopus data got via PMID.')
else:
raise
raise Exception('No scopus data returned.')
else:
raise
raise Exception('No PMID')
except:
print('Unexpected error with PMID.')
logging.warning('Unexpected error with PMID.')

# querying with PMID failed, so try DOI.
try:
if DOI != "":
Expand All @@ -63,17 +68,21 @@ def getScopus(zotero_ID, PMID, DOI):

try:
response = urllib.request.urlopen(request_string).read()
except Exception as e:
except urllib.error.HTTPError as e: # Catch HTTPError specifically
if e.code == 429:
print("Too many Scopus requests")
print(str(e))
logging.warn("Too many Scopus requests")
logging.warn(str(e))
logging.warning("Too many Scopus requests")
logging.warning(str(e))
return "QUOTAEXCEEDED"
else:
print("Uncaught error" + str(e))
logging.warn("Uncaught error" + str(e))
logging.warning("Uncaught error" + str(e))
return
except Exception as e: # Catch other exceptions
print("Error: " + str(e))
logging.warning("Error: " + str(e))
return

scopus_object = json.loads(response)

Expand All @@ -82,7 +91,7 @@ def getScopus(zotero_ID, PMID, DOI):
error = scopus_object['search-results']['entry'][0]['error']
if error == 'Result set was empty':
logging.info('Result set empty for ' + str(DOI))
logging.warn(scopus_object)
logging.warning(scopus_object)
del(scopus_object)
except:
pass
Expand Down

0 comments on commit cdf708f

Please sign in to comment.