|
| 1 | +# Licensed under a 3-clause BSD style license - see LICENSE.rst |
| 2 | +""" |
| 3 | +Module to search the SAO/NASA Astrophysics Data System |
| 4 | +
|
| 5 | +:author: Magnus Persson <[email protected]> |
| 6 | +
|
| 7 | +""" |
| 8 | + |
| 9 | +import warnings |
| 10 | +from ..query import BaseQuery |
| 11 | +from ..utils import commons, async_to_sync |
| 12 | +from . import conf |
| 13 | +from astropy.table import Table, Column |
| 14 | + |
| 15 | +from ..utils.class_or_instance import class_or_instance |
| 16 | +from ..utils import commons, async_to_sync |
| 17 | +from .utils import * |
| 18 | + |
| 19 | +from xml.dom import minidom |
| 20 | + |
| 21 | +__all__ = ['ADS', 'ADSClass'] |
| 22 | + |
| 23 | +@async_to_sync |
| 24 | +class ADSClass(BaseQuery): |
| 25 | + |
| 26 | + SERVER = conf.server |
| 27 | + QUERY_ADVANCED_PATH = conf.advanced_path |
| 28 | + QUERY_SIMPLE_PATH = conf.simple_path |
| 29 | + TIMEOUT = conf.timeout |
| 30 | + |
| 31 | + QUERY_SIMPLE_URL = SERVER + QUERY_SIMPLE_PATH |
| 32 | + QUERY_ADVANCED_URL = SERVER + QUERY_ADVANCED_PATH |
| 33 | + |
| 34 | + def __init__(self, *args): |
| 35 | + """ set some parameters """ |
| 36 | + pass |
| 37 | + |
| 38 | + @class_or_instance |
| 39 | + def query_simple(self, query_string, get_query_payload=False, get_raw_response=False): |
| 40 | + self.query_string = query_string |
| 41 | + request_payload = self._args_to_payload(query_string) |
| 42 | + |
| 43 | + response = commons.send_request(self.QUERY_SIMPLE_URL, request_payload, self.TIMEOUT) |
| 44 | + |
| 45 | + # primarily for debug purposes, but also useful if you want to send |
| 46 | + # someone a URL linking directly to the data |
| 47 | + if get_query_payload: |
| 48 | + return request_payload |
| 49 | + if get_raw_response: |
| 50 | + return response |
| 51 | + # parse the XML response into AstroPy Table |
| 52 | + resulttable = self._parse_response(response.encode(results.encoding).decode('utf-8')) |
| 53 | + |
| 54 | + return resulttable |
| 55 | + |
| 56 | + def _parse_response(self, response): |
| 57 | + xmlrepr = minidom.parseString(response.text) |
| 58 | + # Check if there are any results! |
| 59 | + |
| 60 | + # get the list of hits |
| 61 | + hitlist = xmlrepr.childNodes[0].childNodes |
| 62 | + hitlist = hitlist[1::2] # every second hit is a "line break" |
| 63 | + |
| 64 | + # Grab the various fields |
| 65 | + titles = _get_data_from_xml(hitlist, 'title') |
| 66 | + bibcode = _get_data_from_xml(hitlist, 'bibcode') |
| 67 | + journal = _get_data_from_xml(hitlist, 'journal') |
| 68 | + volume = _get_data_from_xml(hitlist, 'volume') |
| 69 | + pubdate = _get_data_from_xml(hitlist, 'pubdate') |
| 70 | + page = _get_data_from_xml(hitlist, 'page') |
| 71 | + score = _get_data_from_xml(hitlist, 'score') |
| 72 | + citations = _get_data_from_xml(hitlist, 'citations') |
| 73 | + abstract = _get_data_from_xml(hitlist, 'abstract') |
| 74 | + doi = _get_data_from_xml(hitlist, 'DOI') |
| 75 | + eprintid = _get_data_from_xml(hitlist, 'eprintid') |
| 76 | + authors = _get_data_from_xml(hitlist, 'author') |
| 77 | + # put into AstroPy Table |
| 78 | + t = Table() |
| 79 | + t['title'] = titles |
| 80 | + t['bibcode'] = bibcode |
| 81 | + t['journal'] = journal |
| 82 | + t['volume'] = volume |
| 83 | + t['pubdate'] = pubdate |
| 84 | + t['page'] = page |
| 85 | + t['score'] = score |
| 86 | + t['citations'] = citations |
| 87 | + t['abstract'] = abstract |
| 88 | + t['doi'] = doi |
| 89 | + t['eprintid'] = eprintid |
| 90 | + t['authors'] = authors |
| 91 | + |
| 92 | + return t |
| 93 | + |
| 94 | + def _args_to_payload(self, query_string): |
| 95 | + # convert arguments to a valid requests payload |
| 96 | + # i.e. a dictionary |
| 97 | + return {'qsearch' : query_string, 'data_type' : 'XML'} |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | +ADS = ADSClass() |
| 102 | + |
0 commit comments