updating the bot notebook

This commit is contained in:
manetta 2021-01-22 17:00:14 +01:00
parent c91d8c3a22
commit ecb57c98d9
2 changed files with 166 additions and 276 deletions

View File

@ -1,247 +0,0 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Mastodon bot Example"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This Mastodon bot example is part of the module *Bots as Infrapuncture*. :-)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(explain a bit how Jupyter notebooks work)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"!{sys.executable} -m pip install Mastodon.py"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"from mastodon import Mastodon # https://github.com/halcy/Mastodon.py"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"instance = 'https://botsin.space'\n",
"# username = 'USERNAME'\n",
"# password = 'PASSWORD'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Register the bot as an application, save details as a (.secret) plain text file.\n",
"\n",
"# This only needs to be done once!\n",
"# (we already did it)\n",
"\n",
"# Mastodon.create_app(\n",
"# 'bots-as-infrapunctures',\n",
"# api_base_url = instance,\n",
"# to_file = 'mastodon-bot.secret'\n",
"# )"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Write your login details to a (.secret) plain text file.\n",
"\n",
"# This only needs to be done once!\n",
"# (we already did it)\n",
"\n",
"# mastodon = Mastodon(\n",
"# client_id = 'mastodon-bot.secret',\n",
"# api_base_url = instance\n",
"# )\n",
"\n",
"# mastodon.log_in(\n",
"# username,\n",
"# password,\n",
"# to_file = 'mastodon-bot-usercred.secret'\n",
"# )"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we will run the bot!!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Start up the bot....\n",
"\n",
"mastodon = Mastodon(\n",
" access_token = 'mastodon-bot-usercred.secret',\n",
" api_base_url = instance\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Start tooting!\n",
"\n",
"mastodon.toot('testing tooting')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Another one, automized over a longer period of time\n",
"\n",
"from time import sleep\n",
"\n",
"toots = [\n",
" 'Sentence 1',\n",
" 'Sentence 2',\n",
" 'Sentence 3',\n",
" 'Sentence 4',\n",
" 'Sentence 5'\n",
"]\n",
"\n",
"for toot in toots:\n",
" mastodon.toot(toot)\n",
" sleep(300) # 300 seconds = 5 minutes"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Something with a dictionary\n",
"\n",
"vocabulary = {\n",
" \n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Generate sentences ? \n",
"\n",
"names = ['', '', '']\n",
"objects = ['', '', '']\n",
"actions = ['', '', '']"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<method 'time' of 'datetime.datetime' objects>\n"
]
}
],
"source": [
"# Using the current time\n",
"\n",
"from datetime import datetime\n",
"\n",
"print(datetime.now)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}

View File

@ -7,6 +7,26 @@
"# Mastodon bot Example" "# Mastodon bot Example"
] ]
}, },
{
"cell_type": "markdown",
"metadata": {},
"source": [
"With this Notebook you can make a bot! \n",
"\n",
"This is a space for experimentation, so feel free to use it as a place to test and play around.\n",
"\n",
"The bot will post its messages to the Mastodon instance [botsin.space](https://botsin.space/about), a space dedicated fully to bot users. \n",
"\n",
"Once your bot is running it will write messages here: <https://botsin.space/@infrapunctures>."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"-------------------"
]
},
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
@ -26,8 +46,7 @@
"\n", "\n",
"You can change the code blocks by double clicking the block and you can run them in order to see the effects.\n", "You can change the code blocks by double clicking the block and you can run them in order to see the effects.\n",
"\n", "\n",
"The bot we will be tooting on the Mastodon instance [botsin.space](https://botsin.space/about), a space dedicated fully to bot users. \n", "Also, you can see if a code block is being processed by looking at the `[*]` symbol on the left side. If it changed into a number, for example `[1]`, your code block is executed!"
"You can find the @infrapunctures bot [here](https://botsin.space/@infrapunctures)."
] ]
}, },
{ {
@ -46,9 +65,29 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 1,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Defaulting to user installation because normal site-packages is not writeable\n",
"Requirement already satisfied: Mastodon.py in /home/mb/.local/lib/python3.7/site-packages (1.5.1)\n",
"Requirement already satisfied: python-magic in /usr/local/lib/python3.7/dist-packages (from Mastodon.py) (0.4.15)\n",
"Requirement already satisfied: requests>=2.4.2 in /home/mb/.local/lib/python3.7/site-packages (from Mastodon.py) (2.25.0)\n",
"Requirement already satisfied: decorator>=4.0.0 in /usr/lib/python3/dist-packages (from Mastodon.py) (4.3.0)\n",
"Requirement already satisfied: pytz in /home/mb/.local/lib/python3.7/site-packages (from Mastodon.py) (2020.1)\n",
"Requirement already satisfied: python-dateutil in /home/mb/.local/lib/python3.7/site-packages (from Mastodon.py) (2.8.1)\n",
"Requirement already satisfied: six in /usr/lib/python3/dist-packages (from Mastodon.py) (1.12.0)\n",
"Requirement already satisfied: blurhash>=1.1.4 in /home/mb/.local/lib/python3.7/site-packages (from Mastodon.py) (1.1.4)\n",
"Requirement already satisfied: chardet<4,>=3.0.2 in /usr/lib/python3/dist-packages (from requests>=2.4.2->Mastodon.py) (3.0.4)\n",
"Requirement already satisfied: idna<3,>=2.5 in /usr/lib/python3/dist-packages (from requests>=2.4.2->Mastodon.py) (2.6)\n",
"Requirement already satisfied: certifi>=2017.4.17 in /usr/lib/python3/dist-packages (from requests>=2.4.2->Mastodon.py) (2018.8.24)\n",
"Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/lib/python3/dist-packages (from requests>=2.4.2->Mastodon.py) (1.24.1)\n"
]
}
],
"source": [ "source": [
"import sys\n", "import sys\n",
"!{sys.executable} -m pip install Mastodon.py" "!{sys.executable} -m pip install Mastodon.py"
@ -56,7 +95,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 2,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -79,7 +118,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 4,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -102,7 +141,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 5,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -127,16 +166,43 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"This is the simplest command you need to run to make a toot:" "### Oneliner bot"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This is the simplest command you need to run to make a toot:\n",
"\n",
"(If it works: it will print a lot of metadata of the toot you just sent out!)<br>\n",
"(Also: you should be able to see your toot here: https://botsin.space/@infrapunctures) "
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 17,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The bot just tooted the following: \"testing tooting\", you can see it here: https://botsin.space/@infrapunctures\n"
]
}
],
"source": [ "source": [
"mastodon.toot('testing tooting')" "toot = 'testing tooting'\n",
"mastodon.toot(toot)\n",
"print(f'The bot just tooted the following: \"{ toot }\", you can see it here: https://botsin.space/@infrapunctures')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Every 5 minutes bot"
] ]
}, },
{ {
@ -148,9 +214,21 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 15,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The bot just tooted the following: \"Sentence 1\", you can see it here: https://botsin.space/@infrapunctures\n",
"The bot just tooted the following: \"Sentence 2\", you can see it here: https://botsin.space/@infrapunctures\n",
"The bot just tooted the following: \"Sentence 3\", you can see it here: https://botsin.space/@infrapunctures\n",
"The bot just tooted the following: \"Sentence 4\", you can see it here: https://botsin.space/@infrapunctures\n",
"The bot just tooted the following: \"Sentence 5\", you can see it here: https://botsin.space/@infrapunctures\n"
]
}
],
"source": [ "source": [
"from time import sleep\n", "from time import sleep\n",
"\n", "\n",
@ -164,6 +242,7 @@
"\n", "\n",
"for toot in toots:\n", "for toot in toots:\n",
" mastodon.toot(toot)\n", " mastodon.toot(toot)\n",
" print(f'The bot just tooted the following: \"{ toot }\", you can see it here: https://botsin.space/@infrapunctures')\n",
" sleep(300) # 300 seconds = 5 minutes" " sleep(300) # 300 seconds = 5 minutes"
] ]
}, },
@ -171,14 +250,31 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"In this block we will generate a sentence from these three lists and toot it." "### Sentence generator bot"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's make another bot. \n",
"\n",
"In this block we will generate a sentence from three lists of words and toot it."
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 11,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The bot just tooted the following: \"Parks likes oranges\", you can see it here: https://botsin.space/@infrapunctures\n"
]
}
],
"source": [ "source": [
"import random\n", "import random\n",
"\n", "\n",
@ -192,49 +288,76 @@
"\n", "\n",
"separator = ' '\n", "separator = ' '\n",
"\n", "\n",
"ourtoot = n + separator + a + separator + o\n", "toot = n + separator + a + separator + o\n",
"mastodon.toot(ourtoot)" "mastodon.toot(toot)\n",
"\n",
"print(f'The bot just tooted the following: \"{ toot }\", you can see it here: https://botsin.space/@infrapunctures')"
] ]
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"date.today() returns the day of the week as an integer, where Monday is 0 and Sunday is 6. \n", "### Workday / Weekend bot"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"One more bot!\n",
"\n", "\n",
"The code bellow will make your bot toot the first sentence if it is a work day and \"the second sentence if it is the weekend. " "`date.today()` returns the day of the week as an integer, where Monday is `0` and Sunday is `6`. \n",
"\n",
"The code bellow will make your bot toot the first sentence if it is a work day, and the second sentence if it is the weekend. "
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 14,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The bot just tooted the following: \"I would prefer not to.\", you can see it here: https://botsin.space/@infrapunctures\n"
]
}
],
"source": [ "source": [
"from datetime import date\n", "from datetime import date\n",
"\n", "\n",
"day = date.today().weekday()\n", "day = date.today().weekday()\n",
"\n", "\n",
"if (day != 5 & day != 6):\n", "if (day != 5 & day != 6):\n",
" mastodon.toot(\"When we have no memory or little imagination of an alternative to a life centered on work, there are few incentives to reflect on why we work as we do and what we might wish to do instead.\")\n", " toot = \"When we have no memory or little imagination of an alternative to a life centered on work, there are few incentives to reflect on why we work as we do and what we might wish to do instead.\"\n",
" mastodon.toot(toot)\n",
"else:\n", "else:\n",
" mastodon.toot(\"I would prefer not to.\")" " toot = \"I would prefer not to.\"\n",
] " mastodon.toot(toot)\n",
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Make your own bot"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"These have been only a few things you can try out that are meant to illustrate some of the programming logic we mentioned in the module. \n",
" \n", " \n",
"If you would like to make your own bot from scratch, you can remove the '#' from every line below and save it as a new file. The \"#\" turns them into comments instead of executable lines, so they will not run. These only need to be run once which for this module had been done in preparation. The code registers the bot as an application and saves the details as a .secret plain text file." "print(f'The bot just tooted the following: \"{ toot }\", you can see it here: https://botsin.space/@infrapunctures')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## For further exploration: make your own bot!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you would like to make your own bot from scratch, you can remove the `#` from the code blocks below and save it as a new Notebook. \n",
"\n",
"The `#` turns them into comments instead of executable lines, so they will not run. \n",
"\n",
"These only need to be run once which for this module had been done in preparation. \n",
"\n",
"The code registers the bot as an application and saves the details as a `.secret` plain text file."
] ]
}, },
{ {
@ -250,12 +373,12 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 18,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"# Mastodon.create_app(\n", "# Mastodon.create_app(\n",
"# 'bots-as-infrapunctures',\n", "# 'NAMEOFYOURBOT',\n",
"# api_base_url = instance,\n", "# api_base_url = instance,\n",
"# to_file = 'mastodon-bot.secret'\n", "# to_file = 'mastodon-bot.secret'\n",
"# )" "# )"
@ -278,6 +401,20 @@
"# to_file = 'mastodon-bot-usercred.secret'\n", "# to_file = 'mastodon-bot-usercred.secret'\n",
"# )" "# )"
] ]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you put this code block in the beginning of your Notebook, you can register your own bot!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
} }
], ],
"metadata": { "metadata": {