Skip to content

Instantly share code, notes, and snippets.

@cloudgrimm
Forked from svpino/twitter-unfollow.ipynb
Created August 12, 2020 11:21
Show Gist options
  • Save cloudgrimm/96fd09f51a62df5571a66c6dbc3159ef to your computer and use it in GitHub Desktop.
Save cloudgrimm/96fd09f51a62df5571a66c6dbc3159ef to your computer and use it in GitHub Desktop.

Revisions

  1. @svpino svpino revised this gist Aug 11, 2020. 1 changed file with 12 additions and 1 deletion.
    13 changes: 12 additions & 1 deletion twitter-unfollow.ipynb
    Original file line number Diff line number Diff line change
    @@ -6,14 +6,25 @@
    "name": "twitter-unfollow.ipynb",
    "provenance": [],
    "collapsed_sections": [],
    "authorship_tag": "ABX9TyPvmBNi6ybzh03oqQrRZHOQ"
    "authorship_tag": "ABX9TyPvmBNi6ybzh03oqQrRZHOQ",
    "include_colab_link": true
    },
    "kernelspec": {
    "name": "python3",
    "display_name": "Python 3"
    }
    },
    "cells": [
    {
    "cell_type": "markdown",
    "metadata": {
    "id": "view-in-github",
    "colab_type": "text"
    },
    "source": [
    "<a href=\"https://colab.research.google.com/gist/svpino/d7312c82162f093a4cb55e76b9805437/twitter-unfollow.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
    ]
    },
    {
    "cell_type": "code",
    "metadata": {
  2. @svpino svpino created this gist Aug 11, 2020.
    127 changes: 127 additions & 0 deletions twitter-unfollow.ipynb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,127 @@
    {
    "nbformat": 4,
    "nbformat_minor": 0,
    "metadata": {
    "colab": {
    "name": "twitter-unfollow.ipynb",
    "provenance": [],
    "collapsed_sections": [],
    "authorship_tag": "ABX9TyPvmBNi6ybzh03oqQrRZHOQ"
    },
    "kernelspec": {
    "name": "python3",
    "display_name": "Python 3"
    }
    },
    "cells": [
    {
    "cell_type": "code",
    "metadata": {
    "id": "fUpFTRp7eooH",
    "colab_type": "code",
    "colab": {}
    },
    "source": [
    "import os\n",
    "import tweepy\n",
    "import pandas as pd\n",
    "from datetime import datetime, timedelta"
    ],
    "execution_count": 18,
    "outputs": []
    },
    {
    "cell_type": "code",
    "metadata": {
    "id": "EE7qH-Vdes0w",
    "colab_type": "code",
    "colab": {}
    },
    "source": [
    "API_KEY = \"YOUR API KEY GOES HERE\"\n",
    "API_KEY_SECRET = \"YOUR API KEY SECRET GOES HERE\"\n",
    "ACCESS_TOKEN = \"YOUR ACCESS TOKEN GOES HERE\"\n",
    "ACCESS_TOKEN_SECRET = \"YOUR ACCESS TOKEN SECRET GOES HERE\"\n",
    "\n",
    "USER = \"YOUR USER GOES HERE\"\n",
    "\n",
    "# Any user you are following that hasn't posted during the \n",
    "# last DAYS_WITHOUT_ACTIVITY days will be unfolled.\n",
    "DAYS_WITHOUT_ACTIVITY = 60\n",
    "\n",
    "# Any user that posts less than once every 5 days will be\n",
    "# unfollowed. \n",
    "DAILY_TWEET_FREQUENCY = 1. / 5"
    ],
    "execution_count": 40,
    "outputs": []
    },
    {
    "cell_type": "code",
    "metadata": {
    "id": "EMCouEuHhUxO",
    "colab_type": "code",
    "colab": {}
    },
    "source": [
    "auth = tweepy.OAuthHandler(API_KEY, API_KEY_SECRET)\n",
    "auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)\n",
    "api = tweepy.API(auth, wait_on_rate_limit=True)"
    ],
    "execution_count": 41,
    "outputs": []
    },
    {
    "cell_type": "code",
    "metadata": {
    "id": "Ac5FKQEBoYvT",
    "colab_type": "code",
    "colab": {}
    },
    "source": [
    "threshold = datetime.now() - timedelta(days=DAYS_WITHOUT_ACTIVITY)"
    ],
    "execution_count": 42,
    "outputs": []
    },
    {
    "cell_type": "code",
    "metadata": {
    "id": "WydohrPBh8JM",
    "colab_type": "code",
    "colab": {}
    },
    "source": [
    "count = 0\n",
    "data = []\n",
    "for user in api.friends_ids(USER):\n",
    " status = api.user_timeline(user, count=100)\n",
    "\n",
    " span = ((status[0].created_at - status[-1].created_at).days)\n",
    " frequency = (len(status) / span) if span > 0 else None\n",
    " \n",
    " if status[0].created_at < threshold or (frequency is not None and frequency < DAILY_TWEET_FREQUENCY):\n",
    " print(f\"Unfollowing @{status[0].user.screen_name} ({status[0].user.name}). Last status update on {status[0].created_at}. Frequency: {frequency:.2f}\")\n",
    " api.destroy_friendship(user)\n",
    " count += 1\n",
    "\n",
    "print(f\"You just unfollowed {count} accounts. @{USER} is now following {len(api.friends_ids(USER))}.\")\n"
    ],
    "execution_count": null,
    "outputs": []
    },
    {
    "cell_type": "code",
    "metadata": {
    "id": "7qCcjkHi1nrT",
    "colab_type": "code",
    "colab": {}
    },
    "source": [
    ""
    ],
    "execution_count": null,
    "outputs": []
    }
    ]
    }