wikipediagame - annna - Annna the nice friendly bot.
HTML git clone git://bitreich.org/annna/ git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/annna/
DIR Log
DIR Files
DIR Refs
DIR Tags
DIR README
---
wikipediagame (4569B)
---
1 #!/usr/bin/env python
2 # coding=utf-8
3 #
4 # Idea from: https://github.com/izabera/izabot/blob/master/cus_lib.py#L89
5 #
6
7 import os
8 import sys
9 import getopt
10 import wikipedia as w
11 import json
12 import random
13 from difflib import SequenceMatcher
14
15 def usage(app):
16 app = os.path.basename(app)
17 print("usage: %s [-h] cmd" % (app), file=sys.stderr)
18 sys.exit(1)
19
20 def concealtitle(s, title):
21 splittitle = title.replace(",", " ").replace("-", " ")
22 for titlepart in splittitle.split():
23 s = s.replace(titlepart, "*" * len(titlepart))
24 return s
25
26 def geturi(wpage):
27 wuri = wpage.url
28 return wuri.replace("https://en.wikipedia.org/wiki", "gopher://gopherpedia.com/0")
29
30 def endgame(hintpath, titlepath):
31 if os.path.exists(hintpath):
32 os.remove(hintpath)
33 if os.path.exists(titlepath):
34 os.remove(titlepath)
35
36 def main(args):
37 try:
38 opts, largs = getopt.getopt(args[1:], "h")
39 except getopt.GetoptError as err:
40 print(str(err))
41 usage(args[0])
42
43 basepath = "/home/annna/bin/modules/wikipediagame"
44 printsummary = 0
45 newtitle = 0
46 title = None
47
48 for o, a in opts:
49 if o == "-h":
50 usage(args[0])
51 else:
52 assert False, "unhandled option"
53
54 if len(largs) < 1:
55 usage(args[0])
56
57 titlepath = "%s/lasttitle" % (basepath)
58 hintpath = "%s/hintsize" % (basepath)
59
60 cmd = largs[0]
61 if cmd == "init":
62 if len(largs) > 1:
63 searchresults = w.search(largs[1])
64 while len(searchresults) > 0:
65 title = random.choice(searchresults)
66 try:
67 summary = w.summary(title)
68 break
69 except w.exceptions.DisambiguationError:
70 searchresults.remove(title)
71 continue
72
73 if title == None:
74 summary = None
75 while summary == None:
76 title = str(w.random())
77 try:
78 summary = w.summary(title)
79 except (w.exceptions.DisambiguationError, w.exceptions.PageError):
80 continue
81
82 if os.path.exists(hintpath):
83 os.remove(hintpath)
84 if os.path.exists(titlepath):
85 os.remove(titlepath)
86 newtitle = 1
87 printsummary = 1
88 else:
89 if os.path.exists(titlepath):
90 titlefd = open(titlepath, "r")
91 title = str(json.load(titlefd))
92 titlefd.close()
93 else:
94 title = ""
95
96 if len(title) == 0:
97 print("There is no game started. Please run init.")
98 return 0
99
100 if newtitle == 1:
101 titlefd = open(titlepath, "w+")
102 json.dump(title, titlefd)
103 titlefd.close()
104
105 if cmd == "summary":
106 printsummary = 1
107
108 if printsummary == 1:
109 summary = w.summary(title).replace("\n", " ")
110 print(concealtitle(summary, title))
111
112 if os.path.exists(hintpath):
113 hintfd = open(hintpath, "r")
114 try:
115 hintsize = int(json.load(hintfd))
116 except json.decoder.JSONDecodeError:
117 hintsize = 0
118 hintfd.close()
119 else:
120 hintsize = 0
121
122 if cmd == "hint":
123 hintsize += 3
124 hint = title[:hintsize] \
125 + "".join(["*" if c != ' ' else ' ' for c in title[hintsize:]])
126 print("Hint: %s" % (hint))
127
128 hintfd = open(hintpath, "w+")
129 json.dump(hintsize, hintfd)
130 hintfd.close()
131
132 if cmd == "more":
133 wpage = w.page(title)
134 images = [item for item in wpage.images if item.find('/commons/')]
135 if len(images) > 0 and random.random() < 0.5:
136 print(random.choice(images))
137 else:
138 paragraphs = wpage.content.split('\n')
139 paragraph = random.choice([item for item in paragraphs if len(item) > 5 and item[0] != '='])
140 print(concealtitle(paragraph, title))
141
142 if cmd == "guess":
143 if len(largs) < 2:
144 usage(args[0])
145 trytext = largs[1]
146 if title.strip().lower() == trytext.strip().lower():
147 print("Congrats! You have found the right title! :: %s" % (geturi(w.page(title))))
148 endgame(hintpath, titlepath)
149 else:
150 print("Sorry, wrong guess. (%.0f%% correct)" % \
151 (SequenceMatcher(None, title.strip().lower(), trytext.strip().lower()).ratio() * 100))
152
153 if cmd == "giveup":
154 print("The correct title was: %s" % (geturi(w.page(title))))
155 endgame(hintpath, titlepath)
156
157 return 0
158
159 if __name__ == "__main__":
160 sys.exit(main(sys.argv))
161