Added quick & dirty yt-dlp bot for instagram video links
This commit is contained in:
		
							parent
							
								
									db83f35e12
								
							
						
					
					
						commit
						e52f9bb963
					
				
							
								
								
									
										114
									
								
								python/ig-ytdlpbot.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										114
									
								
								python/ig-ytdlpbot.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,114 @@
 | 
				
			|||||||
 | 
					import sys,discord,logging,logging.handlers,datetime,os,subprocess
 | 
				
			||||||
 | 
					from discord.ext import commands
 | 
				
			||||||
 | 
					from discord import app_commands
 | 
				
			||||||
 | 
					from signal import signal, SIGINT
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# BOT STATICS
 | 
				
			||||||
 | 
					discordtoken='put yer token here, matey'
 | 
				
			||||||
 | 
					botowner='your id goes here :D'
 | 
				
			||||||
 | 
					botname='ytdlp-bot'
 | 
				
			||||||
 | 
					dependencies=['yt-dlp']
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# BOT SETTINGS INIT
 | 
				
			||||||
 | 
					intents = discord.Intents.default()
 | 
				
			||||||
 | 
					intents.message_content=True
 | 
				
			||||||
 | 
					intents.members=True
 | 
				
			||||||
 | 
					client = discord.Client(intents=intents)
 | 
				
			||||||
 | 
					tree = app_commands.CommandTree(client)
 | 
				
			||||||
 | 
					reqperms=discord.Permissions()
 | 
				
			||||||
 | 
					reqperms.manage_messages = True
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# LOGGING INIT
 | 
				
			||||||
 | 
					logging.basicConfig()
 | 
				
			||||||
 | 
					logger=logging.getLogger(botname)
 | 
				
			||||||
 | 
					logger.setLevel(logging.DEBUG)
 | 
				
			||||||
 | 
					formatter=logging.Formatter('[{asctime}] [{levelname:<8}] {name}: {message}', '%Y-%m-%d %H:%M:%S', style='{')
 | 
				
			||||||
 | 
					handler=logging.handlers.RotatingFileHandler(filename=f"{botname}.log", encoding='utf-8', maxBytes=32*1024*1024, backupCount=5)
 | 
				
			||||||
 | 
					handler.setFormatter(formatter)
 | 
				
			||||||
 | 
					logger.addHandler(handler)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# HELPER FUNCTIONS
 | 
				
			||||||
 | 
					def debug(a):
 | 
				
			||||||
 | 
					 logger.debug(a)
 | 
				
			||||||
 | 
					def info(a):
 | 
				
			||||||
 | 
					 logger.info(a)
 | 
				
			||||||
 | 
					def warn(a):
 | 
				
			||||||
 | 
					 logger.warning(a)
 | 
				
			||||||
 | 
					def error(a):
 | 
				
			||||||
 | 
					 logger.error(a)
 | 
				
			||||||
 | 
					def critical(a):
 | 
				
			||||||
 | 
					 logger.critical(a)
 | 
				
			||||||
 | 
					def timestr(time):
 | 
				
			||||||
 | 
					 return f"{datestr(time)} {time.hour:02}:{time.minute:02}"
 | 
				
			||||||
 | 
					def datestr(time):
 | 
				
			||||||
 | 
					 return f"{time.year}-{time.month:02}-{time.day:02}"
 | 
				
			||||||
 | 
					def timemath(delta): # delta = # of days in the past
 | 
				
			||||||
 | 
					 now=datetime.datetime.now()
 | 
				
			||||||
 | 
					 then=now-datetime.timedelta(days=delta)
 | 
				
			||||||
 | 
					 return then
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def bincheck(binary):
 | 
				
			||||||
 | 
					  ytdlpid = subprocess.Popen(["which", binary], stdout=subprocess.PIPE)
 | 
				
			||||||
 | 
					  exit_code = ytdlpid.wait()
 | 
				
			||||||
 | 
					  return True if exit_code == 0 else False
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def cleanstr(string):
 | 
				
			||||||
 | 
					  return string.encode('ascii', errors='ignore').decode().lower()[:20]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def isntowner(ctx):
 | 
				
			||||||
 | 
					  return False if str(ctx.author.id) == botowner else True
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					info("")
 | 
				
			||||||
 | 
					info("--  STARTING UP  --")
 | 
				
			||||||
 | 
					info("")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					async def ytdl(message,ytdlink):
 | 
				
			||||||
 | 
					  # https://stackoverflow.com/a/707001
 | 
				
			||||||
 | 
					  ytdlpid = subprocess.Popen(["yt-dlp", ytdlink], stdout=subprocess.PIPE,text=True)
 | 
				
			||||||
 | 
					  (output, err) = ytdlpid.communicate()
 | 
				
			||||||
 | 
					  exit_code = ytdlpid.wait()
 | 
				
			||||||
 | 
					  arrput=output.split('\n')
 | 
				
			||||||
 | 
					  filename=''
 | 
				
			||||||
 | 
					  if exit_code != 0:
 | 
				
			||||||
 | 
					    return (exit_code,err)
 | 
				
			||||||
 | 
					  for line in arrput:
 | 
				
			||||||
 | 
					    if "Destination:" in line:
 | 
				
			||||||
 | 
					      filename=' '.join(line.split(' ')[2:])
 | 
				
			||||||
 | 
					      return (exit_code,filename)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Dependency check
 | 
				
			||||||
 | 
					for dep in dependencies:
 | 
				
			||||||
 | 
					  if (not bincheck(dep)):
 | 
				
			||||||
 | 
					    critical(f"Cannot find dependency {dep} - exiting")
 | 
				
			||||||
 | 
					    sys.exit(1)
 | 
				
			||||||
 | 
					info("Dependency check completed successfully")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@client.event
 | 
				
			||||||
 | 
					async def on_message(message):
 | 
				
			||||||
 | 
					  if message.author == client.user:
 | 
				
			||||||
 | 
					    return
 | 
				
			||||||
 | 
					  if isntowner(message):
 | 
				
			||||||
 | 
					    return
 | 
				
			||||||
 | 
					  if "instagram.com/reel" in message.content or "instagram.com/p" in message.content:
 | 
				
			||||||
 | 
					    debug(f"Detected instagram link in message '{message.content}' from '{message.author.name}'")
 | 
				
			||||||
 | 
					    if "bot trigger word" not in message.content:
 | 
				
			||||||
 | 
					      debug(f"'{message.author.name}' not owner >:(")
 | 
				
			||||||
 | 
					      return
 | 
				
			||||||
 | 
					    iglink=message.content.split(' ')[-1]
 | 
				
			||||||
 | 
					    if "instagram.com" not in iglink:
 | 
				
			||||||
 | 
					      error(f"Detected link in message.content is invalid: {iglink}")
 | 
				
			||||||
 | 
					      await message.reply(content=f"bruh {iglink} is broken or somethin...")
 | 
				
			||||||
 | 
					      # invalid lunk
 | 
				
			||||||
 | 
					      return
 | 
				
			||||||
 | 
					    (ytdlcode,strdata) = await ytdl(message,iglink)
 | 
				
			||||||
 | 
					    if ytdlcode == 0:
 | 
				
			||||||
 | 
					      info(f"Successfully downloaded video - uploading now...")
 | 
				
			||||||
 | 
					      myfile=discord.File(f"{strdata}")
 | 
				
			||||||
 | 
					      await message.reply(file=myfile)
 | 
				
			||||||
 | 
					      info(f"deleting {strdata} file...")
 | 
				
			||||||
 | 
					      os.remove(f"{strdata}")
 | 
				
			||||||
 | 
					    else:
 | 
				
			||||||
 | 
					      error(f"ytdl exited ({ytdlcode}) - {strdata}")
 | 
				
			||||||
 | 
					      await message.reply(content=f"encountered an error, send help pls: {strdata}")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					client.run(discordtoken
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user