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ů.

170 řádky
9.4 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2009-2012 by 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 earwigbot.commands import Command
  23. __all__ = ["Notes"]
  24. class Notes(Command):
  25. """A mini IRC-based wiki for storing notes, tips, and reminders."""
  26. name = "notes"
  27. def process(self, data):
  28. pass
  29. class OldCommand(object):
  30. def parse(self):
  31. if command == "notes" or command == "note" or command == "about" or command == "data" or command == "database":
  32. try:
  33. action = line2[4]
  34. except BaseException:
  35. reply("What do you want me to do? Type \"!notes help\" for more information.", chan, nick)
  36. return
  37. import MySQLdb
  38. db = MySQLdb.connect(db="u_earwig_ircbot", host="sql", read_default_file="/home/earwig/.my.cnf")
  39. specify = ' '.join(line2[5:])
  40. if action == "help" or action == "manual":
  41. shortCommandList = "read, write, change, undo, delete, move, author, category, list, report, developer"
  42. if specify == "read":
  43. say("To read an entry, type \"!notes read <entry>\".", chan)
  44. elif specify == "write":
  45. say("To write a new entry, type \"!notes write <entry> <content>\". This will create a new entry only if one does not exist, see the below command...", chan)
  46. elif specify == "change":
  47. say("To change an entry, type \"!notes change <entry> <new content>\". The old entry will be stored in the database, so it can be undone later.", chan)
  48. elif specify == "undo":
  49. say("To undo a change, type \"!notes undo <entry>\".", chan)
  50. elif specify == "delete":
  51. say("To delete an entry, type \"!notes delete <entry>\". For security reasons, only bot admins can do this.", chan)
  52. elif specify == "move":
  53. say("To move an entry, type \"!notes move <old_title> <new_title>\".", chan)
  54. elif specify == "author":
  55. say("To return the author of an entry, type \"!notes author <entry>\".", chan)
  56. elif specify == "category" or specify == "cat":
  57. say("To change an entry's category, type \"!notes category <entry> <category>\".", chan)
  58. elif specify == "list":
  59. say("To list all categories in the database, type \"!notes list\". Type \"!notes list <category>\" to get all entries in a certain category.", chan)
  60. elif specify == "report":
  61. say("To give some statistics about the mini-wiki, including some debugging information, type \"!notes report\" in a PM.", chan)
  62. elif specify == "developer":
  63. say("To do developer work, such as writing to the database directly, type \"!notes developer <command>\". This can only be done by the bot owner.", chan)
  64. else:
  65. db.query("SELECT * FROM version;")
  66. r = db.use_result()
  67. data = r.fetch_row(0)
  68. version = data[0]
  69. reply("The Earwig Mini-Wiki: running v%s." % version, chan, nick)
  70. reply("The full list of commands, for reference, are: %s." % shortCommandList, chan, nick)
  71. reply("For an explaination of a certain command, type \"!notes help <command>\".", chan, nick)
  72. reply("You can also access the database from the Toolserver: http://toolserver.org/~earwig/cgi-bin/irc_database.py", chan, nick)
  73. time.sleep(0.4)
  74. return
  75. elif action == "read":
  76. specify = string.lower(specify)
  77. if " " in specify: specify = string.split(specify, " ")[0]
  78. if not specify or "\"" in specify:
  79. reply("Please include the name of the entry you would like to read after the command, e.g. !notes read earwig", chan, nick)
  80. return
  81. try:
  82. db.query("SELECT entry_content FROM entries WHERE entry_title = \"%s\";" % specify)
  83. r = db.use_result()
  84. data = r.fetch_row(0)
  85. entry = data[0][0]
  86. say("Entry \"\x02%s\x0F\": %s" % (specify, entry), chan)
  87. except Exception:
  88. reply("There is no entry titled \"\x02%s\x0F\"." % specify, chan, nick)
  89. return
  90. elif action == "delete" or action == "remove":
  91. specify = string.lower(specify)
  92. if " " in specify: specify = string.split(specify, " ")[0]
  93. if not specify or "\"" in specify:
  94. reply("Please include the name of the entry you would like to delete after the command, e.g. !notes delete earwig", chan, nick)
  95. return
  96. if authy == "owner" or authy == "admin":
  97. try:
  98. db.query("DELETE from entries where entry_title = \"%s\";" % specify)
  99. r = db.use_result()
  100. db.commit()
  101. reply("The entry on \"\x02%s\x0F\" has been removed." % specify, chan, nick)
  102. except Exception:
  103. phenny.reply("Unable to remove the entry on \"\x02%s\x0F\", because it doesn't exist." % specify, chan, nick)
  104. else:
  105. reply("Only bot admins can remove entries.", chan, nick)
  106. return
  107. elif action == "developer":
  108. if authy == "owner":
  109. db.query(specify)
  110. r = db.use_result()
  111. try:
  112. print r.fetch_row(0)
  113. except Exception:
  114. pass
  115. db.commit()
  116. reply("Done.", chan, nick)
  117. else:
  118. reply("Only the bot owner can modify the raw database.", chan, nick)
  119. return
  120. elif action == "write":
  121. try:
  122. write = line2[5]
  123. content = ' '.join(line2[6:])
  124. except Exception:
  125. reply("Please include some content in your entry.", chan, nick)
  126. return
  127. db.query("SELECT * from entries WHERE entry_title = \"%s\";" % write)
  128. r = db.use_result()
  129. data = r.fetch_row(0)
  130. if data:
  131. reply("An entry on %s already exists; please use \"!notes change %s %s\"." % (write, write, content), chan, nick)
  132. return
  133. content2 = content.replace('"', '\\' + '"')
  134. db.query("INSERT INTO entries (entry_title, entry_author, entry_category, entry_content, entry_content_old) VALUES (\"%s\", \"%s\", \"uncategorized\", \"%s\", NULL);" % (write, nick, content2))
  135. db.commit()
  136. reply("You have written an entry titled \"\x02%s\x0F\", with the following content: \"%s\"" % (write, content), chan, nick)
  137. return
  138. elif action == "change":
  139. reply("NotImplementedError", chan, nick)
  140. elif action == "undo":
  141. reply("NotImplementedError", chan, nick)
  142. elif action == "move":
  143. reply("NotImplementedError", chan, nick)
  144. elif action == "author":
  145. try:
  146. entry = line2[5]
  147. except Exception:
  148. reply("Please include the name of the entry you would like to get information for after the command, e.g. !notes author earwig", chan, nick)
  149. return
  150. db.query("SELECT entry_author from entries WHERE entry_title = \"%s\";" % entry)
  151. r = db.use_result()
  152. data = r.fetch_row(0)
  153. if data:
  154. say("The author of \"\x02%s\x0F\" is \x02%s\x0F." % (entry, data[0][0]), chan)
  155. return
  156. reply("There is no entry titled \"\x02%s\x0F\"." % entry, chan, nick)
  157. return
  158. elif action == "cat" or action == "category":
  159. reply("NotImplementedError", chan, nick)
  160. elif action == "list":
  161. reply("NotImplementedError", chan, nick)
  162. elif action == "report":
  163. reply("NotImplementedError", chan, nick)