Skip to content

Commit

Permalink
feat: add logging error sentry, version, fix torrent upload
Browse files Browse the repository at this point in the history
  • Loading branch information
verdel committed Feb 16, 2024
1 parent 57b7602 commit a70d634
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 11 deletions.
4 changes: 4 additions & 0 deletions conf/config.sample.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,7 @@ db:
schedule:
check_period: 10
max_instances: 1

sentry:
dsn: https://[email protected]/4504037633556480
environment: testing
47 changes: 46 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ python-telegram-bot = {extras = ["socks", "job-queue"], version = "^20.4"}
PyYAML = "^6.0.1"
aiosqlite = "^0.19.0"
transmission-rpc = "^7.0.3"

sentry-sdk = "^1.40.4"

[tool.poetry.group.dev.dependencies]
black = "^24.0.0"
Expand All @@ -30,7 +30,7 @@ line-length = 120
select = ["F", "E", "W", "C90",
"I", "N", "S", "B", "A",
"ISC", "T20", "Q", "PTH"]
ignore = ["A003"]
ignore = ["A003", "C901", "E501"]


[tool.black]
Expand All @@ -39,4 +39,6 @@ line-length = 120

[tool.pyright]
reportUnnecessaryTypeIgnoreComment="warning"
exclude = ["docker"]
exclude = ["docker"]

[tool.poetry_bumpversion.file."transmission_telegram_bot/_version.py"]
1 change: 1 addition & 0 deletions transmission_telegram_bot/_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = "0.1.2"
18 changes: 13 additions & 5 deletions transmission_telegram_bot/bot.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import argparse
import sys
import traceback
from base64 import b64encode
from base64 import b64decode, b64encode
from functools import wraps
from pathlib import Path
from textwrap import dedent
from typing import Coroutine

import sentry_sdk
from emoji import emojize
from telegram import (
InlineKeyboardButton,
Expand All @@ -23,6 +24,7 @@
)

import transmission_telegram_bot.tools as tools
from transmission_telegram_bot import _version
from transmission_telegram_bot.db import DB
from transmission_telegram_bot.transmission import Transmission

Expand Down Expand Up @@ -147,7 +149,7 @@ async def download_torrent_logic(update, context):
else:
try:
result = transmission.add_torrent(
torrent_data=context.user_data["torrent_data"],
torrent_data=b64decode(context.user_data["torrent_data"]),
download_dir=callback_data,
)
except Exception:
Expand Down Expand Up @@ -394,9 +396,7 @@ async def error_action(update, context):
trace = "".join(traceback.format_tb(sys.exc_info()[2]))
payload = ""
if update.effective_user:
payload += f" with the user {(update.effective_user.id, update.effective_user.first_name)}"
if update.effective_chat:
payload += f" within the chat <i>{update.effective_chat.title}</i>"
payload += f" with the user {(update.effective_user.id, update.effective_user.first_name, update.effective_user.last_name)}"
if update.effective_chat.username:
payload += f" (@{update.effective_chat.username})"
text = f"The error happened{payload}. The full traceback:\n\n{trace}"
Expand Down Expand Up @@ -487,6 +487,14 @@ def main():
logger.error(f"Config file error: {exc}")
sys.exit(1)

if "sentry" in cfg:
sentry_sdk.init(
dsn=cfg["sentry"]["dsn"],
environment=cfg["sentry"]["environment"],
release=_version.__version__,
attach_stacktrace=True,
)

try:
transmission = Transmission(
address=cfg["transmission"]["address"],
Expand Down
5 changes: 3 additions & 2 deletions transmission_telegram_bot/transmission.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ def remove_torrent(self, torrent_id: int, delete_data: bool = True):
"""Remove torrent by torrent id"""
return self.tc.remove_torrent(ids=torrent_id, delete_data=delete_data)

def add_torrent(self, torrent_data: str = "", **kwargs) -> Torrent:
def add_torrent(self, torrent_data: bytes, **kwargs) -> Torrent:
"""Add torrent to transmission"""
if "download_dir" in kwargs:
return self.tc.add_torrent(torrent=torrent_data, download_dir=kwargs.get("download_dir"))
torrent = self.tc.add_torrent(torrent=torrent_data, download_dir=kwargs.get("download_dir"))
return torrent
else:
return self.tc.add_torrent(torrent=torrent_data)

0 comments on commit a70d634

Please sign in to comment.