Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
{
"cells": [
{
"cell_type": "code",
"execution_count": 323,
"metadata": {},
"outputs": [],
"source": [
"def twitter_setup():\n",
" auth=tweepy.OAuthHandler(consumer_key, consumer_secret)\n",
" auth.set_access_token(access_key, access_secret)\n",
" \n",
" api = tweepy.API(auth)\n",
" return api"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Getting tweets before 1128345760368431105\n",
"Number of Tweets have been downloaded so far: 400\n",
"Getting tweets before 1123512982741241855\n",
"Number of Tweets have been downloaded so far: 600\n",
"Getting tweets before 1118686321826885631\n",
"Number of Tweets have been downloaded so far: 800\n",
"Getting tweets before 1114354119777685504\n",
"Number of Tweets have been downloaded so far: 1000\n",
"Getting tweets before 1108936425922150403\n",
"Number of Tweets have been downloaded so far: 1200\n",
"Getting tweets before 1102590748438884351\n",
"Number of Tweets have been downloaded so far: 1400\n",
"Getting tweets before 1094659509199417343\n",
"Number of Tweets have been downloaded so far: 1600\n",
"Getting tweets before 1088423991100956671\n",
"Number of Tweets have been downloaded so far: 1800\n",
"Getting tweets before 1081572431146483711\n",
"Number of Tweets have been downloaded so far: 2000\n",
"Getting tweets before 1074895701836877823\n",
"Number of Tweets have been downloaded so far: 2200\n",
"Getting tweets before 1067256569090457599\n",
"Number of Tweets have been downloaded so far: 2400\n",
"Getting tweets before 1061971475505143810\n",
"Number of Tweets have been downloaded so far: 2600\n",
"Getting tweets before 1055812121798144000\n",
"Number of Tweets have been downloaded so far: 2800\n",
"Getting tweets before 1052230249360982015\n",
"Number of Tweets have been downloaded so far: 3000\n",
"Getting tweets before 1047094193368305664\n",
"Number of Tweets have been downloaded so far: 3200\n",
"Getting tweets before 1041751619602456575\n",
"Number of Tweets have been downloaded so far: 3226\n",
"Getting tweets before 1041701171143090176\n",
"Number of Tweets have been downloaded so far: 3226\n",
"5 recent Tweets\n",
"Spent time with my Mother and sought her blessings. https://t.co/DZmYSi9HaR\n",
"\n",
"Located in Khanpur in Ahmedabad, the BJP headquarters has witnessed many landmark events. It was from here that BJP… https://t.co/QZLaOZr04s\n",
"\n",
"Thankful to the people of Ahmedabad for coming to bless us this evening. https://t.co/p19EIvoNMI\n",
"\n",
"Thank you Deputy PM @drwanazizah for your good wishes. I recall our meeting during my visit to Malaysia last year.… https://t.co/GkMIusQWaQ\n",
"\n",
"Thank you for your kind words @RailaOdinga. India values the partnership with Kenya and we will continue to bring o… https://t.co/gAEfwxqMYM\n",
"\n"
]
}
],
"source": [
"screen_name=\"narendramodi\"\n",
"\n",
"def get_all_tweets(screen_name):\n",
" auth=tweepy.OAuthHandler(consumer_key, consumer_secret)\n",
" auth.set_access_token(access_key, access_secret)\n",
" api=tweepy.API(auth)\n",
" \n",
" tweets=[]\n",
" new_tweets=api.user_timeline(screen_name=screen_name, count=200)\n",
" tweets.extend(new_tweets)\n",
" \n",
" oldest=tweets[-1].id-1\n",
" \n",
" while len(new_tweets)>0:\n",
" print(f\"Getting tweets before {oldest}\")\n",
" #all request will include oldest parameter to exclude the chances of the duplicates \n",
" new_tweets=api.user_timeline(screen_name=screen_name, count=200, max_id=oldest)\n",
" tweets.extend(new_tweets)\n",
" oldest=tweets[-1].id-1\n",
" print(f\"Number of Tweets have been downloaded so far: {len(tweets)}\")\n",
" \n",
" return tweets\n",
"\n",
"tweets=get_all_tweets(screen_name)\n",
"print(\"5 recent Tweets\")\n",
"for tweet in tweets[:5]:\n",
" print(tweet.text)\n",
" print()"
]
}
],
"metadata": {
"celltoolbar": "Edit Metadata",
"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.7.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment