Add wikipedia guessing game to annna. - annna - Annna the nice friendly bot. | |
git clone git://bitreich.org/annna/ git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws6… | |
Log | |
Files | |
Refs | |
Tags | |
README | |
--- | |
commit 56bd88325d9acf1fce9aa8cbe0e03245c48e8ec1 | |
parent 0a9075f602fca9a5f41cc68f2b627b338c8a0376 | |
Author: Annna Robert-Houdin <[email protected]> | |
Date: Mon, 10 Apr 2023 12:40:31 +0200 | |
Add wikipedia guessing game to annna. | |
Diffstat: | |
M annna-channel-service | 3 +++ | |
A wikipediagame | 111 ++++++++++++++++++++++++++++++ | |
2 files changed, 114 insertions(+), 0 deletions(-) | |
--- | |
diff --git a/annna-channel-service b/annna-channel-service | |
@@ -28,6 +28,9 @@ do | |
\#bitreich-idle) | |
annna-message-idle "${server}" "${channel}" "${user}" "${text}" | |
;; | |
+ \#bitreich-wikigame) | |
+ annna-message-wikigame "${server}" "${channel}" "${user}" "${t… | |
+ ;; | |
*) | |
annna-message-common "${server}" "${channel}" "${user}" "${tex… | |
;; | |
diff --git a/wikipediagame b/wikipediagame | |
@@ -0,0 +1,111 @@ | |
+#!/usr/bin/env python | |
+# coding=utf-8 | |
+# | |
+# Idea from: https://github.com/izabera/izabot/blob/master/cus_lib.py#L89 | |
+# | |
+ | |
+import os | |
+import sys | |
+import getopt | |
+import wikipedia as w | |
+import json | |
+ | |
+def usage(app): | |
+ app = os.path.basename(app) | |
+ print("usage: %s [-h] cmd" % (app), file=sys.stderr) | |
+ sys.exit(1) | |
+ | |
+def main(args): | |
+ try: | |
+ opts, largs = getopt.getopt(args[1:], "h") | |
+ except getopt.GetoptError as err: | |
+ print(str(err)) | |
+ usage(args[0]) | |
+ | |
+ basepath = "/home/annna/bin/modules/wikipediagame" | |
+ printsummary = 0 | |
+ newtitle = 0 | |
+ | |
+ for o, a in opts: | |
+ if o == "-h": | |
+ usage(args[0]) | |
+ else: | |
+ assert False, "unhandled option" | |
+ | |
+ if len(largs) < 1: | |
+ usage(args[0]) | |
+ | |
+ titlepath = "%s/lasttitle" % (basepath) | |
+ hintpath = "%s/hintsize" % (basepath) | |
+ | |
+ cmd = largs[0] | |
+ if cmd == "init": | |
+ title = str(w.random()) | |
+ if os.path.exists(hintpath): | |
+ os.remove(hintpath) | |
+ if os.path.exists(titlepath): | |
+ os.remove(titlepath) | |
+ newtitle = 1 | |
+ printsummary = 1 | |
+ else: | |
+ if os.path.exists(titlepath): | |
+ titlefd = open(titlepath, "r") | |
+ title = str(json.load(titlefd)) | |
+ titlefd.close() | |
+ else: | |
+ title = str(w.random()) | |
+ newtitle = 1 | |
+ printsummary = 1 | |
+ | |
+ if newtitle == 1: | |
+ titlefd = open(titlepath, "w+") | |
+ json.dump(title, titlefd) | |
+ titlefd.close() | |
+ | |
+ if cmd == "summary": | |
+ printsummary = 1 | |
+ | |
+ if printsummary == 1: | |
+ summary = w.summary(title).replace("\n", " ") | |
+ for titlepart in title.split(" "): | |
+ summary = summary.replace(titlepart, "*" * len(titlepart)) | |
+ print(summary) | |
+ | |
+ if os.path.exists(hintpath): | |
+ hintfd = open(hintpath, "r") | |
+ try: | |
+ hintsize = int(json.load(hintfd)) | |
+ except json.decoder.JSONDecodeError: | |
+ hintsize = 0 | |
+ hintfd.close() | |
+ else: | |
+ hintsize = 0 | |
+ | |
+ if cmd == "hint": | |
+ hintsize += 3 | |
+ hint = title[:hintsize] \ | |
+ + "".join(["*" if c != ' ' else ' ' for c in title[hintsize:]]) | |
+ print("Hint: %s" % (hint)) | |
+ | |
+ hintfd = open(hintpath, "w+") | |
+ json.dump(hintsize, hintfd) | |
+ hintfd.close() | |
+ | |
+ if cmd == "try": | |
+ if len(largs) < 2: | |
+ usage(args[0]) | |
+ trytext = largs[1] | |
+ if title.strip().lower() == trytext.strip().lower(): | |
+ print("Congrats! You have found the right title! :: %s" % (title)) | |
+ if os.path.exists(hintpath): | |
+ os.remove(hintpath) | |
+ if os.path.exists(titlepath): | |
+ os.remove(titlepath) | |
+ else: | |
+ print("Sorry, wrong guess.") | |
+ | |
+ return 0 | |
+ | |
+if __name__ == "__main__": | |
+ sys.exit(main(sys.argv)) | |
+ |