Last active 1639361745

Steven Smith revised this gist 1515814255. Go to revision

1 file changed, 47 insertions, 14 deletions

searchbot.py

@@ -4,9 +4,10 @@ import asyncio
4 4 import aiohttp
5 5 from urllib.parse import quote_plus, urlencode
6 6 import os.path
7 - from os import getcwd
7 + from os import getcwd, chdir
8 8 from urllib.parse import unquote
9 9 from mutagen import File
10 + import youtube_dl
10 11 client = discord.Client()
11 12
12 13 if not discord.opus.is_loaded():
@@ -23,15 +24,31 @@ player = None
23 24
24 25 @client.event
25 26 async def on_message(message):
26 - def after():
27 - coro = client.voice_client_in(message.server).disconnect()
28 - fut = asyncio.run_coroutine_threadsafe(coro, client.loop)
29 - try:
30 - fut.result()
31 - except:
32 - pass
33 27 global lastresults
34 28 global player
29 + if message.content.startswith("!upload ") and any(role.name == "Uploader" for role in message.author.roles):
30 + url, path = message.content[8:].split(" ", 1)
31 + opts = {}
32 + if path[-2:] == " v":
33 + opts["format"] = "bestvideo"
34 + opts["outtmpl"] = os.path.join("/var/www/html/", "vid", path[:-2], "%(title)s.%(ext)s")
35 + else:
36 + opts["postprocessors"] = [{
37 + 'key': 'FFmpegExtractAudio',
38 + 'preferredcodec': "mp3",
39 + 'preferredquality': "5",
40 + 'nopostoverwrites': False,
41 + }]
42 + opts["outtmpl"] = os.path.join("/var/www/html/", path, "%(title)s.%(ext)s")
43 + if not url or not path:
44 + await client.send_message(message.channel, "Usage: !upload <url> <path>")
45 + return
46 + resp = await client.send_message(message.channel, "On it, just a second")
47 + await client.send_typing(message.channel)
48 + with youtube_dl.YoutubeDL(opts) as ydl:
49 + ydl.download([url])
50 + await client.edit_message(resp, "Job done!")
51 +
35 52 if message.content.startswith("!search "):
36 53 with aiohttp.ClientSession() as session:
37 54 async with session.get("http://tallyall.club/search.php", params={"q": message.content[8:], "json": ""}) as resp:
@@ -43,11 +60,11 @@ async def on_message(message):
43 60 lastresults = data["results"]
44 61 numbers = [":one:", ":two:", ":three:", ":four:", ":five:", ":six:", ":seven:", ":eight:"]
45 62 for n,r in enumerate(data["results"][:8]):
46 - out.append("{} {}".format(numbers[n], os.path.basename(r)))
63 + out.append("{} **{}** ({})".format(numbers[n], os.path.basename(r),os.path.dirname(r).split("/")[-1]))
47 64 out.append(":signal_strength: !play <number>")
48 - if len(data["results"]) > 8:
49 - out.append(":arrow_right: Full results at http://tallyall.club/search.php?" + urlencode({"q": message.content[8:]}))
65 + out.append(":arrow_right: Full results at http://tallyall.club/search.php?" + urlencode({"q": message.content[8:]}))
50 66 await client.send_message(message.channel, "\n".join(out))
67 +
51 68 if message.content.startswith("!play "):
52 69 if player:
53 70 player.stop()
@@ -64,20 +81,36 @@ async def on_message(message):
64 81 if voice:
65 82 await voice.disconnect()
66 83 voice = await client.join_voice_channel(client.get_channel("386339345914920980"))
67 - player = voice.create_ffmpeg_player(path, after=after)
84 + player = voice.create_ffmpeg_player(path)
68 85 player.start()
69 86 else:
70 87 await client.send_message(message.channel, "Invalid path")
71 - if message.content.startswith("!stop "):
88 +
89 + if message.content.startswith("!stop"):
72 90 if player:
73 91 player.stop()
74 92 voice = client.voice_client_in(message.server)
75 93 await voice.disconnect()
76 94
95 + if message.content.startswith("!link "):
96 + msg = message.content[6:]
97 + if msg.isdigit():
98 + if int(msg) <= len(lastresults) and int(msg) > 0:
99 + msg = lastresults[int(msg)-1]
100 + else:
101 + await client.send_message(message.channel, "Invalid result number")
102 + return
103 + path = "/var/www/html/" + unquote(msg.strip().replace("http://tallyall.club/", ""))
104 + if is_safe_path("/var/www/html", path) and os.path.isfile(path):
105 + with open(path, "rb") as f:
106 + await client.send_file(message.channel, f, filename=os.path.basename(path))
107 + else:
108 + await client.send_message(message.channel, "Something went wrong")
109 +
77 110 @client.event
78 111 async def on_ready():
79 112 print("Logged in as {} ({})".format(client.user.name, client.user.id))
80 - print(discord.utils.oauth_url(client.user.id, permissions=discord.Permissions(3072)))
113 + print(discord.utils.oauth_url(client.user.id, permissions=discord.Permissions(3197952)))
81 114
82 115 with open(".bottoken") as f:
83 116 creds = f.read()

