From f871121f753c42094363c29c99960c33a04a6515 Mon Sep 17 00:00:00 2001 From: sunwoo1524 Date: Sun, 3 Dec 2023 23:01:55 +0900 Subject: [PATCH] init --- .gitignore | 4 ++++ README.md | 31 +++++++++++++++++++++++++++++++ main.py | 30 ++++++++++++++++++++++++++++++ requirements.txt | 2 ++ setting_example.py | 4 ++++ 5 files changed, 71 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 main.py create mode 100644 requirements.txt create mode 100644 setting_example.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..be5d806 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +venv/ +setting.py +google_api.json +__pycache__/ \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..82f500a --- /dev/null +++ b/README.md @@ -0,0 +1,31 @@ +# Misskey Random Sentence Bot +## How to run +1. clone repo +2. make venv +``` +python3 -m venv venv +source ./venv/bin/activate +``` +3. install modules +``` +pip install -r requirements.txt +``` +4. change setting +``` +# copy example setting file +cp setting_example.py setting.py + +# edit setting file +vim setting.py +``` +- `instance_address`: address of misskey instance that your bot will run +- `access_token`: API access token of your bot account +- `google_api_json`: your google api access json file +- `spread_sheet_url`: URL of spread sheet that include sentences as: + +![](https://r2.worldc.one/media/fa04a9ba-e50e-4b63-81c3-73416f1481b7.webp) + +5. RUN +``` +python main.py +``` \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..c4390d0 --- /dev/null +++ b/main.py @@ -0,0 +1,30 @@ +from misskey import Misskey +from setting import instance_address, access_token, google_api_json, spread_sheet_url +import gspread +import random + +# authentication to misskey +misskey = Misskey(address=instance_address, i=access_token) + +# authentication to google spread sheet +gc = gspread.service_account(google_api_json) + + +def writeRandomSentenceNote(): + # get all sentences + sheet = gc.open_by_url(spread_sheet_url) + work_sheet = sheet.get_worksheet(0) + sentences = work_sheet.col_values(1) + + # choice random sentence + sentence = random.choice(sentences) + + # write note + random_sentence_note = misskey.notes_create(text=sentence) + note_id = random_sentence_note["createdNote"]["id"] + note_text = random_sentence_note["createdNote"]["text"] + print(f"{note_id} | {note_text}") + + +if __name__ == "__main__": + writeRandomSentenceNote() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..c1825e7 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +Misskey.py +gspread \ No newline at end of file diff --git a/setting_example.py b/setting_example.py new file mode 100644 index 0000000..5be10bb --- /dev/null +++ b/setting_example.py @@ -0,0 +1,4 @@ +instance_address = "your_misskey_instance.tld" +access_token = "your_misskey_api_access_token" +google_api_json = "your_google_api_json_file.json" # Absolute path is recommended. +spread_sheet_url = "your_google_spread_sheet_url" \ No newline at end of file