{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Twitter API (brief introduction)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<U>Notes if you are using Jupyter Notebook</U>:  to call <B>exit()</B> from a notebook, please use <B>sys.exit()</B> (requires <B>import sys</B>); if a strange error occurs, it may be because Jupyter retains variables from all executed cells.  To reset the notebook's variables, click 'Restart Kernel' (the circular arrow) -- this will not undo any text changes.  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To access Twitter data programmatically, you must register with Twitter and establish a free or paid account.  All accounts are reviewed by Twitter and approval is not immediate.\n",
    "\n",
    "Free accounts are \"throttled\", meaning they will allow a limited number of requests in a 15-minute period.  \n",
    "\n",
    "It is likely that to make full use of real-time twitter data at scale, a paid account would be required."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h4>use Tweepy library to authenticate with Twitter</h4>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tweepy\n",
    "\n",
    "import creds   # a credentials files\n",
    "\n",
    "auth = tweepy.OAthHandler(keys.consumer_key,\n",
    "                          keys.consumer_secret)\n",
    "\n",
    "auth.set_access_token(keys.access_token,\n",
    "                      keys.access_token_secret)\n",
    "\n",
    "api = tweepy.API(auth, wait_on_rate_limit=True,\n",
    "                 wait_on_rate_limit_notify=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<H4>get user information</h4>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "nasa_user = api.get_user('nasa')\n",
    "\n",
    "print(nasa_user.id)                # 11348282\n",
    "\n",
    "print(nasa_user.name)              # 'NASA'\n",
    "\n",
    "print(nasa_user.followers_count)   # 29453541"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h4>term search</h4>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tweets = api.search(q='Trump', count=99)\n",
    "\n",
    "lnkd_tweets = []\n",
    "\n",
    "cursor = tweepy.cursor(api.search, q='LinkedIn')\n",
    "\n",
    "for tweet in cursor.items(10000):\n",
    "    lnkd_tweets.append(tweet)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h4>search modifiers (date, exclusions, positive or negative):</h4>\n",
    "\n",
    "https://twitter.com/search-home\n",
    "\n",
    "click on the operators link"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h4>search for a hash tag</h4>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fed_tweets = []\n",
    "\n",
    "cursor = tweepy.cursor(api.search, q='#federalreserve', from:2019-11-20)\n",
    "\n",
    "for tweet in cursor.items(10000):\n",
    "    lnkd_tweets.append(tweet)"
   ]
  }
 ],
 "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.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
