Skip to content

Commit a131aa2

Browse files
FEAT: Implement Instagram Bot (#1300)
* Added Instagram_BOT * Added Instagram BOT * Added README.md for Instagram BOT * Added Instagram BOT * Update README.md * Added instagram_bot.py
1 parent 7a9b056 commit a131aa2

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed

Python/Instagram_BOT/README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# 🍕 Instagram BOT
2+
3+
## Prerequisites
4+
5+
* Create Virtual Environment
6+
```
7+
python -m venv env
8+
```
9+
* Activate the Environment
10+
```
11+
env\Scripts\activate
12+
```
13+
* Install all the dependencies
14+
```
15+
pip install -r requirements.txt
16+
```
17+
18+
## Usage
19+
20+
* Start the Script <code> instagram_bot.py</code>
21+
<img src = "https://user-images.githubusercontent.com/24206423/156173269-b6a641b0-6704-4fde-ac56-02a9f2763938.png" height = 300>
22+
23+
24+
* <code> 1</code> to Download Profile Image from Username
25+
* <code> 2</code> to Download All Posts from Username
26+
* <code> 3</code> to Download Images from Hashtags
27+
28+
## Examples
29+
30+
* <code>Example 1</code> - Download Profile Image of <code>jacquelinef143</code>
31+
<div>
32+
<img src = "https://user-images.githubusercontent.com/24206423/156174852-2615dd80-548e-4839-8105-a0accdefba6f.png" height = 200>
33+
<img src = "https://user-images.githubusercontent.com/24206423/156174856-1bd8eb95-69da-4591-814f-b75cde6db9e6.png">
34+
</div>
35+
<br>
36+
37+
* <code>Example 2</code> - Download Post Images of <code>jacquelinef143</code>
38+
39+
<div align = "center">
40+
<img src = "https://user-images.githubusercontent.com/24206423/156177713-f4b8a664-fc85-42fc-8d8f-c72b76442dde.gif">
41+
</div>
42+
<br>
43+
44+
* <code>Example 3</code> - Download Hash Tag Images of <code>dogs</code>
45+
46+
<div align = "center">
47+
<img src = "https://user-images.githubusercontent.com/24206423/156178673-6dc13f7b-49d6-4284-8cbd-9310356ad300.gif">
48+
</div>
49+
50+
## Author - [Harshit Bansal](https://github.com/harshitbansall)
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import os, shutil, sys, instaloader
2+
from prettytable import PrettyTable
3+
instagramBot = instaloader.Instaloader(quiet = True)
4+
5+
def auth():
6+
# Inputting the Username of User
7+
userName = input("Enter Your Instagram Username: ")
8+
try:
9+
# Try getting auth credentials from pre-saved files
10+
instagramBot.load_session_from_file(userName)
11+
except:
12+
try:
13+
# Make the auth credentials
14+
os.system("instaloader -l {0}".format(userName))
15+
instagramBot.load_session_from_file(userName)
16+
except:
17+
# Login in the account
18+
instagramBot.login(userName,input("Enter Password: "))
19+
instagramBot.save_session_to_file()
20+
print("{0} Logged In.".format(userName))
21+
22+
def main():
23+
# Login
24+
auth()
25+
# While Loop so Script does not exit
26+
while True:
27+
# Printing the Tasks
28+
print("Indexes and Tasks")
29+
myTable = PrettyTable(["Index","Task"])
30+
myTable.add_row(["1", "Download Profile Image from Username"])
31+
myTable.add_row(["2", "Download All Posts from Username"])
32+
myTable.add_row(["3", "Download Images from Hashtags"])
33+
print(myTable)
34+
# Inputting what to do
35+
query = input("Enter Index to Perform: ")
36+
# Download Profile Picture case
37+
if query == "1":
38+
currentPath = os.getcwd()
39+
username = input("Enter Username to Download Profile Image: ")
40+
instagramBot.download_profile(username, profile_pic_only = True)
41+
for file in os.listdir(username):
42+
if ".jpg" in file:
43+
shutil.move("{0}/{1}".format(username,file),currentPath)
44+
shutil.rmtree(username)
45+
print("'{0}' Profile Image Downloaded.".format(username))
46+
# Download all Photos and Videos
47+
elif query == "2":
48+
currentPath = os.getcwd()
49+
username = input("Enter Username to Download: ")
50+
try :
51+
profile = instaloader.Profile.from_username(instagramBot.context, username)
52+
except:
53+
print("Username Not Found.")
54+
if os.path.exists(username) is False:
55+
os.mkdir(username)
56+
os.chdir(username)
57+
if os.path.exists("Videos") is False:
58+
os.mkdir("Videos")
59+
posts = profile.get_posts()
60+
print("Downloading... Ctrl + C to Stop in between. Don't Open Username Folder.")
61+
for index, post in enumerate(posts):
62+
try:
63+
instagramBot.download_post(post, target = index)
64+
except KeyboardInterrupt:
65+
print("Downloader Exited.")
66+
break
67+
# Organizing the Directory
68+
for folder in os.listdir():
69+
if "." not in folder:
70+
for item in os.listdir(folder):
71+
if ".jpg" in item:
72+
shutil.move(folder+"/"+item, "{0}/{1}".format(currentPath, username))
73+
elif ".mp4" in item:
74+
try:
75+
shutil.move(folder+"/"+item, "{0}/{1}/Videos".format(currentPath, username))
76+
except:
77+
continue
78+
shutil.rmtree(folder)
79+
print("{} Folder Created.".format(username))
80+
# Download Images using hashtag
81+
elif query == "3":
82+
try:
83+
instaloader.Instaloader(download_videos=False, save_metadata=False, post_metadata_txt_pattern='').download_hashtag(input("Enter Hashtag: "), max_count=20)
84+
except KeyboardInterrupt:
85+
print("Downloader Exited")
86+
if __name__ == "__main__":
87+
main()
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
certifi==2021.10.8
2+
charset-normalizer==2.0.12
3+
idna==3.3
4+
instaloader==4.8.4
5+
prettytable==3.1.1
6+
requests==2.27.1
7+
urllib3==1.26.8
8+
wcwidth==0.2.5

0 commit comments

Comments
 (0)