Skip to content

Add Instagram Downloader Plugin for Ultroid#497

Open
paman7647 wants to merge 2 commits intoTeamUltroid:mainfrom
paman7647:patch-2
Open

Add Instagram Downloader Plugin for Ultroid#497
paman7647 wants to merge 2 commits intoTeamUltroid:mainfrom
paman7647:patch-2

Conversation

@paman7647
Copy link

No description provided.

Copilot AI review requested due to automatic review settings March 1, 2026 16:57
Copy link
Author

@paman7647 paman7647 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xditya Review it

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds an Instagram media downloader command intended to integrate as an Ultroid plugin, using yt-dlp to fetch and upload Instagram reels/videos/photos to Telegram.

Changes:

  • Introduces a new Instagram downloader command (.ig <link>) with progress reporting.
  • Implements download extraction via yt-dlp and uploads results via fast_uploader + send_file.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

from pyUltroid import LOGS
from pyUltroid.fns.helper import humanbytes, run_async, time_formatter
from pyUltroid.fns.tools import set_attributes
from . import ultroid_cmd
Copy link

Copilot AI Mar 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This module is added at the repo root, but Ultroid’s Loader only auto-imports plugin modules from the plugins/ directory; additionally, from . import ultroid_cmd will fail unless the file is inside the plugins package. Please move/rename this to plugins/instagram.py (or similar) and keep imports consistent with other plugins so it’s actually loaded at startup.

Suggested change
from . import ultroid_cmd
from plugins import ultroid_cmd

Copilot uses AI. Check for mistakes.
Comment on lines +69 to +75
opts = {
"quiet": True,
"prefer_ffmpeg": True,
"geo-bypass": True,
"nocheckcertificate": True,
"outtmpl": "%(id)s.%(ext)s",
"progress_hooks": [lambda d: asyncio.create_task(ig_progress(d, time.time(), msg))],
Copy link

Copilot AI Mar 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

progress_hooks is invoked from the yt-dlp download thread (because _download_ig uses @run_async), so calling asyncio.create_task(...) here will raise RuntimeError: no running event loop and/or spawn an unbounded number of tasks. Capture the main loop once (e.g., loop = asyncio.get_running_loop() and a single start_time) and use a thread-safe scheduling approach (e.g., asyncio.run_coroutine_threadsafe) with proper rate-limiting.

Suggested change
opts = {
"quiet": True,
"prefer_ffmpeg": True,
"geo-bypass": True,
"nocheckcertificate": True,
"outtmpl": "%(id)s.%(ext)s",
"progress_hooks": [lambda d: asyncio.create_task(ig_progress(d, time.time(), msg))],
# Capture the main event loop and a single start time for this download session
loop = asyncio.get_running_loop()
start_time = time.time()
# Simple rate-limiting state to avoid flooding the loop with tasks
_rate_limit_state = {"last_update": 0.0}
def progress_hook(d):
"""
Thread-safe progress hook called by yt-dlp from its download thread.
Schedules ig_progress on the main asyncio loop with basic rate limiting.
"""
now = time.time()
# Allow an update at most once per second
if now - _rate_limit_state["last_update"] < 1.0:
return
_rate_limit_state["last_update"] = now
try:
asyncio.run_coroutine_threadsafe(
ig_progress(d, start_time, msg),
loop,
)
except RuntimeError:
# Event loop may be closed; ignore in that case
pass
opts = {
"quiet": True,
"prefer_ffmpeg": True,
"geo-bypass": True,
"nocheckcertificate": True,
"outtmpl": "%(id)s.%(ext)s",
"progress_hooks": [progress_hook],

Copilot uses AI. Check for mistakes.
Comment on lines +98 to +104
media_path = None
for f in glob.glob(f"{media_id}*"):
if not f.endswith(".jpg"):
media_path = f
break

if not media_path:
Copy link

Copilot AI Mar 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file-selection logic skips any downloaded .jpg file, which means Instagram photo posts (and carousels containing photos) will be silently ignored because the only downloaded media may be a .jpg. Instead of excluding .jpg, use yt-dlp’s returned filename fields (e.g., _filename/requested_downloads) or select by the entry’s expected extension so photos are handled correctly.

Suggested change
media_path = None
for f in glob.glob(f"{media_id}*"):
if not f.endswith(".jpg"):
media_path = f
break
if not media_path:
media_path = media.get("_filename")
# Fallback: construct from id/ext using outtmpl "%(id)s.%(ext)s"
if not media_path:
ext = media.get("ext")
if ext:
candidate = f"{media_id}.{ext}"
if os.path.exists(candidate):
media_path = candidate
# Last resort: any file starting with the media_id (no .jpg exclusion)
if not media_path:
matches = glob.glob(f"{media_id}.*")
if matches:
media_path = matches[0]
if not media_path or not os.path.exists(media_path):

Copilot uses AI. Check for mistakes.
Comment on lines +92 to +111
title = media.get("title") or "Instagram_Media"

if len(title) > 30:
title = title[:27] + "..."

# Find downloaded file from yt-dlp
media_path = None
for f in glob.glob(f"{media_id}*"):
if not f.endswith(".jpg"):
media_path = f
break

if not media_path:
continue

# Rename file
ext = "." + media_path.split(".")[-1]
final_name = f"{title}{ext}"

try:
Copy link

Copilot AI Mar 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

title comes from remote metadata and is used directly to build final_name. If the title contains path separators (e.g., ../, /, \) or other special characters, os.rename may write outside the working dir or fail unpredictably. Please sanitize to a safe basename (strip separators / reserved chars) before constructing the output filename.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants