2020-10-02 13:26:36 +02:00
|
|
|
{
|
|
|
|
"cells": [
|
|
|
|
{
|
|
|
|
"cell_type": "markdown",
|
|
|
|
"metadata": {},
|
|
|
|
"source": [
|
2020-10-02 17:19:58 +02:00
|
|
|
"# 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)"
|
2020-10-02 13:26:36 +02:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
2020-11-05 18:19:38 +01:00
|
|
|
"cell_type": "markdown",
|
2020-10-02 13:26:36 +02:00
|
|
|
"metadata": {},
|
|
|
|
"source": [
|
2020-11-05 18:19:38 +01:00
|
|
|
"## Import the Mastodon library"
|
2020-10-02 13:26:36 +02:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
2020-11-05 18:19:38 +01:00
|
|
|
"cell_type": "markdown",
|
2020-10-02 13:26:36 +02:00
|
|
|
"metadata": {},
|
|
|
|
"source": [
|
2020-11-05 18:19:38 +01:00
|
|
|
"We're using a Python library that is written for Mastodon.\n",
|
|
|
|
"\n",
|
|
|
|
"https://github.com/halcy/Mastodon.py"
|
2020-10-02 13:26:36 +02:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": null,
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
2020-11-05 18:19:38 +01:00
|
|
|
"# import sys\n",
|
|
|
|
"# !{sys.executable} -m pip install Mastodon.py"
|
2020-10-02 13:26:36 +02:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2020-11-05 18:19:38 +01:00
|
|
|
"execution_count": 9,
|
2020-10-02 13:26:36 +02:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
2020-11-05 18:19:38 +01:00
|
|
|
"from mastodon import Mastodon "
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "markdown",
|
|
|
|
"metadata": {},
|
|
|
|
"source": [
|
|
|
|
"## Configuring the infrastructure"
|
2020-10-02 13:26:36 +02:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2020-11-05 18:19:38 +01:00
|
|
|
"execution_count": 15,
|
2020-10-02 13:26:36 +02:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
2020-11-05 18:19:38 +01:00
|
|
|
"instance = 'https://botsin.space'"
|
2020-10-02 13:26:36 +02:00
|
|
|
]
|
|
|
|
},
|
2020-10-02 17:19:58 +02:00
|
|
|
{
|
|
|
|
"cell_type": "markdown",
|
|
|
|
"metadata": {},
|
|
|
|
"source": [
|
2020-11-05 18:19:38 +01:00
|
|
|
"## Starting the bot...."
|
2020-10-02 17:19:58 +02:00
|
|
|
]
|
|
|
|
},
|
2020-10-02 13:26:36 +02:00
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": null,
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"mastodon = Mastodon(\n",
|
|
|
|
" access_token = 'mastodon-bot-usercred.secret',\n",
|
|
|
|
" api_base_url = instance\n",
|
2020-11-05 17:39:14 +01:00
|
|
|
")"
|
|
|
|
]
|
|
|
|
},
|
2020-11-05 18:19:38 +01:00
|
|
|
{
|
|
|
|
"cell_type": "markdown",
|
|
|
|
"metadata": {},
|
|
|
|
"source": [
|
|
|
|
"## Tooting time! \n",
|
|
|
|
"\n",
|
|
|
|
"These are different examples you can use."
|
|
|
|
]
|
|
|
|
},
|
2020-11-05 17:39:14 +01:00
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": null,
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
2020-11-05 18:19:38 +01:00
|
|
|
"# Write a toot.\n",
|
2020-11-05 17:39:14 +01:00
|
|
|
"\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",
|
2020-10-02 13:26:36 +02:00
|
|
|
"\n",
|
2020-10-02 17:19:58 +02:00
|
|
|
"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",
|
2020-11-05 17:39:14 +01:00
|
|
|
" sleep(300) # 300 seconds = 5 minutes"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2020-11-05 18:19:38 +01:00
|
|
|
"execution_count": 24,
|
2020-11-05 17:39:14 +01:00
|
|
|
"metadata": {},
|
2020-11-05 18:19:38 +01:00
|
|
|
"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['}"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"execution_count": 24,
|
|
|
|
"metadata": {},
|
|
|
|
"output_type": "execute_result"
|
|
|
|
}
|
|
|
|
],
|
2020-11-05 17:39:14 +01:00
|
|
|
"source": [
|
2020-11-05 18:19:38 +01:00
|
|
|
"# Tooting a media file (audio, video, image)\n",
|
|
|
|
"# https://mastodonpy.readthedocs.io/en/stable/#media-dicts\n",
|
|
|
|
"\n",
|
|
|
|
"import urllib.request\n",
|
|
|
|
"\n",
|
|
|
|
"url = 'https://vvvvvvaria.org/bots-as-digital-infrapunctures/images/slide.png'\n",
|
|
|
|
"caption = 'Testing'\n",
|
2020-11-05 17:39:14 +01:00
|
|
|
"\n",
|
2020-11-05 18:19:38 +01:00
|
|
|
"urllib.request.urlretrieve(url, 'media/tmp.jpg')\n",
|
|
|
|
"mastodon.media_post('media/tmp.jpg', 'image/jpeg')"
|
2020-11-05 17:39:14 +01:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": null,
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"# Generate sentences ? \n",
|
|
|
|
"\n",
|
2020-11-05 18:19:38 +01:00
|
|
|
"import random\n",
|
|
|
|
"\n",
|
2020-11-05 17:39:14 +01:00
|
|
|
"names = ['', '', '']\n",
|
|
|
|
"objects = ['', '', '']\n",
|
2020-11-05 18:19:38 +01:00
|
|
|
"actions = ['', '', '']\n",
|
|
|
|
"\n",
|
|
|
|
"n = random.choice(names)\n",
|
|
|
|
"o = random.choice(objects)\n",
|
|
|
|
"a = random.choice(actions)\n",
|
|
|
|
"\n",
|
|
|
|
"toot = ''\n",
|
|
|
|
"mastodon.toot(toot)\n"
|
2020-10-02 13:26:36 +02:00
|
|
|
]
|
2020-11-05 17:39:14 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": 14,
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"name": "stdout",
|
|
|
|
"output_type": "stream",
|
|
|
|
"text": [
|
|
|
|
"2020-11-05 17:31:54.233814\n"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"source": [
|
|
|
|
"# Using the current time\n",
|
|
|
|
"\n",
|
|
|
|
"from datetime import datetime\n",
|
|
|
|
"\n",
|
|
|
|
"print(datetime.now())\n"
|
|
|
|
]
|
|
|
|
},
|
2020-11-05 18:19:38 +01:00
|
|
|
{
|
|
|
|
"cell_type": "markdown",
|
|
|
|
"metadata": {},
|
|
|
|
"source": [
|
|
|
|
"## Make your own bot"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": null,
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"# instance = 'URL'\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",
|
|
|
|
"# )"
|
|
|
|
]
|
|
|
|
},
|
2020-11-05 17:39:14 +01:00
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": null,
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": []
|
2020-10-02 13:26:36 +02:00
|
|
|
}
|
|
|
|
],
|
|
|
|
"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
|
|
|
|
}
|