mikf / gallery-dl

Command-line program to download image galleries and collections from several image hosting sites
GNU General Public License v2.0
11.18k stars 911 forks source link

Integrate gallery_dl module with python #4801

Closed Azuriye closed 9 months ago

Azuriye commented 9 months ago

Basically I'm creating a small python bot where it scrapes the user's twitter link and uploads it to the respective channel.

import re
import json
import os
import discord
import traceback
from discord.ext import commands
from gallery_dl import config, job

# Load the contents of the config.json file
with open('./config.json', 'r') as file:
    config_data = json.load(file)

# Access the values
twitter_token = config_data.get('TwitterToken')
discord_token = config_data.get('DiscordToken')
discord_channels = config_data.get('DiscordChannels')

intents = discord.Intents.default()
intents.messages = True
intents.guilds = True

bot = commands.Bot(command_prefix='!', intents=intents)

@bot.event
async def on_ready():
    print(f'We have logged in as {bot.user}')

@bot.event
async def on_message(message):
    try:
        if message.author == bot.user:
            return

        if not discord_channels:
            print("Please configure the Discord Channels in the config.json")
            exit(1)
            return

        if str(message.channel.id) in discord_channels:
            content = message.clean_content

            if 'twitter.com' in content:
                url = ""
                try:
                    url = re.search('(https?://[^\s]*)', content).group(1)
                except AttributeError:
                    return False

                if message.attachments:
                    return

                # Load default config files
                config.load()

                # Set Twitter-specific configuration
                config.set(("extractor", "twitter", "cookies"), "auth_token", twitter_token)  # Twitter only

                downloaded_content = job.DownloadJob(url).run()

                if downloaded_content:
                    await message.channel.send(downloaded_content)
    except Exception:
        traceback.print_exc()  # Print the full traceback for debugging

@bot.event
async def on_error(event, *args, **kwargs):
    print(f'Error in event {event}: {args[0]}')

bot.run(discord_token)

I have two doubts before proceeding, Firstly is it possible to just download the image and send it through discord directly without saving the image in my hard drive? Sort of like downloading it in the memory and sending it to discord in an attempt to prevent clogging my storage.

If that's not possible I assume removing the file after uploading it to discord channel would be another approach?

As of now it's just downloading the image and my console prints out the image location as well but for some odd reason it doesn't send the image.

I'm not sure if someone here who worked with discord.py could help me out.

Azuriye commented 9 months ago

Issue resolved with implementation from https://github.com/mikf/gallery-dl/issues/642#issuecomment-1299951930