opensemantic.py 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. """
  2. Open Semantic Search
  3. @website https://www.opensemanticsearch.org/
  4. @provide-api yes (https://www.opensemanticsearch.org/dev)
  5. @using-api yes
  6. @results JSON
  7. @stable yes
  8. @parse url, title, content, publishedDate
  9. """
  10. from dateutil import parser
  11. from json import loads
  12. from urllib.parse import quote
  13. base_url = 'http://localhost:8983/solr/opensemanticsearch/'
  14. search_string = 'query?q={query}'
  15. def request(query, params):
  16. search_path = search_string.format(
  17. query=quote(query),
  18. )
  19. params['url'] = base_url + search_path
  20. return params
  21. def response(resp):
  22. results = []
  23. data = loads(resp.text)
  24. docs = data.get('response', {}).get('docs', [])
  25. for current in docs:
  26. item = {}
  27. item['url'] = current['id']
  28. item['title'] = current['title_txt_txt_en']
  29. if current.get('content_txt'):
  30. item['content'] = current['content_txt'][0]
  31. item['publishedDate'] = parser.parse(current['file_modified_dt'])
  32. results.append(item)
  33. return results