-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
152 lines (118 loc) · 4.37 KB
/
bot.py
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import os
import subprocess
import sys
import time
import urllib.request
from pathlib import Path
from shutil import which
from urllib.parse import urlsplit, urlunsplit
import pickledb
import discord
import requests
import spaw
from discord.ext import commands, tasks
from dotenv import load_dotenv
from pystreamable import StreamableApi
from urlextract import URLExtract
load_dotenv(dotenv_path=Path('.')/'.env')
DISCORD_TOKEN = os.getenv('DISCORD_TOKEN')
S_USER = os.getenv('S_USER')
S_PASS = os.getenv('S_PASS')
spaw_obj = spaw.SPAW()
bot = commands.Bot(command_prefix='!', intents=discord.Intents.default())
streamable_api = StreamableApi(S_USER, S_PASS)
spaw_obj.auth(S_USER, S_PASS)
db = pickledb.load("urls.db", True)
urls = []
@bot.event
async def on_ready():
"""Event when the bot logs in"""
await bot.change_presence(activity=discord.Streaming(name="by rush2sk8", url='https://www.twitch.tv/rush2sk8'))
print('We have logged in as {0.user}'.format(bot))
@tasks.loop(seconds=1)
async def run_upload():
if len(urls) > 0:
message, url, site = urls.pop(0)
if(site == "twitch"):
await twitch(message, url)
elif(site == "reddit"):
await reddit(message, url)
async def twitch(message, url):
db_query = db.get(url)
if db_query == False:
try:
return_code = spaw_obj.videoImport(url)
shortcode = return_code["shortcode"]
while streamable_api.get_info(shortcode)["percent"] != 100:
time.sleep(0.5)
db.set(url, shortcode)
await message.channel.send(f'https://streamable.com/{shortcode}')
except Exception as e:
print(e)
pass
else:
await message.channel.send(f'https://streamable.com/{db_query}')
async def reddit(message, url):
url = f'{urlunsplit(urlsplit(url)._replace(query="", fragment=""))[:-1]}.json'
db_query = db.get(url)
if db_query == False:
try:
json = requests.get(
url, headers={'User-agent': 'reddit-discordbot'}).json()
video_url = json[0]["data"]["children"][0]["data"]["secure_media"]["reddit_video"]["fallback_url"]
video_title = json[0]["data"]["children"][0]["data"]["title"]
l = video_url.split("DASH")[0]
r = video_url.split("?")[-1]
audio_url = f'{l}DASH_audio.mp4?{r}'
final_video_name = "video.mp4"
urllib.request.urlretrieve(
video_url, filename=final_video_name)
try:
urllib.request.urlretrieve(audio_url, filename="audio.mp4")
subprocess.call(
"ffmpeg -y -hide_banner -loglevel error -i video.mp4 -i audio.mp4 -c:v copy -c:a aac output.mp4",
shell=True
)
final_video_name = "output.mp4"
os.remove("audio.mp4")
except Exception:
pass
try:
os.rename(final_video_name, f'{video_title}.mp4')
except Exception as e:
print(e)
upload_status = spaw_obj.videoUpload(f'{video_title}.mp4')
shortcode = upload_status["shortcode"]
while streamable_api.get_info(shortcode)["percent"] != 100:
time.sleep(1)
db.set(url, shortcode)
await message.channel.send(f'https://streamable.com/{shortcode} from: {message.author.mention}')
try:
os.remove(f'{video_title}.mp4')
os.remove("video.mp4")
os.remove("output.mp4")
except Exception as e:
print(e)
except Exception:
pass
else:
await message.channel.send(f'https://streamable.com/{db_query}')
@bot.event
async def on_message(message):
if message.author.id == bot.user.id or message.channel.is_nsfw():
return
m = message.content
extractor = URLExtract()
found = extractor.find_urls(m)
if len(found) > 0:
if "clips" in message.channel.name and \
("twitch" in found[0]) and \
("clip" in found[0]):
urls.append((message, found[0], "twitch"))
elif ("reddit" in found[0]):
urls.append((message, found[0], "reddit"))
if which("ffmpeg") is not None:
run_upload.start()
bot.run(DISCORD_TOKEN)
else:
sys.exit("Bro install ffmpeg")