-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
87 lines (74 loc) · 4.2 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# import instaloader
# from instabot import Bot
# import os
# import time # Import the time module
# def download_videos_and_post(usernames, target_folder, caption, ig_username, ig_password):
# ig = instaloader.Instaloader()
# ig.dirname_pattern = target_folder # Set the target folder for downloads
# post_counter = 1 # Initialize a counter for post numbering
# for username in usernames:
# profile = instaloader.Profile.from_username(ig.context, username)
# print(f"Downloading content from {username}")
# for post in profile.get_posts():
# if post.is_video or post.typename == 'GraphImage': # Check if post is a video or an image
# # Download the video or image
# ig.download_post(post, target=username)
# # Wait for 60 seconds before the next download request
# time.sleep(60) # Time interval between downloads
# # Prepare filenames with the new naming scheme
# file_extension = '.mp4' if post.is_video else '.jpg'
# media_filename = f"{target_folder}/post{post_counter}{file_extension}"
# post_counter += 1 # Increment the counter for each post
# # Post the video or image with the provided caption
# post_media_with_caption(media_filename, caption, username, ig_username, ig_password)
# # Wait for 180 seconds before posting the next video or image
# time.sleep(180) # Time interval between posts
# def post_media_with_caption(media_filename, caption, username_from, ig_username, ig_password):
# bot = Bot()
# bot.logout() # Clear any existing session data
# bot.login(username=ig_username, password=ig_password)
# # Modify the caption to include the username it was downloaded from
# modified_caption = f"{caption} (via @{username_from})"
# if media_filename.endswith('.mp4'):
# bot.upload_video(media_filename, caption=modified_caption)
# else:
# bot.upload_photo(media_filename, caption=modified_caption)
# # Get Instagram login credentials from the user
# ig_username = input("Enter your Instagram username: ")
# ig_password = input("Enter your Instagram password: ")
# # Example usage
# usernames = ['inaspx'] # Replace with actual usernames
# target_folder = 'Videos' # Replace with your target folder path
# caption = "Check out this cool post! Credits to:" # Your provided caption
# download_videos_and_post(usernames, target_folder, caption, ig_username, ig_password)
import instaloader
import os
import time # Import the time module
def download_videos(usernames, target_folder):
ig = instaloader.Instaloader()
ig.dirname_pattern = target_folder # Set the target folder for downloads
post_counter = 1 # Initialize a counter for post numbering
# Ensure the target folder exists
if not os.path.exists(target_folder):
os.makedirs(target_folder)
# Open a text file to store account names from which videos are downloaded
with open(f"{target_folder}/downloaded_videos_info.txt", "w") as info_file:
for username in usernames:
profile = instaloader.Profile.from_username(ig.context, username)
print(f"Downloading videos from {username}")
for post in profile.get_posts():
if post.is_video: # Check if post is a video
# Download the video
ig.download_post(post, target=username)
# Prepare filenames with the new naming scheme
media_filename = f"{target_folder}/video{post_counter}.mp4"
post_counter += 1 # Increment the counter for each post
print(f"Downloaded {media_filename}")
# Write the account name and video filename to the text file
info_file.write(f"{username}: {media_filename}\n")
# Wait for 60 seconds before the next download request
time.sleep(60) # Time interval between downloads
# Example usage
usernames = ['inaspx'] # Replace with actual usernames
target_folder = 'Videos' # Replace with your target folder path
download_videos(usernames, target_folder)