opensemantic.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """Open Semantic Search
  3. """
  4. from json import loads
  5. from urllib.parse import quote
  6. from dateutil import parser
  7. # about
  8. about = {
  9. "website": 'https://www.opensemanticsearch.org/',
  10. "wikidata_id": None,
  11. "official_api_documentation": 'https://www.opensemanticsearch.org/dev',
  12. "use_official_api": True,
  13. "require_api_key": False,
  14. "results": 'JSON',
  15. }
  16. base_url = 'http://localhost:8983/solr/opensemanticsearch/'
  17. search_string = 'query?q={query}'
  18. def request(query, params):
  19. search_path = search_string.format(
  20. query=quote(query),
  21. )
  22. params['url'] = base_url + search_path
  23. return params
  24. def response(resp):
  25. results = []
  26. data = loads(resp.text)
  27. docs = data.get('response', {}).get('docs', [])
  28. for current in docs:
  29. item = {}
  30. item['url'] = current['id']
  31. item['title'] = current['title_txt_txt_en']
  32. if current.get('content_txt'):
  33. item['content'] = current['content_txt'][0]
  34. item['publishedDate'] = parser.parse(current['file_modified_dt'])
  35. results.append(item)
  36. return results