Spaces:
Sleeping
Sleeping
| import yt_dlp | |
| import json | |
| import gradio as gr | |
| from datetime import datetime | |
| # Define your cookie string here | |
| cookie_string = "ps_n=1;datr=2J_dZcb_CIis0pgQNad0fqLy;ig_nrcb=1;ds_user_id=72979345438;csrftoken=mdwAyl0iOXWbFXx7vNEUf7vxGizG80y8;ig_did=FAA19E5B-5E11-42D3-8FC7-95FD118748A2;ps_l=1;wd=1904x918;mid=Z2kh7wAEAAG8D8kXI7cCtroTWDew;sessionid=72979345438%3AvQ6KirvjwELb8j%3A28%3AAYd1a2j7hnUcvZt2DDGbUUFIuNihMfeaYdxjQ_wxsw;dpr=1;rur=\"CLN\\05472979345438\\0541771850559:01f7ef2ea34cba0de75c19fcba6d6a708c1c5af7d60c7b795fbdeefd95b84250805e35f0\"" | |
| def parse_cookie_string(cookie_string): | |
| # Convert cookie string into a list of dictionaries with name, value, domain, and path | |
| cookies = [] | |
| cookie_list = cookie_string.split(';') | |
| for cookie in cookie_list: | |
| cookie = cookie.strip() | |
| if '=' in cookie: | |
| name, value = cookie.split('=', 1) | |
| cookies.append({'name': name, 'value': value, 'domain': 'instagram.com', 'path': '/'}) | |
| return cookies | |
| def get_instagram_media_info(link): | |
| try: | |
| # Parse the cookie string into individual cookies | |
| cookies = parse_cookie_string(cookie_string) | |
| # Prepare cookies as a dictionary for yt-dlp | |
| cookies_dict = {cookie['name']: cookie['value'] for cookie in cookies} | |
| # Set up yt-dlp options with cookies | |
| ydl_opts = { | |
| 'quiet': True, # Suppress unnecessary output | |
| 'force_generic_extractor': True, # Force generic extractor to handle Instagram links | |
| 'cookie': cookies_dict # Pass cookies directly | |
| } | |
| with yt_dlp.YoutubeDL(ydl_opts) as ydl: | |
| # Extract media information (without downloading) | |
| info_dict = ydl.extract_info(link, download=False) | |
| # Extract video title | |
| title = info_dict.get('title', 'No title available') | |
| # Check if audio is present | |
| has_audio = any(format.get('acodec') != 'none' for format in info_dict.get('formats', [])) | |
| # Initialize list to store format info | |
| formats_info = [] | |
| # Loop through formats and filter out audio and video formats | |
| for format in info_dict.get('formats', []): | |
| # Gather info on audio and video formats | |
| format_info = { | |
| 'format_id': format.get('format_id'), | |
| 'url': format.get('url'), | |
| 'audio': format.get('acodec') != 'none', # Audio is present if acodec exists | |
| 'height': format.get('height', None) # Resolution (height) | |
| } | |
| formats_info.append(format_info) | |
| # Prepare the final response | |
| response = { | |
| 'title': title, | |
| 'has_audio': has_audio, | |
| 'formats': formats_info, | |
| 'thumbnail': info_dict.get('thumbnail', 'No thumbnail available') | |
| } | |
| # Return the response as a formatted JSON string | |
| return json.dumps(response, indent=4) | |
| except Exception as e: | |
| return json.dumps({'error': str(e)}, indent=4) | |
| # Gradio interface | |
| def gradio_interface(link): | |
| return get_instagram_media_info(link) | |
| iface = gr.Interface( | |
| fn=gradio_interface, | |
| inputs=gr.Textbox(label="Instagram Link"), # User inputs Instagram link | |
| outputs=gr.JSON(label="Media Info"), # Output in JSON format | |
| title="Instagram Media Info Extractor", | |
| description="Enter the Instagram reel URL to get media info including title, formats, and audio details. (Your cookie is already set in the code.)" | |
| ) | |
| iface.launch() | |