sqlite.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """SQLite is a small, fast and reliable SQL database engine. It does not require
  3. any extra dependency.
  4. Configuration
  5. =============
  6. The engine has the following (additional) settings:
  7. - :py:obj:`result_type`
  8. Example
  9. =======
  10. .. _MediathekView: https://mediathekview.de/
  11. To demonstrate the power of database engines, here is a more complex example
  12. which reads from a MediathekView_ (DE) movie database. For this example of the
  13. SQLite engine download the database:
  14. - https://liste.mediathekview.de/filmliste-v2.db.bz2
  15. and unpack into ``searx/data/filmliste-v2.db``. To search the database use e.g
  16. Query to test: ``!mediathekview concert``
  17. .. code:: yaml
  18. - name: mediathekview
  19. engine: sqlite
  20. shortcut: mediathekview
  21. categories: [general, videos]
  22. result_type: MainResult
  23. database: searx/data/filmliste-v2.db
  24. query_str: >-
  25. SELECT title || ' (' || time(duration, 'unixepoch') || ')' AS title,
  26. COALESCE( NULLIF(url_video_hd,''), NULLIF(url_video_sd,''), url_video) AS url,
  27. description AS content
  28. FROM film
  29. WHERE title LIKE :wildcard OR description LIKE :wildcard
  30. ORDER BY duration DESC
  31. Implementations
  32. ===============
  33. """
  34. import typing
  35. import sqlite3
  36. import contextlib
  37. from searx.result_types import EngineResults
  38. from searx.result_types import MainResult, KeyValue
  39. engine_type = "offline"
  40. database = ""
  41. """Filename of the SQLite DB."""
  42. query_str = ""
  43. """SQL query that returns the result items."""
  44. result_type: typing.Literal["MainResult", "KeyValue"] = "KeyValue"
  45. """The result type can be :py:obj:`MainResult` or :py:obj:`KeyValue`."""
  46. limit = 10
  47. paging = True
  48. def init(engine_settings):
  49. if 'query_str' not in engine_settings:
  50. raise ValueError('query_str cannot be empty')
  51. if not engine_settings['query_str'].lower().startswith('select '):
  52. raise ValueError('only SELECT query is supported')
  53. @contextlib.contextmanager
  54. def sqlite_cursor():
  55. """Implements a :py:obj:`Context Manager <contextlib.contextmanager>` for a
  56. :py:obj:`sqlite3.Cursor`.
  57. Open database in read only mode: if the database doesn't exist. The default
  58. mode creates an empty file on the file system. See:
  59. * https://docs.python.org/3/library/sqlite3.html#sqlite3.connect
  60. * https://www.sqlite.org/uri.html
  61. """
  62. uri = 'file:' + database + '?mode=ro'
  63. with contextlib.closing(sqlite3.connect(uri, uri=True)) as connect:
  64. connect.row_factory = sqlite3.Row
  65. with contextlib.closing(connect.cursor()) as cursor:
  66. yield cursor
  67. def search(query, params) -> EngineResults:
  68. res = EngineResults()
  69. query_params = {
  70. 'query': query,
  71. 'wildcard': r'%' + query.replace(' ', r'%') + r'%',
  72. 'limit': limit,
  73. 'offset': (params['pageno'] - 1) * limit,
  74. }
  75. query_to_run = query_str + ' LIMIT :limit OFFSET :offset'
  76. with sqlite_cursor() as cur:
  77. cur.execute(query_to_run, query_params)
  78. col_names = [cn[0] for cn in cur.description]
  79. for row in cur.fetchall():
  80. kvmap = dict(zip(col_names, map(str, row)))
  81. if result_type == "MainResult":
  82. item = MainResult(**kvmap) # type: ignore
  83. else:
  84. item = KeyValue(kvmap=kvmap)
  85. res.add(item)
  86. return res