Skip to content

Instantly share code, notes, and snippets.

@btahir
Created June 25, 2019 08:06
Show Gist options
  • Save btahir/2d9b2e952db272b3a96cee5038b5fcc2 to your computer and use it in GitHub Desktop.
Save btahir/2d9b2e952db272b3a96cee5038b5fcc2 to your computer and use it in GitHub Desktop.
instagram-playbook.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "instagram-playbook.ipynb",
"version": "0.3.2",
"provenance": [],
"toc_visible": true,
"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/btahir/2d9b2e952db272b3a96cee5038b5fcc2/instagram-playbook.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "RXwlVCfIiCtS",
"colab_type": "text"
},
"source": [
"# Instagram Playbook"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Vus1DLJViu_Q",
"colab_type": "text"
},
"source": [
"This notebook lets you upload photos/video to your instagram account.\n",
"\n",
"We will be using this nice unofficial Instagram API to connect to our account: https://github.com/LevPasha/Instagram-API-python.git\n",
"\n",
"We will use this API to do the following things:\n",
"\n",
"* Upload a single photo\n",
"* Upload multiple photos in one post (Carousel)\n",
"* Upload a video\n",
"\n",
"**Note:** You risk getting you account banned if you overuse/misuse this tool. You have been warned.\n"
]
},
{
"cell_type": "code",
"metadata": {
"id": "vVu5z9SCIK5S",
"colab_type": "code",
"colab": {}
},
"source": [
"# let's clone the repo\n",
"from IPython.display import clear_output\n",
"\n",
"!git clone https://github.com/LevPasha/Instagram-API-python.git\n",
" \n",
"clear_output()"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "XqvhKFb2iFgv",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "a74256a2-dddc-4690-da72-d3b4b02ef8ad"
},
"source": [
"cd Instagram-API-python"
],
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"text": [
"/content/Instagram-API-python\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "qCQLIJzziNo3",
"colab_type": "code",
"colab": {}
},
"source": [
"# install the requirements\n",
"!pip install -r requirements.txt\n",
"\n",
"clear_output()"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "KxbcVqrEiTax",
"colab_type": "text"
},
"source": [
"##Authenticate With Instagram"
]
},
{
"cell_type": "code",
"metadata": {
"id": "J4hja1kuiPKP",
"colab_type": "code",
"colab": {}
},
"source": [
"#@title Enter Instagram Credentials:\n",
"from InstagramAPI import InstagramAPI\n",
"\n",
"Username = '' #@param {type:\"string\"}\n",
"Password = '' #@param {type:\"string\"}\n",
"\n",
"InstagramAPI = InstagramAPI(Username, Password)\n",
"InstagramAPI.login()"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "K87sPdCakKiZ",
"colab_type": "text"
},
"source": [
"Please upload the files (photos/videos) to Colab before proceeding. You can use the Upload button (top left) to get files from your local computer or use wget or requests to download from urls."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "jFP2K8fmkjS5",
"colab_type": "text"
},
"source": [
"## Upload Single Photo"
]
},
{
"cell_type": "code",
"metadata": {
"id": "qLx39WJRiPis",
"colab_type": "code",
"colab": {}
},
"source": [
"#@markdown ### Add the filename for photo to be included.\n",
"Photo = '' #@param {type:\"string\"}\n",
"Caption = '' #@param {type:\"string\"}\n",
"#@markdown #### Add up to 30 hashtags like this: #instagram #python #notebook #api etc.\n",
"Hashtags = '' #@param {type:\"string\"}\n",
"\n",
"photo_path = '/content/' + Photo\n",
"all_content = Caption + '\\n\\n' + Hashtags\n",
"\n",
"InstagramAPI.uploadPhoto(photo_path, caption=all_content)"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "ZfS6jtSWknnw",
"colab_type": "text"
},
"source": [
"## Upload Carousel / Album (2-10 photos)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "-flK4-dylXZf",
"colab_type": "text"
},
"source": [
"Unfortunately, the API does not let us mix photos and videos together like the App. We will settle for doing this for just photos for now. We can add anywhere from 2-10 photos per post."
]
},
{
"cell_type": "code",
"metadata": {
"id": "9DT6BnauiPue",
"colab_type": "code",
"colab": {}
},
"source": [
"#@markdown ### Add the filenames for photos to be included.\n",
"photo_1 = '' #@param {type:\"string\"}\n",
"photo_2 = '' #@param {type:\"string\"}\n",
"photo_3 = '' #@param {type:\"string\"}\n",
"photo_4 = '' #@param {type:\"string\"}\n",
"photo_5 = '' #@param {type:\"string\"}\n",
"photo_6 = '' #@param {type:\"string\"}\n",
"photo_7 = '' #@param {type:\"string\"}\n",
"photo_8 = '' #@param {type:\"string\"}\n",
"photo_9 = '' #@param {type:\"string\"}\n",
"photo_10 = '' #@param {type:\"string\"}\n",
"#@markdown ### Post Content\n",
"Caption = '' #@param {type:\"string\"}\n",
"Hashtags = '' #@param {type:\"string\"}\n",
"\n",
"all_content = Caption + '\\n\\n' + Hashtags\n",
"\n",
"photoList = [photo_1, photo_2, photo_3, photo_4, photo_5,\n",
" photo_6, photo_7, photo_8, photo_9, photo_10]\n",
"\n",
"media = []\n",
"for photo in photoList:\n",
" if photo != '':\n",
" media.append({\n",
" 'type': 'photo',\n",
" 'file': '/content/' + photo \n",
" })\n",
"\n",
"InstagramAPI.uploadAlbum(media, caption=all_content)"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "6juYf8tHlOjK",
"colab_type": "text"
},
"source": [
"## Upload Video (Maximum: 60 seconds)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Ndpqp7HWljts",
"colab_type": "text"
},
"source": [
"Let's switch to video! We can upload a video of length at most 1 minute."
]
},
{
"cell_type": "code",
"metadata": {
"id": "TGsOU4d_iPsA",
"colab_type": "code",
"colab": {}
},
"source": [
"#@markdown ### Add the video filename (.mp4 etc) and preview thumbnail image\n",
"Video = '' #@param {type:\"string\"}\n",
"Thumbnail = '' #@param {type:\"string\"}\n",
"Caption = '' #@param {type:\"string\"}\n",
"Hashtags = '' #@param {type:\"string\"}\n",
"\n",
"video_path = '/content/' + Video\n",
"thumbnail_path = '/content/' + Thumbnail\n",
"all_content = Caption + '\\n\\n' + Hashtags\n",
"\n",
"\n",
"InstagramAPI.uploadVideo(video_path, thumbnail=thumbnail_path, caption=all_content)"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "BRaEQnspoh3z",
"colab_type": "text"
},
"source": [
"Yay! We were able to replicate most of Instagram's uploading behavior via this notebook. Try it out if it makes your life easier but please don't abuse their policies i.e. be an annoying bot. \n",
"\n",
"Remember - With great code comes great responsibility!"
]
},
{
"cell_type": "code",
"metadata": {
"id": "OYLeDViHqvVo",
"colab_type": "code",
"colab": {}
},
"source": [
""
],
"execution_count": 0,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment