|
| 1 | +#! /usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +""" |
| 5 | +Make general actions for Marvin, one function for each action. |
| 6 | +""" |
| 7 | +import time |
| 8 | +import datetime |
| 9 | +import json |
| 10 | +import random |
| 11 | + |
| 12 | +# Load all strings from file |
| 13 | +with open("marvin_strings.json", encoding="utf-8") as f: |
| 14 | + STRINGS = json.load(f) |
| 15 | + |
| 16 | +# Configuration loaded |
| 17 | +CONFIG = None |
| 18 | + |
| 19 | +def setConfig(config): |
| 20 | + """ |
| 21 | + Keep reference to the loaded configuration. |
| 22 | + """ |
| 23 | + global CONFIG |
| 24 | + CONFIG = config |
| 25 | + |
| 26 | + |
| 27 | +def getString(key, key1=None): |
| 28 | + """ |
| 29 | + Get a string from the string database. |
| 30 | + """ |
| 31 | + data = STRINGS[key] |
| 32 | + if isinstance(data, list): |
| 33 | + res = data[random.randint(0, len(data) - 1)] |
| 34 | + elif isinstance(data, dict): |
| 35 | + if key1 is None: |
| 36 | + res = data |
| 37 | + else: |
| 38 | + res = data[key1] |
| 39 | + if isinstance(res, list): |
| 40 | + res = res[random.randint(0, len(res) - 1)] |
| 41 | + elif isinstance(data, str): |
| 42 | + res = data |
| 43 | + |
| 44 | + return res |
| 45 | + |
| 46 | + |
| 47 | +def getAllGeneralActions(): |
| 48 | + """ |
| 49 | + Return all general actions as an array. |
| 50 | + """ |
| 51 | + return [ |
| 52 | + marvinMorning |
| 53 | + ] |
| 54 | + |
| 55 | + |
| 56 | +def marvinMorning(row, asList=None, asStr=None): |
| 57 | + """ |
| 58 | + Marvin says Good morning after someone else says it |
| 59 | + """ |
| 60 | + phrases = [ |
| 61 | + "morgon", |
| 62 | + "godmorgon", |
| 63 | + "god morgon", |
| 64 | + "morrn", |
| 65 | + "morn" |
| 66 | + ] |
| 67 | + |
| 68 | + morning_phrases = [ |
| 69 | + "Godmorgon! :-)", |
| 70 | + "Morgon allesammans", |
| 71 | + "Morgon gott folk", |
| 72 | + "Guten morgen", |
| 73 | + "Morgon" |
| 74 | + ] |
| 75 | + |
| 76 | + for phrase in phrases: |
| 77 | + if phrase in row: |
| 78 | + msg = random.choice(morning_phrases) |
| 79 | + |
| 80 | + try: |
| 81 | + file = open('data/marvinMorning_date.txt', 'r+') |
| 82 | + file_data = file.read() |
| 83 | + |
| 84 | + if not file_data == str(datetime.date.today()): |
| 85 | + f = open('data/marvinMorning_date.txt', 'w') |
| 86 | + f.write(str(datetime.date.today())) |
| 87 | + return msg |
| 88 | + except IOError: |
| 89 | + file = open('data/marvinMorning_date.txt', 'w') |
| 90 | + file.write(str(datetime.date.today())) |
| 91 | + return msg |
0 commit comments