first commit

This commit is contained in:
MrRaph_
2024-12-02 15:48:11 +01:00
parent 62415b4e91
commit cf37cd951d
4 changed files with 130 additions and 0 deletions

1
.gitignore vendored
View File

@@ -160,3 +160,4 @@ cython_debug/
# option (not recommended) you can uncomment the following to ignore the entire idea folder. # option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/ #.idea/
fr.mrraph.hoarder-bot-v0.0.2.mbp

3
base-config.yaml Normal file
View File

@@ -0,0 +1,3 @@
hoarder_url: "http://hoarder.local"
hoarder_api_key: "votre_cle_api"
command_prefix: hoarder

82
hoarder.py Normal file
View File

@@ -0,0 +1,82 @@
import re
import aiohttp
from typing import Type
from mautrix.util.config import BaseProxyConfig, ConfigUpdateHelper
from maubot import Plugin, MessageEvent
from maubot.handlers import event, command
from datetime import datetime
# Setup config file
class Config(BaseProxyConfig):
def do_update(self, helper: ConfigUpdateHelper) -> None:
helper.copy("hoarder_url")
helper.copy("hoarder_api_key")
helper.copy("command_prefix")
class HoarderForwarder(Plugin):
async def start(self) -> None:
await super().start()
self.config.load_and_update()
@classmethod
def get_config_class(cls) -> Type[BaseProxyConfig]:
return Config
async def get_page_title(self, url: str) -> str:
"""Récupère le titre de la page web à partir de l'URL sans utiliser BeautifulSoup."""
try:
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
if response.status == 200:
html = await response.text()
# Rechercher la balise <title>
match = re.search(r"<title>(.*?)</title>", html, re.IGNORECASE | re.DOTALL)
if match:
return match.group(1).strip()
else:
return "Sans titre"
else:
return "Titre introuvable"
except Exception as e:
print(f"Erreur lors de la récupération du titre: {e}")
return "Erreur"
@command.new(
"hoarder",
)
@command.argument("url", pass_raw=True, required=True)
async def send_to_hoarder(self, evt: MessageEvent, url: str) -> None:
await evt.mark_read()
hoarder_url = self.config["hoarder_url"] + '/api/trpc/bookmarks.createBookmark'
api_key = self.config["hoarder_api_key"]
self.log.info(hoarder_url)
#title = await self.get_page_title(url) # Récupère le titre de la page
if not hoarder_url or not api_key:
self.log.warning("L'URL ou la clé API de Hoarder n'est pas configurée.")
return
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json",
"Accept": "application/json",
}
payload = {
"json": {
"type": "link",
"url": url
}
}
try:
async with aiohttp.ClientSession() as session:
async with session.post(hoarder_url, json=payload, headers=headers) as response:
if response.status == 201:
self.log.info(f"URL envoyée avec succès à Hoarder : {url}")
else:
self.log.error(f"Échec de l'envoi de l'URL à Hoarder : {response.status}")
except Exception as e:
self.log.exception(f"Erreur lors de l'envoi de l'URL à Hoarder : {e}")

44
maubot.yaml Normal file
View File

@@ -0,0 +1,44 @@
# This is an example maubot plugin definition file.
# All plugins must include a file like this named "maubot.yaml" in their root directory.
# Target maubot version
maubot: 0.1.0
# The unique ID for the plugin. Java package naming style. (i.e. use your own domain, not xyz.maubot)
id: fr.mrraph.hoarder-bot
# A PEP 440 compliant version string.
version: 0.0.2
# The SPDX license identifier for the plugin. https://spdx.org/licenses/
# Optional, assumes all rights reserved if omitted.
license: MIT
# The list of modules to load from the plugin archive.
# Modules can be directories with an __init__.py file or simply python files.
# Submodules that are imported by modules listed here don't need to be listed separately.
# However, top-level modules must always be listed even if they're imported by other modules.
modules:
- hoarder
#- config
# The main class of the plugin. Format: module/Class
# If `module` is omitted, will default to last module specified in the module list.
# Even if `module` is not omitted here, it must be included in the modules list.
# The main class must extend maubot.Plugin
main_class: HoarderForwarder
# Whether or not instances need a database
database: false
# Extra files that the upcoming build tool should include in the mbp file.
config: true
extra_files:
- base-config.yaml
#- LICENSE
# List of dependencies
#dependencies:
#- config
#soft_dependencies:
#- bar>=0.1