diff options
Diffstat (limited to 'fortune.py')
-rwxr-xr-x | fortune.py | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/fortune.py b/fortune.py new file mode 100755 index 0000000..64f9dd9 --- /dev/null +++ b/fortune.py @@ -0,0 +1,54 @@ +import random +import sys + + +class FortuneCookie(): + """"Holds a cookie""" + + def __init__(self, msg): + self.msg = msg + + +class FortuneFile(): + """Contains methods for querying a cookie file""" + + def __init__(self, filename): + self.filename = filename + + def fortunes(self): + try: + with open(self.filename) as f: + current = "" + for line in f: + if line != "%\n": + current += line + else: + cookie = FortuneCookie(current) + current = "" + yield cookie + if current != "": + yield FortuneCookie(current) + + except FileNotFoundError: + print("[*] Can't find the file " + self.filename) + except IOError: + print("[*] Error opening the file.") + + def get_random_cookie(self): + for i, v in enumerate(self.fortunes()): + rand = random.randint(0, i) + if rand == 0: + ret = v + + return ret + + +if __name__ == "__main__": + if len(sys.argv) != 2: + print("Usage: {} <fortune filename>".format(sys.argv[0])) + sys.exit(1) + + ff = FortuneFile(sys.argv[1]) + cookie = ff.get_random_cookie() + + sys.stdout.write(cookie.msg) |