From d228fbdc82b1580f42dda6f7010cc53ecbc049a1 Mon Sep 17 00:00:00 2001 From: Daniel Persson Date: Wed, 16 Oct 2024 06:28:06 +0200 Subject: [PATCH 1/7] Always return https links --- marvin_actions.py | 4 +-- marvin_strings.json | 62 +++++++++++++++++++++--------------------- test_marvin_actions.py | 6 ++-- 3 files changed, 36 insertions(+), 36 deletions(-) diff --git a/marvin_actions.py b/marvin_actions.py index 3c877cf..9c38646 100644 --- a/marvin_actions.py +++ b/marvin_actions.py @@ -123,7 +123,7 @@ def marvinExplainShell(row): if not query: return None cmd = " ".join(query) - url = "http://explainshell.com/explain?cmd=" + url = "https://explainshell.com/explain?cmd=" url += quote_plus(cmd, "/:") msg = getString("explainShell") return msg.format(url) @@ -458,7 +458,7 @@ def marvinNameday(row): if any(r in row for r in ["nameday", "namnsdag"]): try: now = datetime.datetime.now() - raw_url = "http://api.dryg.net/dagar/v2.1/{year}/{month}/{day}" + raw_url = "https://api.dryg.net/dagar/v2.1/{year}/{month}/{day}" url = raw_url.format(year=now.year, month=now.month, day=now.day) r = requests.get(url, timeout=5) nameday_data = r.json() diff --git a/marvin_strings.json b/marvin_strings.json index 592b892..3862215 100644 --- a/marvin_strings.json +++ b/marvin_strings.json @@ -1,11 +1,11 @@ { - "source": "I PHP-kurserna kan du länka till source.php. Annars delar du koden som en gist (https://gist.github.com) eller i CodeShare, (http://codeshare.io).", + "source": "I PHP-kurserna kan du länka till source.php. Annars delar du koden som en gist (https://gist.github.com) eller i CodeShare, (https://codeshare.io).", - "whois": "Jag är en tjänstvillig själ som gillar webbprogrammering. Jag bor på GitHub https://github.com/mosbth/irc2phpbb och du kan diskutera mig i forumet http://dbwebb.se/t/20", + "whois": "Jag är en tjänstvillig själ som gillar webbprogrammering. Jag bor på GitHub https://github.com/mosbth/irc2phpbb och du kan diskutera mig i forumet https://dbwebb.se/t/20", "menu": "[ vem är | le | lunch [var] | citat | budord 1 - 5 | source | väder | solen | dagens video | nöje/paus/strip/comic [slump] | grill | birthday/födelsedag | nameday/namnsdag | stats | google/googla | explain/förklara | uptime | stream | princip | skämt/joke | hjälp ]", - "ircstats": "Statistik för kanalen finns här: http://dbwebb.se/irssistats/db-o-webb.html", + "ircstats": "Statistik för kanalen finns här: https://dbwebb.se/irssistats/db-o-webb.html", "google": [ "Googla {}", @@ -62,10 +62,10 @@ "budord": { "#1": "Ställ din fråga, länka till exempel och källkod. Häng kvar och vänta på svar.", - "#2": "Var inte rädd för att fråga och fråga tills du får svar: http://dbwebb.se/f/6249", - "#3": "Öva dig ställa smarta frågor: http://dbwebb.se/f/7802", - "#4": "When in doubt - gör ett testprogram. http://dbwebb.se/f/13570", - "#5": "Hey Luke - use the source! http://catb.org/jargon/html/U/UTSL.html" + "#2": "Var inte rädd för att fråga och fråga tills du får svar: https://dbwebb.se/f/6249", + "#3": "Öva dig ställa smarta frågor: https://dbwebb.se/f/7802", + "#4": "When in doubt - gör ett testprogram. https://dbwebb.se/f/13570", + "#5": "Hey Luke - use the source! https://catb.org/jargon/html/U/UTSL.html" }, @@ -158,28 +158,28 @@ ], "lunch-malmo": [ - "afrikanskt på Calabass (http://www.calabass.com/)", - "mexikanskt på Zócalo (http://zocalo.se/restaurangerna/malmo/)", - "hamburgare på Surf Shack (http://surfshacksmashburgers.com/)", - "hamburgare på Casual Street Food (http://www.casualstreetfood.se/)", - "hamburgare på Tommi's (http://www.tommisburgerjoint.se/)", - "kebab på Döner Kebab (http://www.triangeln.com/doner_kebab)" + "afrikanskt på Calabass (https://www.calabass.com/)", + "mexikanskt på Zócalo (https://zocalo.se/restaurangerna/malmo/)", + "hamburgare på Surf Shack (https://surfshacksmashburgers.com/)", + "hamburgare på Casual Street Food (https://www.casualstreetfood.se/)", + "hamburgare på Tommi's (https://www.tommisburgerjoint.se/)", + "kebab på Döner Kebab (https://www.triangeln.com/doner_kebab)" ], "lunch-goteborg": [ "halvspecial på Preem", - "grillat på Odinsgrillen (http://www.odinsparkgrill.se/)", - "hamburgare på Burgersson Frigga (http://www.burgersson.se/)", - "hamburgare på Tugg (http://gbg.tuggburgers.se/)", - "japanskt på Mikado (http://www.mikadogoteborg.se/)", - "pizza på I love pizza (http://www.ilovepizza.se/)", - "burrito på TomToms Burritos (http://www.tomtoms.se/)", - "steak på Brasa (http://www.restaurangbrasa.se/)", - "något fint på Post-hotellet (http://www.nordabargrill.se/lunch/)", - "något på John Scott's Place (http://johnscotts.se/palace/)", - "husman på Vällagat (http://www.vallagat.se/)", - "husman på AtWork (http://atwork.nu/restaurang/)", - "sallad på MiXiT (http://mixitgbg.se/mixit-garda/)" + "grillat på Odinsgrillen (https://www.odinsparkgrill.se/)", + "hamburgare på Burgersson Frigga (https://www.burgersson.se/)", + "hamburgare på Tugg (https://gbg.tuggburgers.se/)", + "japanskt på Mikado (https://www.mikadogoteborg.se/)", + "pizza på I love pizza (https://www.ilovepizza.se/)", + "burrito på TomToms Burritos (https://www.tomtoms.se/)", + "steak på Brasa (https://www.restaurangbrasa.se/)", + "något fint på Post-hotellet (https://www.nordabargrill.se/lunch/)", + "något på John Scott's Place (https://johnscotts.se/palace/)", + "husman på Vällagat (https://www.vallagat.se/)", + "husman på AtWork (https://atwork.nu/restaurang/)", + "sallad på MiXiT (https://mixitgbg.se/mixit-garda/)" ], "sun": { @@ -189,9 +189,9 @@ }, "commitstrip": { - "message": "Ta en paus och hämta energi med dagens comic strip från commitStrip: {url}, kolla lite gifs på http://thecodinglove.com/, läs dagens artikel på http://thedailywtf.com/ och slösurfa https://www.reddit.com/r/programmerhumor", - "url": "http://www.commitstrip.com/en/", - "urlPage": "http://www.commitstrip.com/en/page/", + "message": "Ta en paus och hämta energi med dagens comic strip från commitStrip: {url}, kolla lite gifs på https://thecodinglove.com/, läs dagens artikel på https://thedailywtf.com/ och slösurfa https://www.reddit.com/r/programmerhumor", + "url": "https://www.commitstrip.com/en/", + "urlPage": "https://www.commitstrip.com/en/page/", "first": 1, "last": 688 }, @@ -220,7 +220,7 @@ }, "birthday": { - "url": "http://dbwebb.se/forum/", + "url": "https://dbwebb.se/forum/", "somebody": "Idag gratulerar vi {}", "nobody": "Ingen som fyller år idag.", "error": "Något gick snett, jag har inte en susning om någon fyller år eller inte." @@ -231,7 +231,7 @@ "error": "Något gick snett, jag har inte en susning om någon har namnsdag idag eller inte." }, "uptime": { - "info": "Kolla in statsen för uptime-tävlingen på http://uptime.dbwebb.se/" + "info": "Kolla in statsen för uptime-tävlingen på https://uptime.dbwebb.se/" }, "stream": { "info": "Streamen finns på https://dbwebb.se/stream och sparas i spellistan 'Streamade föreläsningar/seminarier' https://www.youtube.com/playlist?list=PLKtP9l5q3ce8Bhxn22oBYWZZ4-3ms1rMl" @@ -256,7 +256,7 @@ "error": "Chuck Norris har inte tid underhålla er idag!" }, "commit": { - "url": "http://whatthecommit.com/index.txt", + "url": "https://whatthecommit.com/index.txt", "error": "Du får komma på ett själv. Jag är trasig för tillfället!" } } diff --git a/test_marvin_actions.py b/test_marvin_actions.py index 8475623..a83c046 100644 --- a/test_marvin_actions.py +++ b/test_marvin_actions.py @@ -127,7 +127,7 @@ def testGoogle(self): def testExplainShell(self): """Test that marvin can explain shell commands""" - url = "http://explainshell.com/explain?cmd=pwd" + url = "https://explainshell.com/explain?cmd=pwd" self.assertActionOutput(marvin_actions.marvinExplainShell, "explain pwd", url) self.assertActionOutput(marvin_actions.marvinExplainShell, "can you explain pwd", url) self.assertActionOutput( @@ -247,7 +247,7 @@ def testNameDayRequest(self): with mock.patch("marvin_actions.datetime") as d: d.datetime.now.return_value = date(2024, 1, 2) self.executeAction(marvin_actions.marvinNameday, "namnsdag") - self.assertEqual(r.get.call_args.args[0], "http://api.dryg.net/dagar/v2.1/2024/1/2") + self.assertEqual(r.get.call_args.args[0], "https://api.dryg.net/dagar/v2.1/2024/1/2") def testNameDayResponse(self): """Test that marvin properly parses nameday responses""" @@ -315,7 +315,7 @@ def testCommitRequest(self): """Test that marvin sends proper requests when generating commit messages""" with mock.patch("marvin_actions.requests") as r: self.executeAction(marvin_actions.marvinCommit, "vad skriver man efter commit -m?") - self.assertEqual(r.get.call_args.args[0], "http://whatthecommit.com/index.txt") + self.assertEqual(r.get.call_args.args[0], "https://whatthecommit.com/index.txt") def testCommitResponse(self): """Test that marvin properly handles responses when generating commit messages""" From f935133eb94cc5e646f6ce7d13c5e429daae8b68 Mon Sep 17 00:00:00 2001 From: Daniel Persson Date: Sun, 24 Nov 2024 18:59:01 +0100 Subject: [PATCH 2/7] Use multiple context handles with a single with statement To reduce indentation. --- test_marvin_actions.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/test_marvin_actions.py b/test_marvin_actions.py index a83c046..0710698 100644 --- a/test_marvin_actions.py +++ b/test_marvin_actions.py @@ -63,12 +63,11 @@ def assertBBQResponse(self, todaysDate, bbqDate, expectedMessageKey): if expectedMessageKey in ["base", "week", "eternity"]: message = message % bbqDate - with mock.patch("marvin_actions.datetime") as d: + with mock.patch("marvin_actions.datetime") as d, mock.patch("marvin_actions.random") as r: d.date.today.return_value = todaysDate - with mock.patch("marvin_actions.random") as r: - r.randint.return_value = 1 - expected = f"{url}. {message}" - self.assertActionOutput(marvin_actions.marvinTimeToBBQ, "dags att grilla", expected) + r.randint.return_value = 1 + expected = f"{url}. {message}" + self.assertActionOutput(marvin_actions.marvinTimeToBBQ, "dags att grilla", expected) def assertNameDayOutput(self, exampleFile, expectedOutput): @@ -243,11 +242,10 @@ def testNameDayReaction(self): def testNameDayRequest(self): """Test that marvin sends a proper request for nameday info""" - with mock.patch("marvin_actions.requests") as r: - with mock.patch("marvin_actions.datetime") as d: - d.datetime.now.return_value = date(2024, 1, 2) - self.executeAction(marvin_actions.marvinNameday, "namnsdag") - self.assertEqual(r.get.call_args.args[0], "https://api.dryg.net/dagar/v2.1/2024/1/2") + with mock.patch("marvin_actions.requests") as r, mock.patch("marvin_actions.datetime") as d: + d.datetime.now.return_value = date(2024, 1, 2) + self.executeAction(marvin_actions.marvinNameday, "namnsdag") + self.assertEqual(r.get.call_args.args[0], "https://api.dryg.net/dagar/v2.1/2024/1/2") def testNameDayResponse(self): """Test that marvin properly parses nameday responses""" From 96e8b193c184e18906f7e0da0b954a9be1676344 Mon Sep 17 00:00:00 2001 From: Daniel Persson Date: Sun, 24 Nov 2024 19:18:06 +0100 Subject: [PATCH 3/7] Use os.path.join instead of concatenating with "/" For compatibility with systems that don't use "/" as path delimiter. --- test_marvin_actions.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/test_marvin_actions.py b/test_marvin_actions.py index 0710698..9187d01 100644 --- a/test_marvin_actions.py +++ b/test_marvin_actions.py @@ -6,6 +6,7 @@ """ import json +import os from datetime import date from unittest import mock, TestCase @@ -72,7 +73,7 @@ def assertBBQResponse(self, todaysDate, bbqDate, expectedMessageKey): def assertNameDayOutput(self, exampleFile, expectedOutput): """Assert that the proper nameday message is returned, given an inputfile""" - with open(f"namedayFiles/{exampleFile}.json", "r", encoding="UTF-8") as f: + with open(os.path.join("namedayFiles", f"{exampleFile}.json"), "r", encoding="UTF-8") as f: response = requests.models.Response() response._content = str.encode(json.dumps(json.load(f))) with mock.patch("marvin_actions.requests") as r: @@ -81,7 +82,7 @@ def assertNameDayOutput(self, exampleFile, expectedOutput): def assertJokeOutput(self, exampleFile, expectedOutput): """Assert that a joke is returned, given an input file""" - with open(f"jokeFiles/{exampleFile}.json", "r", encoding="UTF-8") as f: + with open(os.path.join("jokeFiles", f"{exampleFile}.json"), "r", encoding="UTF-8") as f: response = requests.models.Response() response._content = str.encode(json.dumps(json.load(f))) with mock.patch("marvin_actions.requests") as r: @@ -90,7 +91,7 @@ def assertJokeOutput(self, exampleFile, expectedOutput): def assertSunOutput(self, expectedOutput): """Test that marvin knows when the sun comes up, given an input file""" - with open("sunFiles/sun.json", "r", encoding="UTF-8") as f: + with open(os.path.join("sunFiles", "sun.json"), "r", encoding="UTF-8") as f: response = requests.models.Response() response._content = str.encode(json.dumps(json.load(f))) with mock.patch("marvin_actions.requests") as r: From d2d4c7166276951240133eb67b9a31ad7b7a9b77 Mon Sep 17 00:00:00 2001 From: Daniel Persson Date: Sun, 24 Nov 2024 20:12:38 +0100 Subject: [PATCH 4/7] Remove IRC stats plugin Since it is IRC specific, and the IRC server is dead, at least for now. --- marvin_actions.py | 12 ------------ marvin_strings.json | 4 +--- test_marvin_actions.py | 5 ----- 3 files changed, 1 insertion(+), 20 deletions(-) diff --git a/marvin_actions.py b/marvin_actions.py index 9c38646..fffb82a 100644 --- a/marvin_actions.py +++ b/marvin_actions.py @@ -31,7 +31,6 @@ def getAllActions(): marvinSource, marvinBudord, marvinQuote, - marvinStats, marvinWeather, marvinSun, marvinSayHi, @@ -221,17 +220,6 @@ def marvinHelp(row): return msg -def marvinStats(row): - """ - Provide a link to the stats. - """ - msg = None - if any(r in row for r in ["stats", "statistik", "ircstats"]): - msg = getString("ircstats") - - return msg - - def marvinSayHi(row): """ Say hi with a nice message. diff --git a/marvin_strings.json b/marvin_strings.json index 3862215..e906471 100644 --- a/marvin_strings.json +++ b/marvin_strings.json @@ -3,9 +3,7 @@ "whois": "Jag är en tjänstvillig själ som gillar webbprogrammering. Jag bor på GitHub https://github.com/mosbth/irc2phpbb och du kan diskutera mig i forumet https://dbwebb.se/t/20", - "menu": "[ vem är | le | lunch [var] | citat | budord 1 - 5 | source | väder | solen | dagens video | nöje/paus/strip/comic [slump] | grill | birthday/födelsedag | nameday/namnsdag | stats | google/googla | explain/förklara | uptime | stream | princip | skämt/joke | hjälp ]", - - "ircstats": "Statistik för kanalen finns här: https://dbwebb.se/irssistats/db-o-webb.html", + "menu": "[ vem är | le | lunch [var] | citat | budord 1 - 5 | source | väder | solen | dagens video | nöje/paus/strip/comic [slump] | grill | birthday/födelsedag | nameday/namnsdag | google/googla | explain/förklara | uptime | stream | princip | skämt/joke | hjälp ]", "google": [ "Googla {}", diff --git a/test_marvin_actions.py b/test_marvin_actions.py index 9187d01..8d5727e 100644 --- a/test_marvin_actions.py +++ b/test_marvin_actions.py @@ -180,11 +180,6 @@ def testHelp(self): self.assertStringsOutput(marvin_actions.marvinHelp, "help", "menu") self.assertActionSilent(marvin_actions.marvinHelp, "halp") - def testStats(self): - """Test that marvin can provide a link to the IRC stats page""" - self.assertStringsOutput(marvin_actions.marvinStats, "stats", "ircstats") - self.assertActionSilent(marvin_actions.marvinStats, "statistics") - def testSayHi(self): """Test that marvin responds to greetings""" with mock.patch("marvin_actions.random") as r: From fdd45648c97abb969b13ad1654867a739b7b9794 Mon Sep 17 00:00:00 2001 From: Daniel Persson Date: Sun, 24 Nov 2024 20:06:18 +0100 Subject: [PATCH 5/7] Use name of weekday as keys in strings database in Video of the Day They make for more readable keys instead of strings containing numbers, especially since they did not start with index 0. --- marvin_actions.py | 6 +++--- marvin_strings.json | 28 ++++++++++++++-------------- test_marvin_actions.py | 12 +++++++----- 3 files changed, 24 insertions(+), 22 deletions(-) diff --git a/marvin_actions.py b/marvin_actions.py index fffb82a..e28f307 100644 --- a/marvin_actions.py +++ b/marvin_actions.py @@ -174,9 +174,9 @@ def videoOfToday(): """ Check what day it is and provide a url to a suitable video together with a greeting. """ - dayNum = datetime.date.weekday(datetime.date.today()) + 1 - msg = getString("weekdays", str(dayNum)) - video = getString("video-of-today", str(dayNum)) + day = datetime.date.today().strftime("%A") + msg = getString("weekdays", day) + video = getString("video-of-today", day) if video: msg += " En passande video är " + video diff --git a/marvin_strings.json b/marvin_strings.json index e906471..ac25b66 100644 --- a/marvin_strings.json +++ b/marvin_strings.json @@ -68,23 +68,23 @@ "weekdays": { - "1": "Idag är det måndag.", - "2": "Idag är det tisdag.", - "3": "Idag är det onsdag.", - "4": "Idag är det torsdag.", - "5": "Idag är det fredag.", - "6": "Idag är det lördag.", - "7": "Idag är det söndag." + "Monday": "Idag är det måndag.", + "Tuesday": "Idag är det tisdag.", + "Wednesday": "Idag är det onsdag.", + "Thursday": "Idag är det torsdag.", + "Friday": "Idag är det fredag.", + "Saturday": "Idag är det lördag.", + "Sunday": "Idag är det söndag." }, "video-of-today": { - "1": "https://www.youtube.com/watch?v=HbJ6Jsy0JfY https://www.youtube.com/watch?v=-Kobdb37Cwc eller https://www.youtube.com/watch?v=d-diB65scQU", - "2": "https://www.youtube.com/watch?v=sL4znuvCuRg", - "3": "https://www.youtube.com/watch?v=F3jnymeJof4", - "4": "https://www.youtube.com/watch?v=9Tg_zvDg3Xo", - "5": "https://www.youtube.com/watch?v=kfVsfOSbJY0", - "6": "https://www.youtube.com/watch?v=GVCzdpagXOQ", - "7": "https://www.youtube.com/watch?v=c3BvW56tjB0" + "Monday": "https://www.youtube.com/watch?v=HbJ6Jsy0JfY https://www.youtube.com/watch?v=-Kobdb37Cwc eller https://www.youtube.com/watch?v=d-diB65scQU", + "Tuesday": "https://www.youtube.com/watch?v=sL4znuvCuRg", + "Wednesday": "https://www.youtube.com/watch?v=F3jnymeJof4", + "Thursday": "https://www.youtube.com/watch?v=9Tg_zvDg3Xo", + "Friday": "https://www.youtube.com/watch?v=kfVsfOSbJY0", + "Saturday": "https://www.youtube.com/watch?v=GVCzdpagXOQ", + "Sunday": "https://www.youtube.com/watch?v=c3BvW56tjB0" }, "lunch-message": [ diff --git a/test_marvin_actions.py b/test_marvin_actions.py index 8d5727e..59eddbd 100644 --- a/test_marvin_actions.py +++ b/test_marvin_actions.py @@ -8,7 +8,7 @@ import json import os -from datetime import date +from datetime import date, timedelta from unittest import mock, TestCase import requests @@ -168,10 +168,12 @@ def testVideoOfToday(self): """Test that marvin can link to a different video each day of the week""" with mock.patch("marvin_actions.datetime") as dt: for d in range(1, 8): - dt.date.weekday.return_value = d - 1 - day = self.strings.get("weekdays").get(str(d)) - video = self.strings.get("video-of-today").get(str(d)) - response = f"{day} En passande video är {video}" + day = date(2024, 11, 25) + timedelta(days=d) + dt.date.today.return_value = day + weekday = day.strftime("%A") + weekdayPhrase = self.strings.get("weekdays").get(weekday) + videoPhrase = self.strings.get("video-of-today").get(weekday) + response = f"{weekdayPhrase} En passande video är {videoPhrase}" self.assertActionOutput(marvin_actions.marvinVideoOfToday, "dagens video", response) self.assertActionSilent(marvin_actions.marvinVideoOfToday, "videoidag") From 1c893351670fa4d5564387fbecf7a5375e365933 Mon Sep 17 00:00:00 2001 From: Daniel Persson Date: Sun, 24 Nov 2024 20:25:20 +0100 Subject: [PATCH 6/7] Restructure Video of the Day related data in strings database Structure the data instead of having multiple separate dicts with the same keys. --- marvin_actions.py | 10 ++++----- marvin_strings.json | 46 +++++++++++++++++++++++++----------------- test_marvin_actions.py | 4 ++-- 3 files changed, 35 insertions(+), 25 deletions(-) diff --git a/marvin_actions.py b/marvin_actions.py index e28f307..85f88ce 100644 --- a/marvin_actions.py +++ b/marvin_actions.py @@ -174,12 +174,12 @@ def videoOfToday(): """ Check what day it is and provide a url to a suitable video together with a greeting. """ - day = datetime.date.today().strftime("%A") - msg = getString("weekdays", day) - video = getString("video-of-today", day) + weekday = datetime.date.today().strftime("%A") + day = getString("video-of-today", weekday) + msg = day.get("message") - if video: - msg += " En passande video är " + video + if day: + msg += " En passande video är " + day.get("url") else: msg += " Jag har ännu ingen passande video för denna dagen." diff --git a/marvin_strings.json b/marvin_strings.json index ac25b66..9a6389b 100644 --- a/marvin_strings.json +++ b/marvin_strings.json @@ -66,25 +66,35 @@ "#5": "Hey Luke - use the source! https://catb.org/jargon/html/U/UTSL.html" }, - - "weekdays": { - "Monday": "Idag är det måndag.", - "Tuesday": "Idag är det tisdag.", - "Wednesday": "Idag är det onsdag.", - "Thursday": "Idag är det torsdag.", - "Friday": "Idag är det fredag.", - "Saturday": "Idag är det lördag.", - "Sunday": "Idag är det söndag." - }, - "video-of-today": { - "Monday": "https://www.youtube.com/watch?v=HbJ6Jsy0JfY https://www.youtube.com/watch?v=-Kobdb37Cwc eller https://www.youtube.com/watch?v=d-diB65scQU", - "Tuesday": "https://www.youtube.com/watch?v=sL4znuvCuRg", - "Wednesday": "https://www.youtube.com/watch?v=F3jnymeJof4", - "Thursday": "https://www.youtube.com/watch?v=9Tg_zvDg3Xo", - "Friday": "https://www.youtube.com/watch?v=kfVsfOSbJY0", - "Saturday": "https://www.youtube.com/watch?v=GVCzdpagXOQ", - "Sunday": "https://www.youtube.com/watch?v=c3BvW56tjB0" + "Monday": { + "url": "https://www.youtube.com/watch?v=HbJ6Jsy0JfY https://www.youtube.com/watch?v=-Kobdb37Cwc eller https://www.youtube.com/watch?v=d-diB65scQU", + "message": "Idag är det måndag." + }, + "Tuesday": { + "url": "https://www.youtube.com/watch?v=sL4znuvCuRg", + "message": "Idag är det tisdag." + }, + "Wednesday": { + "url": "https://www.youtube.com/watch?v=F3jnymeJof4", + "message": "Idag är det onsdag." + }, + "Thursday": { + "url": "https://www.youtube.com/watch?v=9Tg_zvDg3Xo", + "message": "Idag är det torsdag." + }, + "Friday": { + "url": "https://www.youtube.com/watch?v=kfVsfOSbJY0", + "message": "Idag är det fredag." + }, + "Saturday": { + "url": "https://www.youtube.com/watch?v=GVCzdpagXOQ", + "message": "Idag är det lördag." + }, + "Sunday": { + "url": "https://www.youtube.com/watch?v=c3BvW56tjB0", + "message": "Idag är det söndag." + } }, "lunch-message": [ diff --git a/test_marvin_actions.py b/test_marvin_actions.py index 59eddbd..72b2a4b 100644 --- a/test_marvin_actions.py +++ b/test_marvin_actions.py @@ -171,8 +171,8 @@ def testVideoOfToday(self): day = date(2024, 11, 25) + timedelta(days=d) dt.date.today.return_value = day weekday = day.strftime("%A") - weekdayPhrase = self.strings.get("weekdays").get(weekday) - videoPhrase = self.strings.get("video-of-today").get(weekday) + weekdayPhrase = self.strings.get("video-of-today").get(weekday).get("message") + videoPhrase = self.strings.get("video-of-today").get(weekday).get("url") response = f"{weekdayPhrase} En passande video är {videoPhrase}" self.assertActionOutput(marvin_actions.marvinVideoOfToday, "dagens video", response) self.assertActionSilent(marvin_actions.marvinVideoOfToday, "videoidag") From bc3783cec883a0bec9d9c004678f726195a42619 Mon Sep 17 00:00:00 2001 From: Daniel Persson Date: Sun, 24 Nov 2024 21:42:59 +0100 Subject: [PATCH 7/7] Restructure the lunch data in strings database Introduce some depth into the datastructure instead of having multiple separate dictionaries with prefixes in the keys. --- marvin_actions.py | 20 +++-- marvin_strings.json | 182 ++++++++++++++++++++--------------------- test_marvin_actions.py | 4 +- 3 files changed, 103 insertions(+), 103 deletions(-) diff --git a/marvin_actions.py b/marvin_actions.py index 85f88ce..80b94ce 100644 --- a/marvin_actions.py +++ b/marvin_actions.py @@ -243,21 +243,23 @@ def marvinLunch(row): Help decide where to eat. """ lunchOptions = { - 'stan centrum karlskrona kna': 'lunch-karlskrona', - 'ängelholm angelholm engelholm': 'lunch-angelholm', - 'hässleholm hassleholm': 'lunch-hassleholm', - 'malmö malmo malmoe': 'lunch-malmo', - 'göteborg goteborg gbg': 'lunch-goteborg' + 'stan centrum karlskrona kna': 'karlskrona', + 'ängelholm angelholm engelholm': 'angelholm', + 'hässleholm hassleholm': 'hassleholm', + 'malmö malmo malmoe': 'malmo', + 'göteborg goteborg gbg': 'goteborg' } - if any(r in row for r in ["lunch", "mat", "äta", "luncha"]): - lunchStr = getString('lunch-message') + data = getString("lunch") + if any(r in row for r in ["lunch", "mat", "äta", "luncha"]): + places = data.get("location").get("bth") for keys, value in lunchOptions.items(): if any(r in row for r in keys.split(" ")): - return lunchStr.format(getString(value)) + places = data.get("location").get(value) - return lunchStr.format(getString('lunch-bth')) + lunchStr = getString("lunch", "message") + return lunchStr.format(places[random.randint(0, len(places) - 1)]) return None diff --git a/marvin_strings.json b/marvin_strings.json index 9a6389b..a8a2a07 100644 --- a/marvin_strings.json +++ b/marvin_strings.json @@ -97,98 +97,96 @@ } }, - "lunch-message": [ - "Ska vi ta {}?", - "Jag är lite sugen på {}?", - "Jag tänkte käka på {}, ska du med?", - "{}, där är det mysigt, ska vi ta där?" - ], - - "lunch-karlskrona": [ - "Olles krovbar", - "Lila thai stället", - "donken", - "tex mex stället vid subway", - "Subway", - "Nya peking", - "kebab house", - "Royal thai", - "thai stället vid hemmakväll", - "Gelato", - "Indian garden", - "Sumo sushi", - "Pasterian i stan", - "Biobaren", - "Michelangelo" - ], - - "lunch-bth": [ - "thairestaurangen vid korsningen", - "fiket jämte demolabbet", - "Indiska", - "Pappa curry", - "boden uppe på parkeringen", - "Bergåsa kebab", - "Pasterian", - "Villa Oscar", - "Eat here", - "Bistro J" - ], - - "lunch-angelholm": [ - "Kirris", - "Passagen", - "Ica (mmmm broccoli)", - "Åkanten (Om sommar och sol)", - "Top Falafel", - "Subway", - "Acapulco", - "Kebab på torget", - "Ostbutiken", - "Hummerhuset", - "Klitterhus (fancy stuff)" - ], - - "lunch-hassleholm": [ - "pastavagnen på torget", - "Freds", - "mcDonalds", - "subway", - "kinabuffé på Cats", - "valentino", - "lotterilådan", - "casablance", - "det där stället i gallerian", - "infinity", - "östervärn", - "argentina", - "T4" - ], - - "lunch-malmo": [ - "afrikanskt på Calabass (https://www.calabass.com/)", - "mexikanskt på Zócalo (https://zocalo.se/restaurangerna/malmo/)", - "hamburgare på Surf Shack (https://surfshacksmashburgers.com/)", - "hamburgare på Casual Street Food (https://www.casualstreetfood.se/)", - "hamburgare på Tommi's (https://www.tommisburgerjoint.se/)", - "kebab på Döner Kebab (https://www.triangeln.com/doner_kebab)" - ], - - "lunch-goteborg": [ - "halvspecial på Preem", - "grillat på Odinsgrillen (https://www.odinsparkgrill.se/)", - "hamburgare på Burgersson Frigga (https://www.burgersson.se/)", - "hamburgare på Tugg (https://gbg.tuggburgers.se/)", - "japanskt på Mikado (https://www.mikadogoteborg.se/)", - "pizza på I love pizza (https://www.ilovepizza.se/)", - "burrito på TomToms Burritos (https://www.tomtoms.se/)", - "steak på Brasa (https://www.restaurangbrasa.se/)", - "något fint på Post-hotellet (https://www.nordabargrill.se/lunch/)", - "något på John Scott's Place (https://johnscotts.se/palace/)", - "husman på Vällagat (https://www.vallagat.se/)", - "husman på AtWork (https://atwork.nu/restaurang/)", - "sallad på MiXiT (https://mixitgbg.se/mixit-garda/)" - ], + "lunch": { + "message" : [ + "Ska vi ta {}?", + "Jag är lite sugen på {}?", + "Jag tänkte käka på {}, ska du med?", + "{}, där är det mysigt, ska vi ta där?" + ], + "location": { + "karlskrona": [ + "Olles krovbar", + "Lila thai stället", + "donken", + "tex mex stället vid subway", + "Subway", + "Nya peking", + "kebab house", + "Royal thai", + "thai stället vid hemmakväll", + "Gelato", + "Indian garden", + "Sumo sushi", + "Pasterian i stan", + "Biobaren", + "Michelangelo" + ], + "bth": [ + "thairestaurangen vid korsningen", + "fiket jämte demolabbet", + "Indiska", + "Pappa curry", + "boden uppe på parkeringen", + "Bergåsa kebab", + "Pasterian", + "Villa Oscar", + "Eat here", + "Bistro J" + ], + "angelholm": [ + "Kirris", + "Passagen", + "Ica (mmmm broccoli)", + "Åkanten (Om sommar och sol)", + "Top Falafel", + "Subway", + "Acapulco", + "Kebab på torget", + "Ostbutiken", + "Hummerhuset", + "Klitterhus (fancy stuff)" + ], + "hassleholm": [ + "pastavagnen på torget", + "Freds", + "mcDonalds", + "subway", + "kinabuffé på Cats", + "valentino", + "lotterilådan", + "casablance", + "det där stället i gallerian", + "infinity", + "östervärn", + "argentina", + "T4" + ], + "malmo": [ + "afrikanskt på Calabass (https://www.calabass.com/)", + "mexikanskt på Zócalo (https://zocalo.se/restaurangerna/malmo/)", + "hamburgare på Surf Shack (https://surfshacksmashburgers.com/)", + "hamburgare på Casual Street Food (https://www.casualstreetfood.se/)", + "hamburgare på Tommi's (https://www.tommisburgerjoint.se/)", + "kebab på Döner Kebab (https://www.triangeln.com/doner_kebab)" + ], + "goteborg": [ + "halvspecial på Preem", + "grillat på Odinsgrillen (https://www.odinsparkgrill.se/)", + "hamburgare på Burgersson Frigga (https://www.burgersson.se/)", + "hamburgare på Tugg (https://gbg.tuggburgers.se/)", + "japanskt på Mikado (https://www.mikadogoteborg.se/)", + "pizza på I love pizza (https://www.ilovepizza.se/)", + "burrito på TomToms Burritos (https://www.tomtoms.se/)", + "steak på Brasa (https://www.restaurangbrasa.se/)", + "något fint på Post-hotellet (https://www.nordabargrill.se/lunch/)", + "något på John Scott's Place (https://johnscotts.se/palace/)", + "husman på Vällagat (https://www.vallagat.se/)", + "husman på AtWork (https://atwork.nu/restaurang/)", + "sallad på MiXiT (https://mixitgbg.se/mixit-garda/)" + ] + } + }, "sun": { "url": "https://api.sunrisesunset.io/json?lat=56.182244&lng=15.5882305", diff --git a/test_marvin_actions.py b/test_marvin_actions.py index 72b2a4b..8624dab 100644 --- a/test_marvin_actions.py +++ b/test_marvin_actions.py @@ -197,8 +197,8 @@ def testLunchLocations(self): locations = ["karlskrona", "goteborg", "angelholm", "hassleholm", "malmo"] with mock.patch("marvin_actions.random") as r: for location in locations: - for index, place in enumerate(self.strings.get(f"lunch-{location}")): - r.randint.side_effect = [0, index] + for i, place in enumerate(self.strings.get("lunch").get("location").get(location)): + r.randint.side_effect = [0, i] self.assertActionOutput( marvin_actions.marvinLunch, f"mat {location}", f"Ska vi ta {place}?") r.randint.side_effect = [1, 2]