From 6aa8e196130d970741ecaa01a0ad3951ffb959bb Mon Sep 17 00:00:00 2001 From: ccl Date: Thu, 12 Nov 2020 14:18:16 +0100 Subject: [PATCH] removed the image upload snippet and completed the rest --- content/bot-example/mastodon-bot.ipynb | 272 +++++++++++++------------ 1 file changed, 144 insertions(+), 128 deletions(-) diff --git a/content/bot-example/mastodon-bot.ipynb b/content/bot-example/mastodon-bot.ipynb index 6f2579a..d9cda09 100644 --- a/content/bot-example/mastodon-bot.ipynb +++ b/content/bot-example/mastodon-bot.ipynb @@ -22,7 +22,7 @@ "\n", "We're going to be using the [Python](https://www.python.org/) programming language for this example.\n", "\n", - "In order to *run* (execute) a block of code, click the ▶️ symbol from the toolbox on the top right corner.\n", + "In order to execute a block of code, click the *Run* button from the toolbox on the top right corner. Up until the section \"Tooting time!\" every block needs to be run in the order they are layed out in.\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", "\n", @@ -41,48 +41,41 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "We started by importing a Python code library that is written for Mastodon. You can find more information about it on its [code respository](https://github.com/halcy/Mastodon.py). The first two lines were already executed, we we have commented them out for now.\n", - "\n", - "Some of the lines you will see below have a \"#\" in front of them, this turns them into comments instead of executable lines, so they will not run. These only need to be run once, which was done in preparation for this bot." + "We start by importing a Python code library that is written for Mastodon. You can find more information about it on its [code respository](https://github.com/halcy/Mastodon.py)." ] }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Collecting Mastodon.py\n", - " Downloading https://files.pythonhosted.org/packages/7c/80/f12b205fc529fff8e3245fe8e6cafb870f1783476449d3ea2a32b40928c5/Mastodon.py-1.5.1-py2.py3-none-any.whl\n", + "Requirement already satisfied: Mastodon.py in /home/ccl/Documents/bots-as-digital-infrapunctures/bots-venv/lib/python3.7/site-packages (1.5.1)\n", + "Requirement already satisfied: python-magic in /home/ccl/Documents/bots-as-digital-infrapunctures/bots-venv/lib/python3.7/site-packages (from Mastodon.py) (0.4.18)\n", "Requirement already satisfied: requests>=2.4.2 in /home/ccl/Documents/bots-as-digital-infrapunctures/bots-venv/lib/python3.7/site-packages (from Mastodon.py) (2.24.0)\n", - "Requirement already satisfied: pytz in /home/ccl/Documents/bots-as-digital-infrapunctures/bots-venv/lib/python3.7/site-packages (from Mastodon.py) (2020.1)\n", - "Collecting blurhash>=1.1.4 (from Mastodon.py)\n", - " Downloading https://files.pythonhosted.org/packages/80/08/163cb166a2464223a5eb8890a92cc1cf7e8c7c79a2c75e497e3d8f3a4711/blurhash-1.1.4-py2.py3-none-any.whl\n", + "Requirement already satisfied: blurhash>=1.1.4 in /home/ccl/Documents/bots-as-digital-infrapunctures/bots-venv/lib/python3.7/site-packages (from Mastodon.py) (1.1.4)\n", "Requirement already satisfied: decorator>=4.0.0 in /home/ccl/Documents/bots-as-digital-infrapunctures/bots-venv/lib/python3.7/site-packages (from Mastodon.py) (4.4.2)\n", - "Collecting python-magic (from Mastodon.py)\n", - " Downloading https://files.pythonhosted.org/packages/59/77/c76dc35249df428ce2c38a3196e2b2e8f9d2f847a8ca1d4d7a3973c28601/python_magic-0.4.18-py2.py3-none-any.whl\n", "Requirement already satisfied: python-dateutil in /home/ccl/Documents/bots-as-digital-infrapunctures/bots-venv/lib/python3.7/site-packages (from Mastodon.py) (2.8.1)\n", "Requirement already satisfied: six in /home/ccl/Documents/bots-as-digital-infrapunctures/bots-venv/lib/python3.7/site-packages (from Mastodon.py) (1.15.0)\n", + "Requirement already satisfied: pytz in /home/ccl/Documents/bots-as-digital-infrapunctures/bots-venv/lib/python3.7/site-packages (from Mastodon.py) (2020.1)\n", "Requirement already satisfied: chardet<4,>=3.0.2 in /home/ccl/Documents/bots-as-digital-infrapunctures/bots-venv/lib/python3.7/site-packages (from requests>=2.4.2->Mastodon.py) (3.0.4)\n", "Requirement already satisfied: certifi>=2017.4.17 in /home/ccl/Documents/bots-as-digital-infrapunctures/bots-venv/lib/python3.7/site-packages (from requests>=2.4.2->Mastodon.py) (2020.6.20)\n", "Requirement already satisfied: idna<3,>=2.5 in /home/ccl/Documents/bots-as-digital-infrapunctures/bots-venv/lib/python3.7/site-packages (from requests>=2.4.2->Mastodon.py) (2.10)\n", - "Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /home/ccl/Documents/bots-as-digital-infrapunctures/bots-venv/lib/python3.7/site-packages (from requests>=2.4.2->Mastodon.py) (1.25.10)\n", - "Installing collected packages: blurhash, python-magic, Mastodon.py\n", - "Successfully installed Mastodon.py-1.5.1 blurhash-1.1.4 python-magic-0.4.18\n" + "Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /home/ccl/Documents/bots-as-digital-infrapunctures/bots-venv/lib/python3.7/site-packages (from requests>=2.4.2->Mastodon.py) (1.25.10)\n" ] } ], "source": [ - "# import sys\n", - "# !{sys.executable} -m pip install Mastodon.py" + "import sys\n", + "!{sys.executable} -m pip install Mastodon.py" ] }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ @@ -105,7 +98,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ @@ -128,21 +121,9 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 6, "metadata": {}, - "outputs": [ - { - "ename": "NameError", - "evalue": "name 'Mastodon' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m mastodon = Mastodon(\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0maccess_token\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'mastodon-bot-usercred.secret'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mapi_base_url\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0minstance\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m )\n", - "\u001b[0;31mNameError\u001b[0m: name 'Mastodon' is not defined" - ] - } - ], + "outputs": [], "source": [ "mastodon = Mastodon(\n", " access_token = 'mastodon-bot-usercred.secret',\n", @@ -170,9 +151,67 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "{'id': 105197319551754193,\n", + " 'created_at': datetime.datetime(2020, 11, 12, 12, 21, 48, 992000, tzinfo=tzutc()),\n", + " 'in_reply_to_id': None,\n", + " 'in_reply_to_account_id': None,\n", + " 'sensitive': False,\n", + " 'spoiler_text': '',\n", + " 'visibility': 'public',\n", + " 'language': 'en',\n", + " 'uri': 'https://botsin.space/users/infrapunctures/statuses/105197319551754193',\n", + " 'url': 'https://botsin.space/@infrapunctures/105197319551754193',\n", + " 'replies_count': 0,\n", + " 'reblogs_count': 0,\n", + " 'favourites_count': 0,\n", + " 'favourited': False,\n", + " 'reblogged': False,\n", + " 'muted': False,\n", + " 'bookmarked': False,\n", + " 'pinned': False,\n", + " 'content': '

