mastotron

A different interface to mastodon, and other experiments in social media

GPL-3.0 License

Stars
128
Committers
1

{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# mastotron\n", "\n", "Experiments in algorithmifying mastodon" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "<div class="post reblog" style="border:1px solid blue; padding: 0 1em;">\n", "\n", "<a href="https://ausglam.space/@ingridbmason">[email protected] (382 👥) reposted at 12/24/2022 at 10:23:22:\n", "\n", "\n", "\n", "<div class="post origpost" style="border:1px solid orange;padding:0 1em;">\n", "\n", "<a href="https://mastodon.social/@acb">[email protected] (379 👥) <a href="https://mastodon.social/@acb/109567809376185861">wrote on 12/24/2022 at 08:54:51:\n", "\n", "\n", "RT @luismbat@birbsiteWho would have thought that adding a Sierpinski Triangle Fractal as musical notes would actually sound good!😅\n", "\n", "<a href="https://cdn.masto.host/zirkus/cache/media_attachments/files/109/568/001/032/675/283/small/0d3117c3854cbcce.png"><img src="https://cdn.masto.host/zirkus/cache/media_attachments/files/109/568/001/032/675/283/small/0d3117c3854cbcce.png" />\n", "\n", "\n", "3 🗣\n", "  |   \n", "25 🔁\n", "  |  \n", "0 💙\n", "  |  \n", "Post ID: 109567809382202427\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "Post(id=109568157841720925)" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# load the class\n", "from mastotron import Mastotron, Post\n", "\n", "# instantiate; will guide through auth\n", "tron = Mastotron()\n", "\n", "# get latest post\n", "post = tron.latest_post()\n", "post" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "<div class="post origpost" style="border:1px solid orange;padding:0 1em;">\n", "\n", "<a href="https://mstdn.science/@RebeccaRHelm">[email protected] (1,157 👥) <a href="https://mstdn.science/@RebeccaRHelm/109566221656683705">wrote on 12/24/2022 at 02:11:04:\n", "\n", "\n", "EXTREMELY RARE footage of the elusive box jellyfish Chirodectes. Larger than a soccer ball, this jelly is a true ocean mystery, and this video is one of the only in existence, filmed off the coast of Papua New Guinea in 2021.📽️Scuba Ventures Kavieng bit.ly/3FE29tL\n", "\n", "<a href="https://cdn.masto.host/zirkus/cache/media_attachments/files/109/566/221/923/419/468/small/f0a9cc7c3910417e.png"><img src="https://cdn.masto.host/zirkus/cache/media_attachments/files/109/566/221/923/419/468/small/f0a9cc7c3910417e.png" />\n", "\n", "\n", "11 🗣\n", "  |   \n", "77 🔁\n", "  |  \n", "4 💙\n", "  |  \n", "Post ID: 109566222383974657\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "Post(id=109566222383974657)" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Get a post by id on your server\n", "post = Post(id=109566222383974657)\n", "post" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "<div class="post reblog" style="border:1px solid blue; padding: 0 1em;">\n", "\n", "<a href="https://zirk.us/@accommodatingly">accommodatingly (287 👥) reposted at 12/23/2022 at 23:51:23:\n", "\n", "\n", "\n", "<div class="post origpost" style="border:1px solid orange;padding:0 1em;">\n", "\n", "<a href="https://strangeobject.space/@esther">[email protected] (1,367 👥) <a href="https://strangeobject.space/@esther/109563512148655648">wrote on 12/23/2022 at 14:42:00:\n", "\n", "\n", "Thinking of all the queer folks who have to spend the holidays with abusive, unaccepting, unsupportive, or “well meaning” but careless relatives. It’s an especially dark time for many who can’t escape those situations because they’re minors, financially dependent, or under emotional pressure to attend.If you’re out there, struggling, and need a friendly voice, feel free to drop a DM. I’m happy to be your trans goth aunt for a bit.<a href="https://strangeobject.space/tags/queer" class="mention hashtag" rel="nofollow noopener noreferrer" target="_blank">#queer <a href="https://strangeobject.space/tags/trans" class="mention hashtag" rel="nofollow noopener noreferrer" target="_blank">#trans <a href="https://strangeobject.space/tags/holidays" class="mention hashtag" rel="nofollow noopener noreferrer" target="_blank">#holidays <a href="https://strangeobject.space/tags/support" class="mention hashtag" rel="nofollow noopener noreferrer" target="_blank">#support\n", "\n", "\n", "\n", "\n", "12 🗣\n", "  |   \n", "60 🔁\n", "  |  \n", "4 💙\n", "  |  \n", "Post ID: 109563512620764058\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "Post(id=109565672404848243)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# These will show reposts/boosts:\n", "post = Post(id=109565672404848243)\n", "post" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(Post(id=109565672404848243), Post(id=109563512620764058))" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# the post and boosted post\n", "post, post.is_boost_of" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "<div class="post origpost" style="border:1px solid orange;padding:0 1em;">\n", "\n", "<a href="https://zirk.us/@heuser">heuser (235 👥) <a href="https://zirk.us/@heuser/109564529870067074">wrote on 12/23/2022 at 19:00:50:\n", "\n", "\n", "Unexpected challenge: there are very few likes on tweets in my mastodon timeline. (step that up pls everyone thx.) So it's hard to sort the tweets using that metric.\n", "\n", "\n", "\n", "\n", "1 🗣\n", "  |   \n", "0 🔁\n", "  |  \n", "2 💙\n", "  |  \n", "Post ID: 109564529870067074\n", "\n", "\n", "... in reply to: \n", "<div class="post origpost" style="border:1px solid orange;padding:0 1em;">\n", "\n", "<a href="https://zirk.us/@heuser">heuser (235 👥) <a href="https://zirk.us/@heuser/109564459673004810">wrote on 12/23/2022 at 18:42:58:\n", "\n", "\n", "Experimenting with making an "algorithm" to sort a user's feed using python and mastodon.py. This tweet was sent from a jupyter notebook.\n", "\n", "\n", "\n", "\n", "1 🗣\n", "  |   \n", "1 🔁\n", "  |  \n", "3 💙\n", "  |  \n", "Post ID: 109564459673004810\n", "\n", "\n", "\n", "\n", " \n", "\n" ], "text/plain": [ "Post(id=109564529870067074)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# And replies\n", "post = Post(id=109564529870067074)\n", "post" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(Post(id=109564529870067074), Post(id=109564459673004810))" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# the replied-to post\n", "post, post.is_reply_to" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# these can be chained\n", "Post(id=109564658606466762).is_reply_to.is_reply_to.is_reply_to.id == post.id" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Scoring posts" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'Simple': 1.7320508075688774,\n", " 'ExtendedSimple': 1.8171205928321397,\n", " 'SimpleWeighted': 0.11274690420042434,\n", " 'ExtendedSimpleWeighted': 0.11828447555082267,\n", " 'All': 0.4526308886948211}" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Get scores for a post \n", "# (adapted from https://github.com/hodgesmr/mastodon_digest)\n", "post.scores()" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "<div class="post reblog" style="border:1px solid blue; padding: 0 1em;">\n", "\n", "<a href="https://zirk.us/@ecourtem">ecourtem (346 👥) reposted at 12/23/2022 at 14:53:21:\n", "\n", "\n", "\n", "<div class="post origpost" style="border:1px solid orange;padding:0 1em;">\n", "\n", "<a href="https://mstdn.social/@lolennui">[email protected] (5,061 👥) <a href="https://mstdn.social/@lolennui/109560660969613944">wrote on 12/23/2022 at 02:36:55:\n", "\n", "\n", "Every gen X person read a terrifying Stephen King book when they were 9\n", "\n", "\n", "\n", "\n", "55 🗣\n", "  |   \n", "33 🔁\n", "  |  \n", "1 💙\n", "  |  \n", "Post ID: 109560661039545409\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "Post(id=109563556725970365)" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Get top posts by score\n", "top_posts = sorted(\n", " tron.latest_posts(max_posts=100),\n", " key=lambda post: -post.score()\n", ")\n", "\n", "# top post by engagement\n", "top_posts[0]" ] } ], "metadata": { "kernelspec": { "display_name": "venv", "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.10.7" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "3fd48361b044f528a2157a6d50412eb847c334dc1d3abbb213ba1b507b3cd253" } } }, "nbformat": 4, "nbformat_minor": 2 }

Related Projects