A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

154 řádky
5.9 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2009-2012 Ben Kurtovic <ben.kurtovic@verizon.net>
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the "Software"), to deal
  7. # in the Software without restriction, including without limitation the rights
  8. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. # SOFTWARE.
  22. from os import path
  23. import sqlite3 as sqlite
  24. from threading import Lock
  25. from earwigbot.commands import Command
  26. class Notes(Command):
  27. """A mini IRC-based wiki for storing notes, tips, and reminders."""
  28. name = "notes"
  29. commands = ["notes", "note", "about"]
  30. version = 2
  31. def setup(self):
  32. self._dbfile = path.join(self.config.root_dir, "notes.db")
  33. self._db_access_lock = Lock()
  34. def process(self, data):
  35. commands = {
  36. "list": self.do_list,
  37. "read": self.do_read,
  38. "edit": self.do_edit,
  39. "info": self.do_info,
  40. "history": self.do_history,
  41. "rename": self.do_rename,
  42. "delete": self.do_delete,
  43. }
  44. if not data.args:
  45. msg = "\x0302The Earwig Mini-Wiki\x0F: running v{0}. Subcommands are: {1}. You can get help on any with '!{2} help subcommand'."
  46. cmnds = ", ".join((commands))
  47. self.reply(data, msg.format(self.version, cmnds, data.command))
  48. return
  49. command = data.args[0].lower()
  50. if command in commands:
  51. commands[command](data)
  52. else:
  53. self.reply("Unknown subcommand: \x0303{0}\x0F.".format(command))
  54. def create_db(self, conn):
  55. """Initialize the notes database with its necessary tables."""
  56. script = """
  57. CREATE TABLE entries (entry_id, entry_slug, entry_title, entry_revision);
  58. CREATE TABLE users (user_id, user_host);
  59. CREATE TABLE revisions (rev_id, rev_entry, rev_user, rev_timestamp, rev_content);
  60. """
  61. conn.executescript(script)
  62. def do_list(self, data):
  63. """Show a list of entries in the notes database."""
  64. query = "SELECT entry_title FROM entries"
  65. with sqlite.connect(self._dbfile) as conn, self._db_access_lock:
  66. try:
  67. entries = conn.execute(query).fetchall()
  68. except sqlite.OperationalError:
  69. entires = []
  70. if entries:
  71. self.reply(data, "Entries: {0}".format(", ".join(entries)))
  72. else:
  73. self.reply(data, "No entries in the database.")
  74. def do_read(self, data):
  75. """Read an entry from the notes database."""
  76. query = """SELECT entry_title, rev_content FROM entries
  77. INNER JOIN revisions ON entry_revision = rev_id
  78. WHERE entry_slug = ?"""
  79. try:
  80. slug = data.args[1].lower().replace("_", "").replace("-", "")
  81. except IndexError:
  82. self.reply(data, "Please name an entry to read from.")
  83. return
  84. with sqlite.connect(self._dbfile) as conn, self._db_access_lock:
  85. try:
  86. title, content = conn.execute(query, (slug,)).fetchone()
  87. except sqlite.OperationalError:
  88. title, content = slug, None
  89. if content:
  90. self.reply(data, "\x0302{0}\x0F: {1}".format(title, content))
  91. else:
  92. self.reply(data, "Entry \x0302{0}\x0F not found.".format(title))
  93. def do_edit(self, data):
  94. """Edit an entry in the notes database."""
  95. pass
  96. def do_info(self, data):
  97. """Get info on an entry in the notes database."""
  98. pass
  99. def do_history(self, data):
  100. """Get the history of an entry in the notes database."""
  101. query = """SELECT entry_title, rev_timestamp, user_host FROM entries
  102. INNER JOIN revisions ON entry_revision = rev_id
  103. INNER JOIN users ON rev_user = user_id
  104. WHERE entry_slug = ?"""
  105. try:
  106. slug = data.args[1].lower().replace("_", "").replace("-", "")
  107. except IndexError:
  108. self.reply(data, "Please name an entry to get the history of.")
  109. return
  110. with sqlite.connect(self._dbfile) as conn, self._db_access_lock:
  111. try:
  112. data = conn.execute(query, (slug,)).fetchall()
  113. except sqlite.OperationalError:
  114. data = []
  115. if data:
  116. title = data[0][0]
  117. times = [datum[1] for datum in data]
  118. earliest = min(times).strftime("%b %d, %Y %H:%M:%S")
  119. msg = "\x0302{0}\x0F: {1} edits since {2}"
  120. msg = msg.format(title, len(data), earliest)
  121. if len(times) > 1:
  122. latest = max(times).strftime("%b %d, %Y %H:%M:%S")
  123. msg += "; last edit on {0}".format(lastest)
  124. names = [datum[2] for datum in data]
  125. msg += "; authors: {0}.".format(", ".join(list(set(names))))
  126. self.reply(data, msg)
  127. else:
  128. self.reply(data, "Entry \x0302{0}\x0F not found.".format(title))
  129. def do_rename(self, data):
  130. """Rename an entry in the notes database."""
  131. pass
  132. def do_delete(self, data):
  133. """Delete an entry from the notes database."""
  134. pass