standalone_searx.py 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 sys
  23. import codecs
  24. import searx.query
  25. import searx.search
  26. import searx.engines
  27. import searx.preferences
  28. import argparse
  29. searx.engines.initialize_engines(settings['engines'])
  30. # command line parsing
  31. parser = argparse.ArgumentParser(description='Standalone searx.')
  32. parser.add_argument('query', type=str,
  33. help='Text query')
  34. parser.add_argument('--category', type=str, nargs='?',
  35. choices=searx.engines.categories.keys(),
  36. default='general',
  37. help='Search category')
  38. parser.add_argument('--lang', type=str, nargs='?',default='all',
  39. help='Search language')
  40. parser.add_argument('--pageno', type=int, nargs='?', default=1,
  41. help='Page number starting from 1')
  42. parser.add_argument('--safesearch', type=str, nargs='?', choices=['0', '1', '2'], default='0',
  43. help='Safe content filter from none to strict')
  44. parser.add_argument('--timerange', type=str, nargs='?', choices=['day', 'week', 'month', 'year'],
  45. help='Filter by time range')
  46. args = parser.parse_args()
  47. # search results for the query
  48. form = {
  49. "q":args.query,
  50. "categories":args.category.decode('utf-8'),
  51. "pageno":str(args.pageno),
  52. "language":args.lang,
  53. "time_range":args.timerange
  54. }
  55. preferences = searx.preferences.Preferences(['oscar'], searx.engines.categories.keys(), searx.engines.engines, [])
  56. preferences.key_value_settings['safesearch'].parse(args.safesearch)
  57. search_query = searx.search.get_search_query_from_webapp(preferences, form)
  58. search = searx.search.Search(search_query)
  59. result_container = search.search()
  60. # output
  61. from datetime import datetime
  62. def no_parsed_url(results):
  63. for result in results:
  64. del result['parsed_url']
  65. return results
  66. def json_serial(obj):
  67. """JSON serializer for objects not serializable by default json code"""
  68. if isinstance(obj, datetime):
  69. serial = obj.isoformat()
  70. return serial
  71. raise TypeError ("Type not serializable")
  72. result_container_json = {
  73. "search": {
  74. "q": search_query.query,
  75. "pageno": search_query.pageno,
  76. "lang": search_query.lang,
  77. "safesearch": search_query.safesearch,
  78. "timerange": search_query.time_range,
  79. "engines": search_query.engines
  80. },
  81. "results": no_parsed_url(result_container.get_ordered_results()),
  82. "infoboxes": result_container.infoboxes,
  83. "suggestions": list(result_container.suggestions),
  84. "answers": list(result_container.answers),
  85. "paging": result_container.paging,
  86. "results_number": result_container.results_number()
  87. }
  88. sys.stdout = codecs.getwriter("UTF-8")(sys.stdout)
  89. sys.stdout.write(dumps(result_container_json, sort_keys=True, indent=4, ensure_ascii=False, encoding="utf-8", default=json_serial))