testing tooting

',\n", + " 'reblog': None,\n", + " 'application': {'name': 'bots-as-infrapunctures', 'website': None},\n", + " 'account': {'id': 259694,\n", + " 'username': 'infrapunctures',\n", + " 'acct': 'infrapunctures',\n", + " 'display_name': '',\n", + " 'locked': False,\n", + " 'bot': False,\n", + " 'discoverable': None,\n", + " 'group': False,\n", + " 'created_at': datetime.datetime(2020, 10, 2, 12, 51, 39, 454000, tzinfo=tzutc()),\n", + " 'note': '

',\n", + " 'url': 'https://botsin.space/@infrapunctures',\n", + " 'avatar': 'https://botsin.space/avatars/original/missing.png',\n", + " 'avatar_static': 'https://botsin.space/avatars/original/missing.png',\n", + " 'header': 'https://botsin.space/headers/original/missing.png',\n", + " 'header_static': 'https://botsin.space/headers/original/missing.png',\n", + " 'followers_count': 2,\n", + " 'following_count': 1,\n", + " 'statuses_count': 6,\n", + " 'last_status_at': datetime.datetime(2020, 11, 12, 0, 0),\n", + " 'emojis': [],\n", + " 'fields': []},\n", + " 'media_attachments': [],\n", + " 'mentions': [],\n", + " 'tags': [],\n", + " 'emojis': [],\n", + " 'card': None,\n", + " 'poll': None}" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "mastodon.toot('testing tooting')" ] @@ -221,116 +260,112 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Here we are tooting a media file (audio, video, image) using their urls. You can read more about it [here](https://mastodonpy.readthedocs.io/en/stable/#media-dicts)." + "In this block we will generate a sentence from these three lists and toot it." ] }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "{'id': 105158839281621670,\n", - " 'type': 'image',\n", - " 'url': 'https://files.botsin.space/media_attachments/files/105/158/839/281/621/670/original/b6092861a5b9fea8.png',\n", - " 'preview_url': 'https://files.botsin.space/media_attachments/files/105/158/839/281/621/670/small/b6092861a5b9fea8.png',\n", - " 'remote_url': None,\n", - " 'preview_remote_url': None,\n", - " 'text_url': 'https://botsin.space/media/DMDQwSmifxEp29TPEVw',\n", - " 'meta': {'original': {'width': 1707,\n", - " 'height': 960,\n", - " 'size': '1707x960',\n", - " 'aspect': 1.778125},\n", - " 'small': {'width': 533,\n", - " 'height': 300,\n", - " 'size': '533x300',\n", - " 'aspect': 1.7766666666666666}},\n", - " 'description': None,\n", - " 'blurhash': 'UIFPBE9F00?bIUM{%MofRjt7oft7t7M{Rjj['}" + "{'id': 105197369852935060,\n", + " 'created_at': datetime.datetime(2020, 11, 12, 12, 34, 36, 549000, tzinfo=tzutc()),\n", + " 'in_reply_to_id': None,\n", + " 'in_reply_to_account_id': None,\n", + " 'sensitive': False,\n", + " 'spoiler_text': '',\n", + " 'visibility': 'public',\n", + " 'language': 'en',\n", + " 'uri': 'https://botsin.space/users/infrapunctures/statuses/105197369852935060',\n", + " 'url': 'https://botsin.space/@infrapunctures/105197369852935060',\n", + " 'replies_count': 0,\n", + " 'reblogs_count': 0,\n", + " 'favourites_count': 0,\n", + " 'favourited': False,\n", + " 'reblogged': False,\n", + " 'muted': False,\n", + " 'bookmarked': False,\n", + " 'pinned': False,\n", + " 'content': '

