From 20c95d35c70d11289eb50e48be609f5bec171df7 Mon Sep 17 00:00:00 2001 From: Varun Tiwari Date: Mon, 10 Oct 2022 22:08:23 +0530 Subject: [PATCH] Added Python script to download instagram videos --- scripts/instagram-downloader/README.md | 13 +++++++++ scripts/instagram-downloader/main.py | 38 ++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 scripts/instagram-downloader/README.md create mode 100644 scripts/instagram-downloader/main.py diff --git a/scripts/instagram-downloader/README.md b/scripts/instagram-downloader/README.md new file mode 100644 index 0000000..ee14524 --- /dev/null +++ b/scripts/instagram-downloader/README.md @@ -0,0 +1,13 @@ +# JSON to Excel converter + +This is a simple Python script to download instagram videos. + +## Usage + +```bash +# install xlwt +pip install requests + +# run script +python main.py +``` diff --git a/scripts/instagram-downloader/main.py b/scripts/instagram-downloader/main.py new file mode 100644 index 0000000..28f3643 --- /dev/null +++ b/scripts/instagram-downloader/main.py @@ -0,0 +1,38 @@ +import urllib.request +import requests +import json +import re +import sys + + +def downloadVideo(postId): + is_multiple_post = False + videos = [] + url = requests.get("https://instagram.com/p/" + str(postId)) + if url.status_code == 404: + print("Specified post not found") + sys.exit() + json_data = json.loads(re.findall(r"window._sharedData\s=\s(\{.*\});", url.text)[0]) + data = json_data["entry_data"]["PostPage"][0]["graphql"]["shortcode_media"] + if "edge_sidecar_to_children" in data.keys(): + is_multiple_post = True + isVideo = data["is_video"] + if not isVideo and not is_multiple_post: + print("Video not found!") + sys.exit() + if isVideo: + videos.append(data["video_url"]) + if is_multiple_post: + for post in data["edge_sidecar_to_children"]["edges"]: + if post["node"]["is_video"]: + videos.append(post["node"]["video_url"]) + print("Found " + str(len(videos)) + " videos") + for number, video in zip(list(range(len(videos))), videos): + print("Downloading video " + str(number + 1)) + urllib.request.urlretrieve(video, (postId + "_" + str(number + 1) + ".mp4")) + + +if len(sys.argv) == 1: + print("Please enter the video ID") +else: + downloadVideo(sys.argv[1]) \ No newline at end of file