Skip to content

Instantly share code, notes, and snippets.

@divyajyotiuk
Forked from svpino/twitter-unfollow.ipynb
Last active March 29, 2023 10:01
Show Gist options
  • Save divyajyotiuk/4cec37e0142119f0be6abd35a2904c6f to your computer and use it in GitHub Desktop.
Save divyajyotiuk/4cec37e0142119f0be6abd35a2904c6f to your computer and use it in GitHub Desktop.
twitter-unfollow.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "view-in-github"
},
"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",
"execution_count": 18,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "fUpFTRp7eooH"
},
"outputs": [],
"source": [
"import os\n",
"import tweepy\n",
"import pandas as pd\n",
"from datetime import datetime, timedelta"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "EE7qH-Vdes0w"
},
"outputs": [],
"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"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "EMCouEuHhUxO"
},
"outputs": [],
"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)"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "Ac5FKQEBoYvT"
},
"outputs": [],
"source": [
"threshold = datetime.now() - timedelta(days=DAYS_WITHOUT_ACTIVITY)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "WydohrPBh8JM"
},
"outputs": [],
"source": [
"count = 0\n",
"data = []\n",
"for user in api.friends_ids(USER):\n",
" status = api.user_timeline(user, count=100)\n",
" if(len(status) > 0):\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))}.\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "7qCcjkHi1nrT"
},
"outputs": [],
"source": []
}
],
"metadata": {
"colab": {
"authorship_tag": "ABX9TyPvmBNi6ybzh03oqQrRZHOQ",
"collapsed_sections": [],
"include_colab_link": true,
"name": "twitter-unfollow.ipynb",
"provenance": []
},
"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.8.3"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment