wikipediagame: add "more" command showing a random image or paragraph - annna -… | |
git clone git://bitreich.org/annna/ git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws6… | |
Log | |
Files | |
Refs | |
Tags | |
README | |
--- | |
commit 87f7c24dce07d352911c3671b5f3a91d60b78d4e | |
parent 035181bd0f74004cb59a29f7b24fd57584a998aa | |
Author: Anders Damsgaard <[email protected]> | |
Date: Sat, 15 Apr 2023 10:02:55 +0200 | |
wikipediagame: add "more" command showing a random image or paragraph | |
Signed-off-by: Annna Robert-Houdin <[email protected]> | |
Diffstat: | |
M wikipediagame | 29 ++++++++++++++++++++--------- | |
1 file changed, 20 insertions(+), 9 deletions(-) | |
--- | |
diff --git a/wikipediagame b/wikipediagame | |
@@ -2,7 +2,7 @@ | |
# coding=utf-8 | |
# | |
# Idea from: https://github.com/izabera/izabot/blob/master/cus_lib.py#L89 | |
-# | |
+# | |
import os | |
import sys | |
@@ -17,6 +17,12 @@ def usage(app): | |
print("usage: %s [-h] cmd" % (app), file=sys.stderr) | |
sys.exit(1) | |
+def concealtitle(s, title): | |
+ splittitle = title.replace(",", " ").replace("-", " ") | |
+ for titlepart in splittitle.split(): | |
+ s = s.replace(titlepart, "*" * len(titlepart)) | |
+ return s | |
+ | |
def main(args): | |
try: | |
opts, largs = getopt.getopt(args[1:], "h") | |
@@ -83,13 +89,8 @@ def main(args): | |
printsummary = 1 | |
if printsummary == 1: | |
- summary = w.summary(title).\ | |
- replace("\n", " ") | |
- splittitle = title.replace(",", " ").\ | |
- replace("-", " ") | |
- for titlepart in splittitle.split(): | |
- summary = summary.replace(titlepart, "*" * len(titlepart)) | |
- print(summary) | |
+ summary = w.summary(title).replace("\n", " ") | |
+ print(concealtitle(summary, title)) | |
if os.path.exists(hintpath): | |
hintfd = open(hintpath, "r") | |
@@ -110,7 +111,17 @@ def main(args): | |
hintfd = open(hintpath, "w+") | |
json.dump(hintsize, hintfd) | |
hintfd.close() | |
- | |
+ | |
+ if cmd == "more": | |
+ wpage = w.page(title) | |
+ images = [item for item in wpage.images if item.find('/commons/')] | |
+ if len(images) > 0 and random.random() < 0.5: | |
+ print(random.choice(images)) | |
+ else: | |
+ paragraphs = wpage.content.split('\n') | |
+ paragraph = random.choice([item for item in paragraphs if len(item… | |
+ print(concealtitle(paragraph, title)) | |
+ | |
if cmd == "guess": | |
if len(largs) < 2: | |
usage(args[0]) |