opensemantic.py 1.1 KB

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