Skip to content
This repository has been archived by the owner on Sep 30, 2022. It is now read-only.

Add dogbin module #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 133 additions & 0 deletions tg_bot/modules/dogbin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import html
import json
import random

from typing import Optional, List

import requests
from telegram import Message, Chat, Update, Bot, MessageEntity
from telegram import ParseMode
from telegram.ext import CommandHandler, run_async, Filters

from tg_bot import dispatcher
from tg_bot.modules.disable import DisableAbleCommandHandler

BASE_URL = 'https://del.dog'

@run_async
def paste(bot: Bot, update: Update, args: List[str]):
message = update.effective_message

if message.reply_to_message:
data = message.reply_to_message.text
elif len(args) >= 1:
data = message.text.split(None, 1)[1]
else:
message.reply_text("What am I supposed to do with this?!")
return

r = requests.post(f'{BASE_URL}/documents', data=data)

if r.status_code == 404:
update.effective_message.reply_text('Failed to reach dogbin')
r.raise_for_status()

res = r.json()

if r.status_code != 200:
update.effective_message.reply_text(res['message'])
r.raise_for_status()

key = res['key']
if res['isUrl']:
reply = f'Shortened URL: {BASE_URL}/{key}\nYou can view stats, etc. [here]({BASE_URL}/v/{key})'
else:
reply = f'{BASE_URL}/{key}'
update.effective_message.reply_text(reply, parse_mode=ParseMode.MARKDOWN)

@run_async
def get_paste_content(bot: Bot, update: Update, args: List[str]):
message = update.effective_message

if len(args) >= 1:
key = args[0]
else:
message.reply_text("Please supply a paste key!")
return

format_normal = f'{BASE_URL}/'
format_view = f'{BASE_URL}/v/'

if key.startswith(format_view):
key = key[len(format_view):]
elif key.startswith(format_normal):
key = key[len(format_normal):]

r = requests.get(f'{BASE_URL}/raw/{key}')

if r.status_code != 200:
try:
res = r.json()
update.effective_message.reply_text(res['message'])
except Exception:
if r.status_code == 404:
update.effective_message.reply_text('Failed to reach dogbin')
else:
update.effective_message.reply_text('Unknown error occured')
r.raise_for_status()

update.effective_message.reply_text(r.text)

@run_async
def get_paste_stats(bot: Bot, update: Update, args: List[str]):
message = update.effective_message

if len(args) >= 1:
key = args[0]
else:
message.reply_text("Please supply a paste key!")
return

format_normal = f'{BASE_URL}/'
format_view = f'{BASE_URL}/v/'

if key.startswith(format_view):
key = key[len(format_view):]
elif key.startswith(format_normal):
key = key[len(format_normal):]

r = requests.get(f'{BASE_URL}/documents/{key}')

if r.status_code != 200:
try:
res = r.json()
update.effective_message.reply_text(res['message'])
except Exception:
if r.status_code == 404:
update.effective_message.reply_text('Failed to reach dogbin')
else:
update.effective_message.reply_text('Unknown error occured')
r.raise_for_status()

document = r.json()['document']
key = document['_id']
views = document['viewCount']
reply = f'Stats for **[/{key}]({BASE_URL}/{key})**:\nViews: `{views}`'
update.effective_message.reply_text(reply, parse_mode=ParseMode.MARKDOWN)


__help__ = """
- /paste: Create a paste or a shortened url using [dogbin](https://del.dog)
- /getpaste: Get the content of a paste or shortened url from [dogbin](https://del.dog)
- /pastestats: Get stats of a paste or shortened url from [dogbin](https://del.dog)
"""

__mod_name__ = "dogbin"

PASTE_HANDLER = DisableAbleCommandHandler("paste", paste, pass_args=True)
GET_PASTE_HANDLER = DisableAbleCommandHandler("getpaste", get_paste_content, pass_args=True)
PASTE_STATS_HANDLER = DisableAbleCommandHandler("pastestats", get_paste_stats, pass_args=True)

dispatcher.add_handler(PASTE_HANDLER)
dispatcher.add_handler(GET_PASTE_HANDLER)
dispatcher.add_handler(PASTE_STATS_HANDLER)