File size: 3,443 Bytes
e4f3b0e
89c2bb8
 
 
 
 
e4f3b0e
129eb6c
 
89c2bb8
129eb6c
89c2bb8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e4f3b0e
40bc785
 
89c2bb8
e4f3b0e
40bc785
89c2bb8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e4f3b0e
89c2bb8
d9fb47d
89c2bb8
 
 
53127a1
 
7c56264
 
 
53127a1
7c56264
 
 
 
 
 
53127a1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import gradio as gr
import discord
from discord.ext import commands
import asyncio
import os
import re

intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(command_prefix='!', intents=intents)

@bot.command(name='export')
async def export_messages(ctx):
    await ctx.send('Exporting messages...')
    user = bot.get_user(USER_ID)
    messages = await user.history(limit=None).flatten()
    with open('messages.md', 'w', encoding='utf-8') as f:
        for message in messages:
            if message.author == user:
                f.write(f'{message.created_at} - {message.content}\n')
    await ctx.send('Messages exported!')
    
    urls = []
    with open('urls.md', 'w', encoding='utf-8') as f:
        for message in messages:
            if message.author == user:
                if re.search("(?P<url>https?://[^\s]+)", message.content):
                    urls.append(message.content)
                    f.write(f'{message.created_at} - {message.content}\n')
    await ctx.send('URLs exported!')

    await ctx.send('Launching Gradio interface...')
    gr.Interface(fn=download_files, inputs=[gr.inputs.Textbox(label="Directory path")], 
                           outputs=["update", "update", "update", "update", "update", "update"]).launch()


async def download_files(directory_path):
    messages_file_path = os.path.join(directory_path, 'messages.md')
    urls_file_path = os.path.join(directory_path, 'urls.md')
    total_messages = sum(1 for line in open(messages_file_path))
    total_urls = sum(1 for line in open(urls_file_path))
    messages_downloaded = 0
    urls_downloaded = 0
    with open(messages_file_path, 'r', encoding='utf-8') as f:
        for line in f:
            messages_downloaded += 1
            await asyncio.sleep(0) # allow other tasks to run
    with open(urls_file_path, 'r', encoding='utf-8') as f:
        for line in f:
            urls_downloaded += 1
            await asyncio.sleep(0) # allow other tasks to run
    messages_left = total_messages - messages_downloaded
    urls_left = total_urls - urls_downloaded
    time_left = (messages_left + urls_left) / 10 # assume 10 messages/urls downloaded per second
    update_messages = f"Messages Found: {total_messages}, Downloaded: {messages_downloaded}, Left: {messages_left}, ETA: {time_left:.0f} seconds"
    update_urls = f"URLs Found: {total_urls}, Downloaded: {urls_downloaded}, Left: {urls_left}, ETA: {time_left:.0f} seconds"
    update_progress = f"{messages_downloaded + urls_downloaded} / {total_messages + total_urls}"
    update_dir = f"Files will be saved in: {directory_path}"
    update_debug = f"Debug mode is {'on' if DEBUG_MODE else 'off'}"
    update_ip = f"Access the output at: http://localhost:8000/"
    return update_messages, update_urls, update_progress, update_dir, update_debug, update_ip

DEBUG_MODE = False # set to True to enable debug mode

if DEBUG_MODE:
    bot.run(TOKEN)
else:
  import gradio as gr

def export_messages(name, user_id, directory_path):
    # your script code here
    return "Messages have been exported."

iface = gr.Interface(fn=export_messages, 
                     inputs=[gr.inputs.Textbox(label="Name"), 
                             gr.inputs.Textbox(label="User ID"), 
                             gr.inputs.Textbox(label="Directory path")], 
                     outputs="text", 
                     title='Discord Message Exporter')
iface.launch()