]> git.cworth.org Git - turbot/commitdiff
Add some utility code
authorCarl Worth <cworth@cworth.org>
Fri, 7 Jan 2022 03:28:42 +0000 (19:28 -0800)
committerCarl Worth <cworth@cworth.org>
Fri, 7 Jan 2022 03:28:42 +0000 (19:28 -0800)
None of this is really "part of" turbot, but is instead some
standalone code I've written from time to time either while testing
functionality or when live-tweaking something during hunt as follows:

sheets-test.py: Ensuring Google sheets API works as expected

blocks-test.py: Debugging some Slack blocks formatting

hunt-channel-topic-tweak.py: During MH 2021 it was useful (for
a reason I don't recall at the moment) to be able to manually
tweak the topic of some channels from a script and I whipped
these up at the moment.

It's not clear if any of this will be useful going forward, but just
in case I'm holding onto it for now. We may just want to delete this
code if we find we don't end up using it.

turbot/util/blocks-test.py [new file with mode: 0755]
turbot/util/hunt-channel-topic-tweak.py [new file with mode: 0755]
turbot/util/sheets-test.py [new file with mode: 0755]

diff --git a/turbot/util/blocks-test.py b/turbot/util/blocks-test.py
new file mode 100755 (executable)
index 0000000..2e09c0e
--- /dev/null
@@ -0,0 +1,49 @@
+#!/usr/bin/env python3
+
+from blocks import (
+    multi_select_block, accessory_block, section_block, text_block
+)
+
+def new_multi_select_block(label, name, placeholder, options, default=None):
+
+    multi_select = {
+        "action_id": name,
+        "type": "multi_static_select",
+        "placeholder": {
+            "type": "plain_text",
+            "text": placeholder
+        },
+        "options": [
+            {
+                "text": {
+                    "type": "plain_text",
+                    "text": option
+                },
+                "value": option
+            } for option in options
+        ]
+    }
+
+    return accessory_block(
+        section_block(text_block("*{}*".format(label))),
+        multi_select
+    )
+
+blocks = multi_select_block("Label", "Name", "Placeholder",
+                            ["Foo", "Bar", "Baz"])
+
+blocks_str = str(blocks)
+
+new_blocks = new_multi_select_block("Label", "Name", "Placeholder",
+                                    ["Foo", "Bar", "Baz"])
+
+new_blocks_str = str(new_blocks)
+
+print("In main, blocks is: {}".format(blocks_str))
+
+print("Also new_blocks is: {}".format(new_blocks_str))
+
+if blocks_str == new_blocks_str:
+    print("Perfect match!")
+else:
+    print("No match.")
diff --git a/turbot/util/hunt-channel-topic-tweak.py b/turbot/util/hunt-channel-topic-tweak.py
new file mode 100755 (executable)
index 0000000..2951894
--- /dev/null
@@ -0,0 +1,32 @@
+#!/usr/bin/env python3
+
+from urllib.parse import parse_qs
+from slack import WebClient
+import base64
+import boto3
+import requests
+import json
+import pickle
+import os
+import sys
+from types import SimpleNamespace
+from google.auth.transport.requests import Request
+from googleapiclient.discovery import build
+
+# Note: Late import here to have the environment variable above available
+
+if 'SLACK_BOT_TOKEN' in os.environ:
+    slack_bot_token = os.environ['SLACK_BOT_TOKEN']
+else:
+    print("Please set SLACK_BOT_TOKEN in environment variable")
+    sys.exit(1)
+slack_client = WebClient(slack_bot_token)
+
+channel_id='C01K8FJJMR7'
+puzzle_url="https://perpendicular.institute/puzzle/%E2%9C%8F/"
+sheet_url='https://docs.google.com/spreadsheets/d/1951ZTGLvSq5PqHPQ1ygqddRjennsedkNtCaOGH__dMo/edit#gid=2097467701'
+title="(See `/puzzle` for title)"
+topic="(<{}|Puzzle>,<{}|Sheet>) {}".format(puzzle_url, sheet_url, title)
+
+slack_client.conversations_setTopic(channel=channel_id,
+                                    topic=topic)
diff --git a/turbot/util/sheets-test.py b/turbot/util/sheets-test.py
new file mode 100755 (executable)
index 0000000..09a268e
--- /dev/null
@@ -0,0 +1,70 @@
+#!/usr/bin/env python3
+
+from types import SimpleNamespace
+from google.auth.transport.requests import Request
+from googleapiclient.discovery import build
+import sys
+import os
+import base64
+import pickle
+
+from sheets import (
+    sheets_create_folder, sheets_create, spreadsheet_update_data,
+    sheets_create_for_puzzle
+)
+
+if 'GSHEETS_PICKLE_BASE64' not in os.environ:
+    print("Error. Need GSHEETS_PICKLE_BASE64 in environment")
+    sys.exit(1)
+
+gsheets_pickle_base64 = os.environ['GSHEETS_PICKLE_BASE64']
+gsheets_pickle = base64.b64decode(gsheets_pickle_base64)
+gsheets_creds = pickle.loads(gsheets_pickle)
+
+if gsheets_creds:
+    if gsheets_creds.valid:
+        print("Loaded valid GSheets credentials from environment")
+    else:
+        gsheets_creds.refresh(Request())
+        gsheets_pickle = pickle.dumps(gsheets_creds)
+        gsheets_pickle_base64_bytes = base64.b64encode(gsheets_pickle)
+        gsheets_pickle_base64 = gsheets_pickle_base64_bytes.decode('us-ascii')
+        print("Refreshed GSheets credentials to be put into environment:")
+        print("GSHEETS_PICKLE_BASE64={}".format(gsheets_pickle_base64))
+
+service = build('sheets',
+                'v4',
+                credentials=gsheets_creds,
+                cache_discovery=False)
+sheets = service.spreadsheets()
+service = build('drive',
+                'v3',
+                credentials=gsheets_creds,
+                cache_discovery=False)
+files = service.files()
+permissions = service.permissions()
+
+turb = SimpleNamespace()
+turb.sheets = sheets
+turb.files = files
+turb.permissions = permissions
+
+folder_id="1ImWL0g50HSwRHC1UjJvYTHhi1_RsPJrI"
+spreadsheet_id="1sO4NWDmUDa8H2_lDNsgXz-5Gy6Qr55trV_-J9dLyIjA"
+
+if not folder_id:
+    folder_id = sheets_create_folder(turb, "sheet-testing")
+    print("Created folder with folder_id=\"{}\"".format(folder_id))
+
+if not spreadsheet_id:
+    puzzle={
+        "name": "Test puzzle",
+        "url": "https://cworth.org",
+        "channel_url": "slack://something-or-other",
+    }
+    spreadsheet = sheets_create_for_puzzle(turb, puzzle, folder_id)
+    spreadsheet_id = spreadsheet['id']
+    print("Created spreadsheet with spreadsheet_id=\"{}\"".format(
+        spreadsheet_id))
+
+spreadsheet_update_data(turb, spreadsheet_id, 'B2:B2', "B2")