Rosa likes lychees

',\n", + " 'reblog': None,\n", + " 'application': {'name': 'bots-as-infrapunctures', 'website': None},\n", + " 'account': {'id': 259694,\n", + " 'username': 'infrapunctures',\n", + " 'acct': 'infrapunctures',\n", + " 'display_name': '',\n", + " 'locked': False,\n", + " 'bot': False,\n", + " 'discoverable': None,\n", + " 'group': False,\n", + " 'created_at': datetime.datetime(2020, 10, 2, 12, 51, 39, 454000, tzinfo=tzutc()),\n", + " 'note': '

',\n", + " 'url': 'https://botsin.space/@infrapunctures',\n", + " 'avatar': 'https://botsin.space/avatars/original/missing.png',\n", + " 'avatar_static': 'https://botsin.space/avatars/original/missing.png',\n", + " 'header': 'https://botsin.space/headers/original/missing.png',\n", + " 'header_static': 'https://botsin.space/headers/original/missing.png',\n", + " 'followers_count': 2,\n", + " 'following_count': 1,\n", + " 'statuses_count': 8,\n", + " 'last_status_at': datetime.datetime(2020, 11, 12, 0, 0),\n", + " 'emojis': [],\n", + " 'fields': []},\n", + " 'media_attachments': [],\n", + " 'mentions': [],\n", + " 'tags': [],\n", + " 'emojis': [],\n", + " 'card': None,\n", + " 'poll': None}" ] }, - "execution_count": 24, + "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], - "source": [ - "import urllib.request\n", - "\n", - "url = 'https://vvvvvvaria.org/bots-as-digital-infrapunctures/images/slide.png'\n", - "caption = 'Testing'\n", - "\n", - "urllib.request.urlretrieve(url, 'media/tmp.jpg')\n", - "mastodon.media_post('media/tmp.jpg', 'image/jpeg')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "In this block we will generate a sentence with random names and toot it." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "ename": "NameError", - "evalue": "name 'mastodon' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 10\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 11\u001b[0m \u001b[0mtoot\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m''\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 12\u001b[0;31m \u001b[0mmastodon\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtoot\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtoot\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;31mNameError\u001b[0m: name 'mastodon' is not defined" - ] - } - ], "source": [ "import random\n", "\n", "names = ['Rosa', 'Luxembourg', 'Parks']\n", - "objects = ['orange', 'mango', 'lychee']\n", - "actions = ['eats', 'draws', 'picks up']\n", + "objects = ['oranges', 'mangos', 'lychees']\n", + "actions = ['likes', 'dislikes', 'is indifferent to']\n", "\n", "n = random.choice(names)\n", "o = random.choice(objects)\n", "a = random.choice(actions)\n", "\n", - "toot = ''\n", - "mastodon.toot(toot)\n" + "separator = ' '\n", + "\n", + "ourtoot = n + separator + a + separator + o\n", + "mastodon.toot(ourtoot)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "The code bellow will make your bot toot if ..." + "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", - "execution_count": 14, + "execution_count": 18, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "2020-11-05 17:31:54.233814\n" - ] - } - ], + "outputs": [], "source": [ - "# Using the current time\n", + "from datetime import date\n", "\n", - "from datetime import datetime\n", + "day = date.today().weekday()\n", "\n", - "print(datetime.now())\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", + "else:\n", + " mastodon.toot(\"I would prefer not to.\")" ] }, { @@ -344,11 +379,9 @@ "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. You can always make your own bot and complexify the code.\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. \n", + "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", - "(We didn't include this section in the code bot above because it was already executed in the preparation and only needs to be done once.)" + "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." ] }, { @@ -368,11 +401,6 @@ "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", @@ -386,11 +414,6 @@ "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", @@ -402,13 +425,6 @@ "# to_file = 'mastodon-bot-usercred.secret'\n", "# )" ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": {