Код: Выделить всё
import time
import instaloader
import requests
from io import BytesIO
def instagram_downloander(url, content_type):
print(f"Content Type: {content_type}")
try:
ig = instaloader.Instaloader()
shortcode = url.split('/')[-2]
post = instaloader.Post.from_shortcode(ig.context, shortcode)
for _ in range(3):
try:
post = instaloader.Post.from_shortcode(ig.context, shortcode)
if post.is_video:
video_url = post.video_url
response = requests.get(video_url)
if response.status_code == 200:
return BytesIO(response.content), 'video'
else:
image_url = post.url
response = requests.get(image_url)
if response.status_code == 200:
return BytesIO(response.content), 'image'
except instaloader.exceptions.TooManyRequestsException:
print("Rate limit reached. Retrying after delay...")
time.sleep(600)
except Exception as e:
print(f"Retry failed: {e}")
print("Unable to download the post. Please try later.")
return None, None
except instaloader.exceptions.BadResponseException as e:
print(f"An error occurred while fetching post metadata: {e}")
return None, None
except Exception as e:
print(f"Unexpected error: {e}")
time.sleep(10)
return instagram_downloander(url, content_type)
Код: Выделить всё
elif user_state.get(user_id) == 'instagram_post':
url = update.message.text
content_type = 'post'
await update.message.reply_text("Please wait 1 minute for loading...")
media, media_type = instagram_downloander(url, content_type)
if media and media_type:
if media_type == 'video':
await update.message.reply_video(video=media, filename='instagram_video.mp4')
elif media_type == 'image':
await update.message.reply_photo(photo=media, filename='instagram_image.jpg')
else:
await update.message.reply_text("Unsupported media type.")
# Credit.deduct_credits(user_id, 1)
await update.message.reply_text(f"🥳Download successful! \nFor using the bot again, please write /start.")
else:
await update.message.reply_text("Failed to download the Instagram content.")
user_state[user_id] = None
Когда я запускаю код на коде VS и тестирую бота Telegram, он работает правильно. Могу скачать ролики и выложить. Но когда я запускаю его с сервером (цифровой океан), он не работает. Почему? В чем может быть проблема? Спасибо всем.
Я пробую дополнительную библиотеку и меняю IP-адрес, но это не работает:(
Подробнее здесь: https://stackoverflow.com/questions/791 ... legram-bot
Мобильная версия