A Google Apps Script to automatically fetch the latest available IG Stories of a target Instagram user to your Google Drive.
๐ NEW RELEASE ON 2023-02-16 ๐
Thanks for the contributions from hidehiro98โค We can now save different downloaded IG users' files to seperate Google Drive folders ๐.
Please update the Library Version to the latest in your Google Script or make a new copy of the Google Sheet template named with V8 or later.
๐ช MINOR UPDATE ON 2023-02-14 ๐ช
๐งน Deprecated x-instagram-ajax
from the Settings. (* Update Library to V7 or later)
Starting from Build 2021.11.10, the Apps Script has been redesigned to work with a Google Sheet file as a user interface.
Make a copy of our Google Sheet template to your Google Drive by clicking the button below.
The spreadsheet contains the following three sheets:
Subscriptions : A table for you to list the Instagram users and their IDs that you want to subscribe to.
Logs : A log sheet of file download activities.
Settings : A tabular form for you to fill in the values of options and parameters for running the script.
You can use the Google Sheet file and bound our script to it in the following ways:
Method | Descriptions | Remarks |
---|---|---|
Use our shared library | Simply use the copy of our Google Sheet template. The functions to import and use our library are already present in the pre-built script bound to the Google Sheet template. |
|
Inject our code into your project | Modify the script bounded to your Google Sheet file by copying the source code in /dist/bundle.js to a .gs file and following the example below to call the functions from the IGSF object. |
|
Deploy a new library | Create a new Apps Script project, copy copying the source code in /dist/bundle.js to it, and deploy the project as a library for other scripts. |
|
Google Drive
- Folder ID (Optional) : The ID of a Google Drive folder that serves as the default save location. Media files will be downloaded to the Google Drive root folder if this value is unspecified.
-
x-asbd-id ๐ : The value of
x-asbd-id
request-header field sends when you browsewww.instagram.com
with logging in to an account. -
x-csrftoken ๐ : The value of
x-csrftoken
request-header field sends when you browsewww.instagram.com
with logging in to an account. -
x-ig-app-id : The value of
x-ig-app-id
request-header field sends when you browsewww.instagram.com
with logging in to an account. -
x-ig-www-claim : The value of
x-ig-www-claim
request-header field sends when you browsewww.instagram.com
with logging in to an account. -
x-instagram-ajax โ : This field has been deprecated from Build 2023.02.14 (= Library v7).
-
cookie : The value of
cookie
request-header field sends when you browsewww.instagram.com
with logging in to an account.
Health Monitoring
-
Badge File IDs (Optional)
-
Tested Date : The Google Drive file ID of
last-tested-date.svg
badge, which shows show the last execution date oftest_pipeline()
. -
Health Status : The Google Drive file ID of
last-tested-status.svg
badge, which shows if the last execution oftest_pipeline()
was "passed" or "failed".
-
-
Error Report (Optional)
- Email To
: The email address to receive an error message when the execution of
test_pipeline()
returns a "failed" status.
- Email To
: The email address to receive an error message when the execution of
- Visit
www.instagram.com
and log in to your account using a desktop browser, such as Chrome or Firefox. - Open the DevTool by pressing F12 or choose Inspect from the right-click menu on the page.
- Open the Network tab, then enter
?reel_ids=
in the filter. - Go back to the Instagram page and click on an IG story.
- While the stories are playing on the screen, new items named
?reel_ids=...
will be iteratively added to the list of request items. - Click on one of the fetched items and explore its Headers.
- Scroll to the Request Header section, grab the value of your cookie as in the picture below.
- Also, copy the values of
x-asbd-id
,x-csrftoken
,x-ig-app-id
andx-ig-www-claim
at the bottom of the same section.
Add the name and ID of your target Instagram user accounts to the table on the page "Subscriptions"
The Instagram User ID is necessary to query the data of the target Instagram user from the official web API. You can look up the ID with username by using the ID finder powered by The Code of a Ninja.
For example, label bbcnews as the name in the first column and put its ID 16278726 in the second column to subscribe to the stories from BBC News's Instagram account.
It will fetch the photos and videos from Instagram and upload them to your Google Drive folder if it finds any new stories from the listed accounts when you click on the โถRun button.
You can manually create a trigger with the following steps:
-
Open an Apps Script Editor from your Google Sheet file.
-
At the left, click Triggers โฐ.
-
At the bottom right, click Add Trigger.
-
In the dialog, choose
run
as the function to run and configure an appropriate time interval (6 - 12 hours) to run periodically, as the example below.
Instagram changes its API endpoint and data structure occasionally without any announcements. Therefore, you may want to set up a health monitoring to check if the script requests and handles the Instagram API data properly.
test_pipeline()
is a function to perform a health check, trigger badge updates, and error report emails. The health check is considered passing if it detects a presence of stories from the following Instagram accounts,
- bbcnews,
- cnn,
- medium, and
- nasa.
The health check is likely passing because these accounts publish stories frequently. If a health check fails but not an absence of stories on the Instagram accounts, it points out an error occurring in accessing the API endpoint or interpreting media URLs from the data.
You can set up a periodic health check with the following steps:
-
Open an Apps Script Editor from your Google Sheet file.
-
Add the following code to a
.gs
file:function runTestPipeline() { const IGSF = IGStoriesToGDrive.getInstance(); IGSF.test_pipeline(); }
-
At the left, click Triggers โฐ.
-
At the bottom right, click Add Trigger.
-
In the dialog, choose
runTestPipeline
as the function to run and configure an appropriate timer. -
(Optional) In your Google Sheet file, fill in an email address for error reporting on the page "Settings".
-
Open the page "Settings" in your Google Sheet file.
-
Click the + Create badges button to execute
createBadges()
. -
Open your Google Drive, you should see the two badge files,
last-tested-date.svg
andlast-tested-status.svg
, are created in the destination folder. -
Go back to the spreadsheet, their file IDs should be found on the page Settings.
-
Share the SVG files to view publicly and copy their Download URLs if you want to display them on a website or monitoring page.
You can publish the script as a web app and trigger the script with HTTP GET requests.
NOTE: The current version only supports web app deployment if you inject our source code directly into your Apps Script project.
To protect your deployed endpoint, you are required to set a username and password. Copy the following code to your project and replace myUsername
and myPassword
with your values:
function setUserProperties() {
PropertiesService
.getUserProperties()
.setProperty('AUTH_USERNAME', 'myUsername')
.setProperty('AUTH_PASSWORD', 'myPassword');
}
Run setUserProperties()
once to store your username and password as secret properties. After that, this code is no longer necessary and you can remove it.
Now you can test your web app deployment by passing a URL with query strings like this, https://script.google.com/<YOUR_APP_PATH>/exec?usr=<AUTH_USERNAME>&pwd=<AUTH_PASSWORD>&target={"name":"bbcnews","id":"16278726"}
.
- No sensitive data, like your Instagram username, password, cookies, credentials, and tokens, will be shared with our developers.
- The Apps Script, which bounds to a Google Sheet file that stores in your Google Drive will only run and be able to access with your Google Account unless you share the file with other users or place it in a shared folder.
- Self-hosting the code as a standalone project if you want better protection of your data.
- Do not use our script or shared library if you have any worries about any potential security issues with them.
๐ง NEW RELEASE ON 2022-09-04 ๐ง
Fixed the bug that caused (duplicated log entries).
๐ง IMPORTANT UPDATE ON 2022-08-23 ๐ง
The previous builds no longer work from Aug 16,2022 due to Instagram code changes. An ASBD identifier and a CSRF token are now required for authentication to access the Instagram endpoint. Please update to Build 2022.08.23, and make a copy of the new Google Sheet.
โจ NEW FEATURES RELEASED ON 2021-12-06 โจ
-
Save the filename of downloaded files in Column E on log sheet page.
-
Show the thumbnail preview and open the file on Drive by hovering and clicking on a hyperlinked filename.
-
Delete multiple items and their corresponding files from Drive by selecting the checkboxes in Column F and then clicking on "Delete Selected" of log sheet page.
๐๐ MAJOR UPGRADE ON 2021-11-08 ๐๐
Integration of Google Sheet was made to provide a user-friendly GUI for configurations and loggings. The new version allows you to set up your subscriptions and user settings easily and systematically, rather than hard coding them in the Apps Script. Logs will also store in the same Google Sheet file instead of saving separately in multiple Google Doc files.
๐ง IMPORTANT UPDATE ON 2021-09-12 ๐ง
(#17) Google Drive Drive will apply a security update on September 13, 2021. Please update your Apps Script code to avoid failing access to Google Drive files.
๐ง IMPORTANT UPDATE ON 2020-12-09 ๐ง
(#11) Instagram changed code around noon, 7 Dec, UTC. Please update to Build 2020.12.09.
๐ REMOVE THIRD-PARTY DEPENDENCIES in Build 2020.06.05 ๐
Starting from Build 2020.06.05, all story data and files will be fetched directly from Instagram.com.
๐ LAST UPDATE ON 2020-06-05 ๐
The version Build 2020.05.14 works again as storydownloader.net resumed its service on 2020-06-05.
๐ง IMPORTANT UPDATE ON 2020-06-02 ๐ง
The version Build 2020.05.14 failed on 2020-06-02 due to the suspension of the download source, storydownloader.net. The data of IG stories has been changed to fetch from the official site in the new version Build 2020.06.02.
Would you like to buy me a coffee? I would really appreciate it if you could support me for the development.
Distributed under the GNU Affero General Public License v3.0