standalone_searx.py 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/usr/bin/env python
  2. '''
  3. searx is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. searx is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with searx. If not, see < http://www.gnu.org/licenses/ >.
  13. (C) 2016- by Alexandre Flament, <alex@al-f.net>
  14. '''
  15. # set path
  16. from sys import path
  17. from os.path import realpath, dirname
  18. path.append(realpath(dirname(realpath(__file__)) + '/../'))
  19. # initialization
  20. from json import dumps
  21. from searx import settings
  22. import searx.query
  23. import searx.search
  24. import searx.engines
  25. import searx.preferences
  26. import argparse
  27. searx.engines.initialize_engines(settings['engines'])
  28. # command line parsing
  29. parser = argparse.ArgumentParser(description='Standalone searx.')
  30. parser.add_argument('query', type=str,
  31. help='Text query')
  32. parser.add_argument('--category', type=str, nargs='?',
  33. choices=searx.engines.categories.keys(),
  34. default='general',
  35. help='Search category')
  36. parser.add_argument('--lang', type=str, nargs='?',default='all',
  37. help='Search language')
  38. parser.add_argument('--pageno', type=int, nargs='?', default=1,
  39. help='Page number starting from 1')
  40. parser.add_argument('--safesearch', type=str, nargs='?', choices=['0', '1', '2'], default='0',
  41. help='Safe content filter from none to strict')
  42. parser.add_argument('--timerange', type=str, nargs='?', choices=['day', 'week', 'month', 'year'],
  43. help='Filter by time range')
  44. args = parser.parse_args()
  45. # search results for the query
  46. form = {
  47. "q":args.query,
  48. "categories":args.category.decode('utf-8'),
  49. "pageno":str(args.pageno),
  50. "language":args.lang,
  51. "time_range":args.timerange
  52. }
  53. preferences = searx.preferences.Preferences(['oscar'], searx.engines.categories.keys(), searx.engines.engines, [])
  54. preferences.key_value_settings['safesearch'].parse(args.safesearch)
  55. search_query = searx.search.get_search_query_from_webapp(preferences, form)
  56. search = searx.search.Search(search_query)
  57. result_container = search.search()
  58. # output
  59. from datetime import datetime
  60. def no_parsed_url(results):
  61. for result in results:
  62. del result['parsed_url']
  63. return results
  64. def json_serial(obj):
  65. """JSON serializer for objects not serializable by default json code"""
  66. if isinstance(obj, datetime):
  67. serial = obj.isoformat()
  68. return serial
  69. raise TypeError ("Type not serializable")
  70. result_container_json = {
  71. "search": {
  72. "q": search_query.query,
  73. "pageno": search_query.pageno,
  74. "lang": search_query.lang,
  75. "safesearch": search_query.safesearch,
  76. "timerange": search_query.time_range,
  77. "engines": search_query.engines
  78. },
  79. "results": no_parsed_url(result_container.get_ordered_results()),
  80. "infoboxes": result_container.infoboxes,
  81. "suggestions": list(result_container.suggestions),
  82. "answers": list(result_container.answers),
  83. "paging": result_container.paging,
  84. "results_number": result_container.results_number()
  85. }
  86. print(dumps(result_container_json, sort_keys=True, indent=4, ensure_ascii=False, encoding="utf-8", default=json_serial))