Last active
March 31, 2017 05:22
-
-
Save simecek/b7fa2778ab1d2b0bf383a616877514dc to your computer and use it in GitHub Desktop.
Managing Twitter Account With Tweepy
This file contains 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
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Function: Get List Of Users That Liked A Status" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"['472203302', '13520332', '2388203390', '732265223701286912', '416273351', '6490642', '284966399']\n" | |
] | |
} | |
], | |
"source": [ | |
"# copied from http://stackoverflow.com/questions/28982850/twitter-api-getting-list-of-users-who-favorited-a-status\n", | |
"\n", | |
"import urllib2\n", | |
"import re\n", | |
"\n", | |
"def get_user_ids_of_post_likes(post_id):\n", | |
" try:\n", | |
" json_data = urllib2.urlopen('https://twitter.com/i/activity/favorited_popup?id=' + str(post_id)).read()\n", | |
" found_ids = re.findall(r'data-user-id=\\\\\"+\\d+', json_data)\n", | |
" unique_ids = list(set([re.findall(r'\\d+', match)[0] for match in found_ids]))\n", | |
" return unique_ids\n", | |
" except urllib2.HTTPError:\n", | |
" return False\n", | |
"\n", | |
"# Example: \n", | |
"# https://twitter.com/golan/status/731770343052972032\n", | |
"\n", | |
"print get_user_ids_of_post_likes(731770343052972032)\n", | |
"\n", | |
"# ['13520332', '416273351', '284966399']\n", | |
"#\n", | |
"# 13520332 +> @TopLeftBrick\n", | |
"# 416273351 => @Berenger_r\n", | |
"# 284966399 => @FFrink" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Connect to Twitter with Tweepy" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"import tweepy\n", | |
"\n", | |
"# create file twkeys.py with our Twitter API keys\n", | |
"# see https://dototot.com/write-twitter-bot-python-tweepy-follow-new-followers/ \n", | |
"from twkeys import keys\n", | |
" \n", | |
"SCREEN_NAME = keys['screen_name']\n", | |
"CONSUMER_KEY = keys['consumer_key']\n", | |
"CONSUMER_SECRET = keys['consumer_secret']\n", | |
"ACCESS_TOKEN = keys['access_token']\n", | |
"ACCESS_TOKEN_SECRET = keys['access_token_secret']\n", | |
"\n", | |
"auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)\n", | |
"auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)\n", | |
"api = tweepy.API(auth)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Friends and Followers" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"# get list of followers' and friends' (=following) ids\n", | |
"followers = api.followers_ids(SCREEN_NAME)\n", | |
"friends = api.friends_ids(SCREEN_NAME)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"J_the_Prepper\n", | |
"EdEDock\n", | |
"ArtrBautista\n", | |
"Mrs_DarkDonado\n", | |
"odd_wheel\n", | |
"elearning_chad\n", | |
"Angel_Cruijff\n", | |
"mohhinder\n", | |
"paerallax\n", | |
"khalil_hughes\n", | |
"super__shoot\n", | |
"philippebiaut\n", | |
"jamfpoz\n", | |
"tedtedted\n", | |
"datenteiler\n", | |
"vikramcse10\n", | |
"benjixx\n", | |
"pro_cessor\n", | |
"mrjohnmorrow\n", | |
"aeroaks\n", | |
"ViUX\n", | |
"januszkowalczyk\n", | |
"isizo\n", | |
"gwuah_\n", | |
"Containerhouse\n", | |
"VaulsteinR\n", | |
"DanielFritzEU\n", | |
"allusernow\n", | |
"ManikosN\n", | |
"jdevoo\n", | |
"SeattleDataGuy\n", | |
"supreja_s\n" | |
] | |
} | |
], | |
"source": [ | |
"# print all friends that do not follow us\n", | |
"for f in friends:\n", | |
" if f not in followers:\n", | |
" print(api.get_user(f).screen_name)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"# unfollow non-following friends\n", | |
"# for f in friends:\n", | |
"# if f not in followers:\n", | |
"# api.destroy_friendship(f)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Users That Liked Any Of My Recent Tweets" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"49" | |
] | |
}, | |
"execution_count": 5, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"my_recent_tweets = api.user_timeline(screen_name = SCREEN_NAME, count=50)\n", | |
"len(my_recent_tweets)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"# apply 'get_user_ids_of_post_likes' to get users ids\n", | |
"users_liking_recent_tweet = [get_user_ids_of_post_likes(my_tweet.id) for my_tweet in my_recent_tweets]\n", | |
"\n", | |
"import itertools\n", | |
"users_liking_flat = set(list(itertools.chain.from_iterable(users_liking_recent_tweet)))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 49, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"34" | |
] | |
}, | |
"execution_count": 49, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# from the list, let us remove followers, friends and yourself\n", | |
"myself = api.get_user(screen_name = SCREEN_NAME).id\n", | |
"unknown_liking = set([int(f) for f in users_liking_flat]) - set(friends) - set(followers) - set([myself])\n", | |
"len(unknown_liking)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Be Nice To People That Like Your Tweets And Follow Them" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 50, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"for f in unknown_liking:\n", | |
" api.get_user(f).follow()" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 2", | |
"language": "python", | |
"name": "python2" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 2 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython2", | |
"version": "2.7.12" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment