Adding r34 bot
This commit is contained in:
7
r34/maubot.yaml
Normal file
7
r34/maubot.yaml
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
maubot: 0.1.0
|
||||||
|
id: nearfuture.maubot.r34
|
||||||
|
version: 0.1.0
|
||||||
|
modules:
|
||||||
|
- r34bot
|
||||||
|
main_class: R34Bot
|
||||||
|
config: false
|
||||||
36
r34/r34bot.py
Normal file
36
r34/r34bot.py
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
import json
|
||||||
|
import random
|
||||||
|
|
||||||
|
from typing import Type, Optional
|
||||||
|
from mautrix.util.config import BaseProxyConfig, ConfigUpdateHelper
|
||||||
|
from maubot import Plugin, MessageEvent
|
||||||
|
from maubot.handlers import command
|
||||||
|
|
||||||
|
from urllib.parse import urlparse, parse_qs
|
||||||
|
|
||||||
|
class R34Bot(Plugin):
|
||||||
|
|
||||||
|
@command.new("r34")
|
||||||
|
@command.argument("pattern", pass_raw=True, required=True)
|
||||||
|
async def r34(self, evt: MessageEvent, pattern: Optional[str]) -> None:
|
||||||
|
if (pattern):
|
||||||
|
tags=pattern.replace(' ','+')
|
||||||
|
api_url='https://api.rule34.xxx/index.php?page=dapi&s=post&q=index&tags='+tags+"&limit=100&json=1"
|
||||||
|
|
||||||
|
async with self.http.get(api_url) as resp:
|
||||||
|
response_json = await resp.json()
|
||||||
|
self.log.debug(str(response_json))
|
||||||
|
results = []
|
||||||
|
for item in response_json:
|
||||||
|
results.append(item['id'])
|
||||||
|
self.log.debug(str(results))
|
||||||
|
random_result = random.choice(results)
|
||||||
|
url = 'https://rule34.xxx/index.php?page=post&s=view&id='+str(random_result)
|
||||||
|
await evt.respond(url)
|
||||||
|
|
||||||
|
else:
|
||||||
|
api_url='https://rule34.xxx/index.php?page=post&s=random'
|
||||||
|
async with self.http.get(api_url) as resp:
|
||||||
|
self.log.debug(str(resp))
|
||||||
|
parsed = urlparse(str(resp.url))
|
||||||
|
await evt.respond('https://rule34.xxx/index.php?'+str(parsed.query))
|
||||||
Reference in New Issue
Block a user