Created
August 11, 2020 18:58
-
-
Save svpino/d7312c82162f093a4cb55e76b9805437 to your computer and use it in GitHub Desktop.
twitter-unfollow.ipynb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "twitter-unfollow.ipynb", | |
"provenance": [], | |
"collapsed_sections": [], | |
"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": { | |
"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": [] | |
} | |
] | |
} |
I don't understand anything here. How do I use it
I don't understand anything here. How do I use it
This is data science @karenefereyan
I don't understand anything here. How do I use it
Do You know how to code
@kolanse you dont have to be an ass about it. Not everyone knows what Jupyter Notebooks are
This is very cool
I want to convert this to a python library
@fardeen9983 I actually know what jupyter notebook is. Thanks @hikky08
O my God!!!! This is awesome
Great stuff
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very cool. Thanks for sharing.