From b2139ce2e989aca715cc228f2b247c579c0c1765 Mon Sep 17 00:00:00 2001 From: Shobhit Bhosure <38807205+shobhit99@users.noreply.github.com> Date: Wed, 18 Mar 2020 14:03:22 +0530 Subject: [PATCH] Instagram Video Downloader (#94) * Instagram Video Downloader downloads all the videos from instagram post * Update README.md Co-authored-by: Ayush Bhardwaj --- README.md | 1 + insta_video_downloader/README.md | 12 ++++++++ insta_video_downloader/instavideo.py | 38 +++++++++++++++++++++++++ insta_video_downloader/requirements.txt | 1 + 4 files changed, 52 insertions(+) create mode 100644 insta_video_downloader/README.md create mode 100644 insta_video_downloader/instavideo.py create mode 100644 insta_video_downloader/requirements.txt diff --git a/README.md b/README.md index daec0f4..9ec9134 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,7 @@ So far, the following projects have been integrated to this repo: |[Find PhoneNumber in String](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Find-PhoneNumber-in-String)|[Austin Zuniga](https://github.com/AustinZuniga)| |[IMDB TV Series Info Extractor](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/imdb_episode_ratings)|[Yash Raj Sarrof](https://github.com/yashYRS) | |[Yoda-speak Translator](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/speak_like_yoda)|[sonniki](https://github.com/sonniki) | +|[Instagram Video Downloader](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/insta_video_downloader)|[Shobhit Bhosure](https://github.com/shobhit99) | |[Medium Article Downloader](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/medium_article_downloader)|[coolsonu39](https://github.com/coolsonu39)| |[Face Recognition](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/image_recognition)|[LOKESH KHURANA](https://github.com/theluvvkhurana)| |[File Encrypt Decrypt](file-encrypt-decrypt)|[Aditya Arakeri](https://github.com/adityaarakeri)| diff --git a/insta_video_downloader/README.md b/insta_video_downloader/README.md new file mode 100644 index 0000000..f2b9763 --- /dev/null +++ b/insta_video_downloader/README.md @@ -0,0 +1,12 @@ +# Instagram Video Downloader +### Downlaods all the videos from instagram post. User needs to provide instagram post id as parameter +>> Example + +``` +python instavideo.py B3NUFfmgRYW +``` +>> Example output file + +``` +B3NUFfmgRYW_0.mp4 +``` \ No newline at end of file diff --git a/insta_video_downloader/instavideo.py b/insta_video_downloader/instavideo.py new file mode 100644 index 0000000..c05b6c4 --- /dev/null +++ b/insta_video_downloader/instavideo.py @@ -0,0 +1,38 @@ +import urllib.request +import requests +import json +import re +import sys + +def download(post_id): + multiple_posts = False + videos = [] + data = requests.get("https://instagram.com/p/{}".format(post_id)) + if data.status_code == 404: + print("Specified post not found") + sys.exit() + json_data = json.loads(re.findall(r'window._sharedData\s=\s(\{.*\});', data.text)[0]) + data = json_data['entry_data']['PostPage'][0]['graphql']['shortcode_media'] + if 'edge_sidecar_to_children' in data.keys(): + multiple_posts = True + caption = data['edge_media_to_caption']['edges'][0]['node']['text'] + media_url = data['display_resources'][2]['src'] + is_video = data['is_video'] + if not is_video and not multiple_posts: + print("No Videos found") + sys.exit() + if is_video: + videos.append(data['video_url']) + if multiple_posts: + for post in data['edge_sidecar_to_children']['edges']: + if post['node']['is_video']: + videos.append(post['node']['video_url']) + print("Found total {} videos".format(len(videos))) + for no, video in zip(list(range(len(videos))), videos): + print("Downloading video {}".format(no)) + urllib.request.urlretrieve(video, "{}_{}.mp4".format(post_id, no)) + +if len(sys.argv) == 1: + print("Please provide instagram post id") +else: + download(sys.argv[1]) \ No newline at end of file diff --git a/insta_video_downloader/requirements.txt b/insta_video_downloader/requirements.txt new file mode 100644 index 0000000..bebaa03 --- /dev/null +++ b/insta_video_downloader/requirements.txt @@ -0,0 +1 @@ +requests