From f715100dd52ae3afa4c8c1c6c1f78700b5f50bd1 Mon Sep 17 00:00:00 2001 From: erinhmclark Date: Fri, 28 Mar 2025 11:31:23 +0000 Subject: [PATCH] Add run_instagrapi_server.sh and update docs --- docs/source/how_to/run_instagrapi_server.md | 64 ++++++++++++++----- .../run_instagrapi_server.sh | 49 ++++++++++++++ 2 files changed, 96 insertions(+), 17 deletions(-) create mode 100755 scripts/instagrapi_server/run_instagrapi_server.sh diff --git a/docs/source/how_to/run_instagrapi_server.md b/docs/source/how_to/run_instagrapi_server.md index 4eb588e..ace1550 100644 --- a/docs/source/how_to/run_instagrapi_server.md +++ b/docs/source/how_to/run_instagrapi_server.md @@ -3,13 +3,37 @@ The instagram API Extractor requires access to a running instance of the InstagrAPI server. We have a lightweight script with the endpoints required for our Instagram API Extractor module which you can run locally, or via Docker. -To run this you need to install some additional requirements. -## Setup - -Although there is an option to run the server in a Docker container, the authentication is usually rejected without an additional session file, which can be created by running the server locally first. ⚠️ Warning: Remember that it's best not to use your own personal account for archiving. [Here's why](../installation/authentication.md#recommendations-for-authentication). +## Quick Start: Using Docker + +We've provided a convenient shell script (`run_instagrapi_server.sh`) that simplifies the process of setting up and running the Instagrapi server in Docker. This script handles building the Docker image, setting up credentials, and starting the container. + +### 🔧 Running the script: + +Run this script either from the repository root or from within the `scripts/instagrapi_server` directory: + +```bash +./scripts/instagrapi_server/run_instagrapi_server.sh +``` + +This script will: +- Prompt for your Instagram username and password. +- Create the necessary `.env` file. +- Build the Docker image. +- Start the Docker container and authenticate with Instagram, creating a session automatically. + +### ⏱ To run the server again later: +```bash +docker start ig-instasrv +``` + +### 🐛 Debugging: +View logs: +```bash +docker logs ig-instasrv +``` ### Overview: How the Setup Works @@ -20,12 +44,10 @@ Although there is an option to run the server in a Docker container, the authent --- -## 1. One-Time Local Setup +## Optional: Manual / Local Setup -This generates a session file using your login details so Instagram recognises your login as authentic. -This will be reused automatically by the server script, and can also be passed to the Docker container. +If you'd prefer to run the server manually (without Docker), you can follow these steps: -### 🔧 Step-by-step: 1. **Navigate to the server folder (and stay there for the rest of this guide)**: ```bash @@ -59,6 +81,17 @@ This will be reused automatically by the server script, and can also be passed t Login successful, session saved. ``` +✅ Your session is now saved to `secrets/instagrapi_session.json`. + +### To run it again locally: +```bash +poetry run uvicorn src.instaserver:app --port 8000 +``` + +--- + +## Adding the API Endpoint to Auto Archiver + The server should now be running within that session, and accessible at http://127.0.0.1:8000 You can set this in the Auto Archiver orchestration.yaml file like this: @@ -67,10 +100,6 @@ instagram_api_extractor: api_endpoint: http://127.0.0.1:8000 ``` -Or to run it in a Docker container, you can pass the session file to it now. -**Stop the server** (`Ctrl+C`). - -📅 Your session has now been saved to `secrets/instagrapi_session.json`. --- @@ -85,11 +114,11 @@ poetry run uvicorn src.instgrapinstance.instaserver:app --port 8000 --- -## 3. Running via Docker (After Setup to create the session file) +## 3. Running via Docker (After Setup is Complete, either locally or via the script) -Once the session file exists, you can pass this to docker Docker and it should authenticate successfully. +Once the `instagrapi_session.json` and `.env` files are set up, you can pass them Docker and it should authenticate successfully. -### 🔨 Build the Docker image: +### 🔨 Build the Docker image manually: ```bash docker build -t instagrapi-server . ``` @@ -104,7 +133,9 @@ docker run -d \ instagrapi-server ``` -This passes the /secrets/ directory to docker, which should contain the session file created from the first local run. +This passes the /secrets/ directory to docker as well as the environment variables from the `.env` file. + + --- @@ -134,6 +165,5 @@ docker start ig-instasrv ## Notes -- You only need to run the server **locally once** to generate a session. - Never share your `.env` or `instagrapi_session.json` — these contain sensitive login data. - If you want to reset your session, simply delete the `secrets/instagrapi_session.json` file and re-run the local server. diff --git a/scripts/instagrapi_server/run_instagrapi_server.sh b/scripts/instagrapi_server/run_instagrapi_server.sh new file mode 100755 index 0000000..9f19c1f --- /dev/null +++ b/scripts/instagrapi_server/run_instagrapi_server.sh @@ -0,0 +1,49 @@ +#!/usr/bin/env bash +# +# run_instagrapi_server.sh +# Usage: +# From repo root: ./scripts/instagrapi_server/run_instagrapi_server.sh +# From repo root: ./scripts/instagrapi_server/run_instagrapi_server.sh +# Or from script dir: ./run_instagrapi_server.sh +# + +set -e + +# Step 1: cd to the script's directory (contains Dockerfile and secrets/) +cd "$(dirname "$0")" || exit 1 + +# Create secrets/ if it doesn't exist +if [[ ! -d "secrets" ]]; then + echo "Creating secrets/ directory..." + mkdir secrets +fi + +echo "Enter your Instagram credentials to store in secrets/.env" +read -rp "Instagram Username: " IGUSER +read -rsp "Instagram Password: " IGPASS +echo "" + +cat < secrets/.env +INSTAGRAM_USERNAME=$IGUSER +INSTAGRAM_PASSWORD=$IGPASS +EOF +echo "Created secrets/.env with your credentials." + +# Build Docker image +IMAGE_NAME="instagrapi-server" +echo "Building Docker image '$IMAGE_NAME'..." +docker build -t "$IMAGE_NAME" . + +# Run container +CONTAINER_NAME="ig-instasrv" +echo "Running container '$CONTAINER_NAME'..." +docker run -d \ + --env-file secrets/.env \ + -v "$(pwd)/secrets:/app/secrets" \ + -p 8000:8000 \ + --name "$CONTAINER_NAME" \ + "$IMAGE_NAME" + +echo "Done! Instagrapi server is running on port 8000." +echo "Use 'docker logs $CONTAINER_NAME' to view logs." +echo "Use 'docker stop $CONTAINER_NAME' and 'docker rm $CONTAINER_NAME' to stop/remove the container."