1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-23 19:44:20 +00:00

Bugfixes, improvements, experimental pt support

This commit is contained in:
Steffo 2019-12-17 20:11:05 +01:00
parent aa33e24a9d
commit 031f6e1d03
12 changed files with 132 additions and 188 deletions

1
.gitignore vendored
View file

@ -28,4 +28,5 @@ dist/
.idea/**/markdown-navigator.xml
.idea/**/markdown-exported-files.xml
.idea/**/misc.xml
.idea/**/discord.xml
.idea/**/*.iml

View file

@ -23,7 +23,7 @@ from .emojify import EmojifyCommand
from .leagueoflegends import LeagueoflegendsCommand
from .diarioquote import DiarioquoteCommand
# from .mp3 import Mp3Command
from .peertube import PeertubeCommand
from .peertubeupdates import PeertubeUpdatesCommand
from .googlevideo import GooglevideoCommand
from .yahoovideo import YahoovideoCommand
from .userinfo import UserinfoCommand
@ -58,7 +58,7 @@ available_commands = [
LeagueoflegendsCommand,
DiarioquoteCommand,
# Mp3Command,
PeertubeCommand,
PeertubeUpdatesCommand,
GooglevideoCommand,
YahoovideoCommand,
UserinfoCommand,

View file

@ -41,7 +41,7 @@ class EatCommand(Command):
# Sezione delle supercazzole
"antani": "❔ Hai mangiato {food}. \n[i]Con tarapia tapioco o scherziamo? No, mi permetta. Noi siamo in 4.\n"
"Come se fosse antani anche per lei soltanto in due, oppure in quattro anche scribàcchi confaldina?\n"
"Come antifurto, per esempio.[/i]"
"Come antifurto, per esempio.[/i]",
"indice": "☝️ Hai mangiato l'{food}. \n[i]Ecco, lo alzi. Lo vede, lo vede che stuzzica?[/i]",
# sezione con piante e anmali

View file

@ -10,6 +10,7 @@ class GooglevideoCommand(PlayCommand):
syntax = "{ricerca}"
_URL_FORMAT = "gvsearch:{url}"
async def get_url(self, args):
return f"gvsearch:{args.joined()}"
# Too bad gvsearch: always finds nothing.

View file

@ -1,75 +1,24 @@
import aiohttp
import asyncio
import datetime
import logging
import dateparser
from .play import PlayCommand
from royalnet.commands import *
from royalnet.serf.telegram.escape import escape
import aiohttp
import urllib.parse
log = logging.getLogger(__name__)
class PeertubeCommand(Command):
class PeertubeCommand(PlayCommand):
name: str = "peertube"
description: str = "Guarda quando è uscito l'ultimo video su RoyalTube."
aliases = ["pt", "royaltube", "rt"]
_ready = asyncio.Event()
description: str = "Cerca un video su RoyalTube e lo aggiunge alla coda della chat vocale."
_latest_date: datetime.datetime = None
syntax = "{ricerca}"
def __init__(self, interface: CommandInterface):
super().__init__(interface)
if self.interface.name == "telegram":
self.loop.create_task(self._ready_up())
self.loop.create_task(self._update())
async def _get_json(self):
log.debug("Getting jsonfeed")
async def get_url(self, args):
search = urllib.parse.quote(args.joined(require_at_least=1))
async with aiohttp.ClientSession() as session:
async with session.get(self.config["Peertube"]["feed_url"]) as response:
log.debug("Parsing jsonfeed")
async with session.get(self.config["Peertube"]["instance_url"] +
f"/api/v1/search/videos?search={search}") as response:
j = await response.json()
log.debug("Jsonfeed parsed successfully")
return j
async def _send(self, message):
client = self.interface.bot.client
await self.interface.bot.safe_api_call(client.send_message,
chat_id=self.config["Telegram"]["main_group_id"],
text=escape(message),
parse_mode="HTML",
disable_webpage_preview=True)
async def _ready_up(self):
j = await self._get_json()
if j["version"] != "https://jsonfeed.org/version/1":
raise ConfigurationError("url is not a jsonfeed")
videos = j["items"]
for video in reversed(videos):
date_modified = dateparser.parse(video["date_modified"])
if self._latest_date is None or date_modified > self._latest_date:
log.debug(f"Found newer video: {date_modified}")
self._latest_date = date_modified
self._ready.set()
async def _update(self):
await self._ready.wait()
while True:
j = await self._get_json()
videos = j["items"]
for video in reversed(videos):
date_modified = dateparser.parse(video["date_modified"])
if date_modified > self._latest_date:
log.debug(f"Found newer video: {date_modified}")
self._latest_date = date_modified
await self._send(f"🆕 Nuovo video su RoyalTube!\n"
f"[b]{video['title']}[/b]\n"
f"{video['url']}")
await asyncio.sleep(self.config["Peertube"]["feed_update_timeout"])
async def run(self, args: CommandArgs, data: CommandData) -> None:
if self.interface.name != "telegram":
raise UnsupportedError()
await data.reply(f" Ultimo video caricato il: [b]{self._latest_date.isoformat()}[/b]")
if j["total"] < 1:
raise InvalidInputError("Nessun video trovato.")
return f'{self.config["Peertube"]["instance_url"]}/videos/watch/{j["data"][0]["uuid"]}'

View file

@ -0,0 +1,78 @@
import aiohttp
import asyncio
import datetime
import logging
import dateparser
from royalnet.commands import *
from royalnet.serf.telegram.escape import escape
log = logging.getLogger(__name__)
class PeertubeUpdatesCommand(Command):
name: str = "peertubeupdates"
description: str = "Guarda quando è uscito l'ultimo video su PeerTube."
aliases = ["ptu"]
_ready = asyncio.Event()
_latest_date: datetime.datetime = None
def __init__(self, interface: CommandInterface):
super().__init__(interface)
if self.interface.name == "telegram":
self.loop.create_task(self._ready_up())
self.loop.create_task(self._update())
async def _get_json(self):
log.debug("Getting jsonfeed")
async with aiohttp.ClientSession() as session:
async with session.get(self.config["Peertube"]["instance_url"] +
"/feeds/videos.json?sort=-publishedAt&filter=local") as response:
log.debug("Parsing jsonfeed")
j = await response.json()
log.debug("Jsonfeed parsed successfully")
return j
async def _send(self, message):
client = self.interface.bot.client
await self.interface.bot.safe_api_call(client.send_message,
chat_id=self.config["Telegram"]["main_group_id"],
text=escape(message),
parse_mode="HTML",
disable_webpage_preview=True)
async def _ready_up(self):
j = await self._get_json()
if j["version"] != "https://jsonfeed.org/version/1":
raise ConfigurationError("url is not a jsonfeed")
videos = j["items"]
for video in reversed(videos):
date_modified = dateparser.parse(video["date_modified"])
if self._latest_date is None or date_modified > self._latest_date:
log.debug(f"Found newer video: {date_modified}")
self._latest_date = date_modified
self._ready.set()
async def _update(self):
await self._ready.wait()
while True:
j = await self._get_json()
videos = j["items"]
for video in reversed(videos):
date_modified = dateparser.parse(video["date_modified"])
if date_modified > self._latest_date:
log.debug(f"Found newer video: {date_modified}")
self._latest_date = date_modified
await self._send(f"🆕 Nuovo video su RoyalTube!\n"
f"[b]{video['title']}[/b]\n"
f"{video['url']}")
await asyncio.sleep(self.config["Peertube"]["feed_update_timeout"])
async def run(self, args: CommandArgs, data: CommandData) -> None:
if self.interface.name != "telegram":
raise UnsupportedError()
await data.reply(f" Ultimo video caricato il: [b]{self._latest_date.isoformat()}[/b]")

View file

@ -15,10 +15,10 @@ class PlayCommand(Command):
syntax = "{url}"
_URL_FORMAT = "{url}"
async def get_url(self, args: CommandArgs):
return args.joined()
async def run(self, args: CommandArgs, data: CommandData) -> None:
url = args.joined()
# if not (url.startswith("http://") or url.startswith("https://")):
# raise CommandError(f"Il comando [c]{self.interface.prefix}play[/c] funziona solo per riprodurre file da"
# f" un URL.\n"
@ -27,11 +27,14 @@ class PlayCommand(Command):
if self.interface.name == "discord":
message: discord.Message = data.message
guild: discord.Guild = message.guild
if guild is None:
guild_id = None
else:
guild_id: Optional[int] = guild.id
else:
guild_id = None
response: Dict[str, Any] = await self.interface.call_herald_event("discord", "discord_play",
url=self._URL_FORMAT.format(url=url),
url=await self.get_url(args),
guild_id=guild_id)
too_long: List[Dict[str, Any]] = response["too_long"]

View file

@ -76,7 +76,7 @@ class ReminderCommand(Command):
else:
raise UnsupportedError("This command does not support the current interface.")
creator = await data.get_author()
reminder = self.interface.alchemy.Reminder(creator=creator,
reminder = self.interface.alchemy.get(Reminder)(creator=creator,
interface_name=self.interface.name,
interface_data=interface_data,
datetime=date,

View file

@ -10,4 +10,5 @@ class SoundcloudCommand(PlayCommand):
syntax = "{ricerca}"
_URL_FORMAT = "scsearch:{url}"
async def get_url(self, args):
return f"scsearch:{args.joined()}"

View file

@ -10,6 +10,7 @@ class YahoovideoCommand(PlayCommand):
syntax = "{ricerca}"
_URL_FORMAT = "yvsearch:{url}"
async def get_url(self, args):
return f"yvsearch:{args.joined()}"
# Too bad yvsearch: always finds nothing.

View file

@ -10,4 +10,5 @@ class YoutubeCommand(PlayCommand):
syntax = "{ricerca}"
_URL_FORMAT = "ytsearch:{url}"
async def get_url(self, args):
return f"ytsearch:{args.joined()}"

View file

@ -1,91 +0,0 @@
import typing
import discord
import asyncio
import datetime
from royalnet.commands import *
from royalnet.utils import asyncify
from royalnet.audio import YtdlDiscord
from royalnet.audio.playmodes import Playlist
from royalnet.bots import DiscordBot
class ZawarudoCommand(Command):
name: str = "zawarudo"
aliases = ["theworld", "world"]
description: str = "Ferma il tempo!"
syntax = "[ [guild] ] [1-9]"
@staticmethod
async def _legacy_zawarudo_handler(bot: "DiscordBot", guild_name: typing.Optional[str], time: int):
# Find the matching guild
if guild_name:
guilds: typing.List[discord.Guild] = bot.client.find_guild_by_name(guild_name)
else:
guilds = bot.client.guilds
if len(guilds) == 0:
raise CommandError("Server non trovato.")
if len(guilds) > 1:
raise CommandError("Il nome del server è ambiguo.")
guild = list(bot.client.guilds)[0]
# Ensure the guild has a PlayMode before adding the file to it
if not bot.music_data.get(guild):
raise CommandError("Il bot non è in nessun canale vocale.")
# Create url
ytdl_args = {
"format": "bestaudio",
"outtmpl": f"./downloads/{datetime.datetime.now().timestamp()}_%(title)s.%(ext)s"
}
# Start downloading
zw_start: typing.List[YtdlDiscord] = await asyncify(YtdlDiscord.create_from_url,
"https://scaleway.steffo.eu/jojo/zawarudo_intro.mp3",
**ytdl_args)
zw_end: typing.List[YtdlDiscord] = await asyncify(YtdlDiscord.create_from_url,
"https://scaleway.steffo.eu/jojo/zawarudo_outro.mp3",
**ytdl_args)
old_playlist = bot.music_data[guild]
bot.music_data[guild].playmode = Playlist()
# Get voice client
vc: discord.VoiceClient = bot.client.find_voice_client_by_guild(guild)
channel: discord.VoiceChannel = vc.channel
affected: typing.List[typing.Union[discord.User, discord.Member]] = channel.members
await bot.add_to_music_data(zw_start, guild)
for member in affected:
if member.bot:
continue
await member.edit(mute=True)
await asyncio.sleep(time)
await bot.add_to_music_data(zw_end, guild)
for member in affected:
member: typing.Union[discord.User, discord.Member]
if member.bot:
continue
await member.edit(mute=False)
bot.music_data[guild] = old_playlist
await bot.advance_music_data(guild)
return {}
_event_name = "_legacy_zawarudo"
def __init__(self, interface: CommandInterface):
super().__init__(interface)
if interface.name == "discord":
interface.register_herald_action(self._event_name, self._legacy_zawarudo_handler)
async def run(self, args: CommandArgs, data: CommandData) -> None:
guild_name, time = args.match(r"(?:\[(.+)])?\s*(.+)?")
if time is None:
time = 5
else:
time = int(time)
if time < 1:
raise InvalidInputError("The World can't stop time for less than a second.")
if time > 10:
raise InvalidInputError("The World can stop time only for 10 seconds.")
await data.reply(f"🕒 ZA WARUDO! TOKI WO TOMARE!")
await self.interface.call_herald_action("discord", self._event_name, {
"guild_name": guild_name,
"time": time
})