Skip to main content

Add a script for downloading photos from Instagram

ID
ed653a3
date
2024-01-16 21:02:37+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
48a8cea
message
Add a script for downloading photos from Instagram
changed files
5 files, 50 additions, 2 deletions

Changed files

config.fish (5864) → config.fish (5919)

diff --git a/config.fish b/config.fish
index 8077d5c..3f53bdf 100644
--- a/config.fish
+++ b/config.fish
@@ -169,6 +169,7 @@ __create_python_script_alias text/fix_twemoji.py
 __create_python_script_alias text/fix_twitter_thread.py
 __create_python_script_alias text/noplaylist.py
 __create_python_script_alias text/sumsizes.py
+__create_python_script_alias web/download_instagram.py
 __create_python_script_alias web/yt-dlp.py
 
 __create_python_module_alias datasette

requirements.in (256) → requirements.in (268)

diff --git a/requirements.in b/requirements.in
index 268fe8b..2ff08a0 100644
--- a/requirements.in
+++ b/requirements.in
@@ -11,6 +11,7 @@ flickr-url-parser
 httpx
 humanize
 hyperlink
+instaloader
 keyring
 naturalsort==1.5.1
 Pillow

requirements.txt (4008) → requirements.txt (4085)

diff --git a/requirements.txt b/requirements.txt
index de5ce86..6f80336 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -92,6 +92,8 @@ idna==3.6
     #   requests
 iniconfig==2.0.0
     # via pytest
+instaloader==4.10.3
+    # via -r requirements.in
 itsdangerous==2.1.2
     # via
     #   asgi-csrf
@@ -169,7 +171,9 @@ pyyaml==6.0.1
 regex==2023.12.25
     # via -r requirements.in
 requests==2.31.0
-    # via yt-dlp
+    # via
+    #   instaloader
+    #   yt-dlp
 s3transfer==0.8.2
     # via boto3
 six==1.16.0

web/README.md (2215) → web/README.md (2601)

diff --git a/web/README.md b/web/README.md
index 2a8b3bd..bd15b7d 100644
--- a/web/README.md
+++ b/web/README.md
@@ -18,6 +18,12 @@ folder_name = "web"
 
 scripts = [
     {
+        "usage": "download_instagram.py [POST_URL]",
+        "description": """
+        download the photos from an Instagram post.
+        """
+    },
+    {
         "name": "imdown",
         "description": """
         I run this when my Internet connection goes down, and it makes an audible "ping" when it comes back up.
@@ -42,6 +48,15 @@ cog_helpers.create_description_table(folder_name=folder_name, scripts=scripts)
 ]]]-->
 <dl>
   <dt>
+    <a href="https://github.com/alexwlchan/scripts/blob/main/web/download_instagram.py">
+      <code>download_instagram.py [POST_URL]</code>
+    </a>
+  </dt>
+  <dd>
+    download the photos from an Instagram post.
+  </dd>
+
+  <dt>
     <a href="https://github.com/alexwlchan/scripts/blob/main/web/imdown">
       <code>imdown</code>
     </a>
@@ -68,4 +83,4 @@ cog_helpers.create_description_table(folder_name=folder_name, scripts=scripts)
     this is a wrapper around <a href="https://github.com/yt-dlp/yt-dlp">yt-dlp</a> that does parallel downloads of videos in playlists.
   </dd>
 </dl>
-<!-- [[[end]]] (checksum: 248e56c72ea624b8450e3e39e63663e1) -->
+<!-- [[[end]]] (checksum: ba9551c2698692a04b0fc4c5a45c7cf5) -->

web/download_instagram.py (0) → web/download_instagram.py (547)

diff --git a/web/download_instagram.py b/web/download_instagram.py
new file mode 100755
index 0000000..eb53b76
--- /dev/null
+++ b/web/download_instagram.py
@@ -0,0 +1,27 @@
+#!/usr/bin/env python3
+"""
+Download the photos from an Instagram post.
+"""
+
+import sys
+
+import hyperlink
+import instaloader
+
+
+if __name__ == '__main__':
+    try:
+        url = sys.argv[1]
+    except IndexError:
+        sys.exit(f"Usage: {__file__} <URL>")
+
+    url = hyperlink.DecodedURL.from_text(url)
+
+    assert url.host == 'www.instagram.com', url
+    assert url.path[0] == 'p', url
+
+    post_id = url.path[1]
+
+    L = instaloader.Instaloader()
+    post = instaloader.Post.from_shortcode(L.context, post_id)
+    L.download_post(post, post_id)