Steven Smith revised this gist 1512181674. Go to revision

1 file changed, 87 insertions

searchbot.py(file created)

@@ -0,0 +1,87 @@
1 + #!/usr/bin/env python3
2 + import discord
3 + import asyncio
4 + import aiohttp
5 + from urllib.parse import quote_plus, urlencode
6 + import os.path
7 + from os import getcwd
8 + from urllib.parse import unquote
9 + from mutagen import File
10 + client = discord.Client()
11 +
12 + if not discord.opus.is_loaded():
13 + discord.opus.load_opus("libopus.so.0")
14 +
15 + def is_safe_path(basedir, path, follow_symlinks=True):
16 + # resolves symbolic links
17 + if follow_symlinks:
18 + return os.path.realpath(path).startswith(basedir)
19 + return os.path.abspath(path).startswith(basedir)
20 +
21 + lastresults = []
22 + player = None
23 +
24 + @client.event
25 + async def on_message(message):
26 + def after():
27 + coro = client.voice_client_in(message.server).disconnect()
28 + fut = asyncio.run_coroutine_threadsafe(coro, client.loop)
29 + try:
30 + fut.result()
31 + except:
32 + pass
33 + global lastresults
34 + global player
35 + if message.content.startswith("!search "):
36 + with aiohttp.ClientSession() as session:
37 + async with session.get("http://tallyall.club/search.php", params={"q": message.content[8:], "json": ""}) as resp:
38 + data = await resp.json()
39 + if len(data["results"]) < 1:
40 + await client.send_message(message.channel, ":sos: No results")
41 + return
42 + out = ["Search results:"]
43 + lastresults = data["results"]
44 + numbers = [":one:", ":two:", ":three:", ":four:", ":five:", ":six:", ":seven:", ":eight:"]
45 + for n,r in enumerate(data["results"][:8]):
46 + out.append("{} {}".format(numbers[n], os.path.basename(r)))
47 + out.append(":signal_strength: !play <number>")
48 + if len(data["results"]) > 8:
49 + out.append(":arrow_right: Full results at http://tallyall.club/search.php?" + urlencode({"q": message.content[8:]}))
50 + await client.send_message(message.channel, "\n".join(out))
51 + if message.content.startswith("!play "):
52 + if player:
53 + player.stop()
54 + msg = message.content[6:]
55 + if msg.isdigit():
56 + if int(msg) <= len(lastresults) and int(msg) > 0:
57 + msg = lastresults[int(msg)-1]
58 + else:
59 + await client.send_message(message.channel, "Invalid result number")
60 + return
61 + path = "/var/www/html/" + unquote(msg.strip().replace("http://tallyall.club/", ""))
62 + if is_safe_path("/var/www/html", path) and os.path.isfile(path) and File(path):
63 + voice = client.voice_client_in(message.server)
64 + if voice:
65 + await voice.disconnect()
66 + voice = await client.join_voice_channel(client.get_channel("386339345914920980"))
67 + player = voice.create_ffmpeg_player(path, after=after)
68 + player.start()
69 + else:
70 + await client.send_message(message.channel, "Invalid path")
71 + if message.content.startswith("!stop "):
72 + if player:
73 + player.stop()
74 + voice = client.voice_client_in(message.server)
75 + await voice.disconnect()
76 +
77 + @client.event
78 + async def on_ready():
79 + print("Logged in as {} ({})".format(client.user.name, client.user.id))
80 + print(discord.utils.oauth_url(client.user.id, permissions=discord.Permissions(3072)))
81 +
82 + with open(".bottoken") as f:
83 + creds = f.read()
84 + if " " in creds:
85 + client.run(*creds.split(None, 1))
86 + else:
87 + client.run(creds.strip())
Newer Older