From d63dc1bad433b231383c2091d84bccc7a973eb26 Mon Sep 17 00:00:00 2001
From: John Xina <bingchilling@riseup.net>
Date: Mon, 19 Feb 2024 22:10:48 +0800
Subject: [PATCH] Bump up the version of aiotieba to v4

---
 Dockerfile                             |  8 ------
 aiotieba-handle-exception-expose.patch | 40 --------------------------
 app.py                                 | 17 +++++++++--
 requirements.txt                       |  2 +-
 templates/user.html                    | 24 ++++++++--------
 5 files changed, 27 insertions(+), 64 deletions(-)
 delete mode 100644 aiotieba-handle-exception-expose.patch

diff --git a/Dockerfile b/Dockerfile
index 33820b7..1e00a69 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -21,14 +21,6 @@ RUN pip install -r requirements.txt
 COPY aiotieba-handle-exception-expose.patch .
 
 
-# Replace paths in the patch file to reflect the correct paths within the container
-RUN sed -i 's|venv/lib/python3.11/site-packages/|/usr/local/lib/python3.11/site-packages/|g' aiotieba-handle-exception-expose.patch
-
-
-# Apply the patch using the 'patch' command
-RUN patch -d/ -p0 < aiotieba-handle-exception-expose.patch
-
-
 # Expose port 8886
 EXPOSE 8886
 
diff --git a/aiotieba-handle-exception-expose.patch b/aiotieba-handle-exception-expose.patch
deleted file mode 100644
index fccda4b..0000000
--- a/aiotieba-handle-exception-expose.patch
+++ /dev/null
@@ -1,40 +0,0 @@
---- venv/lib/python3.11/site-packages/aiotieba/helper/utils.py
-+++ venv/lib/python3.11/site-packages/aiotieba/helper/utils.py
-@@ -141,35 +141,6 @@
- 
-     def wrapper(func):
-         async def awrapper(*args, **kwargs):
--            try:
--                ret = await func(*args, **kwargs)
--
--            except Exception as err:
--                meth_name = func.__name__
--                tb = err.__traceback__
--                while tb := tb.tb_next:
--                    frame = tb.tb_frame
--                    if frame.f_code.co_name == meth_name:
--                        break
--                frame = tb.tb_next.tb_frame
--
--                log_str: str = frame.f_locals.get('__log__', '')
--                if not no_format:  # need format
--                    log_str = log_str.format(**frame.f_locals)
--                log_str = f"{err}. {log_str}"
--
--                logger = get_logger()
--                if logger.isEnabledFor(log_level):
--                    record = logger.makeRecord(logger.name, log_level, None, 0, log_str, None, None, meth_name)
--                    logger.handle(record)
--
--                exc_handlers._handle(meth_name, err)
--
--                return null_ret_factory()
--
--            else:
--                return ret
--
-+            return await func(*args, **kwargs)
-         return awrapper
--
-     return wrapper
-
diff --git a/app.py b/app.py
index 02b09c0..6fee98f 100644
--- a/app.py
+++ b/app.py
@@ -132,7 +132,7 @@ async def _jinja2_filter_translate(frags, reply_id=0):
             if frag.is_external:
                 markup += 'style="text-color: #ff0000;" '
             else:
-                url = frag.raw_url.lstrip('https://tieba.baidu.com')
+                url = frag.raw_url.path
             markup += f'href="{ url }">{ frag.title }</a>'
             htmlfmt = append_with_leading_clean(htmlfmt, markup)
         elif isinstance(frag, FragAt):
@@ -162,6 +162,9 @@ async def thread_view(tid):
                                             with_comments=should_fetch_comments,
                                             only_thread_author=ao)
 
+        if thread_info.err:
+            return await runtime_error_view(thread_info.err)
+
         available_users = []
         for floor in thread_info:
             for comment in floor.comments:
@@ -188,6 +191,12 @@ async def forum_view():
     async with aiotieba.Client() as tieba:
         forum_info, threads = await asyncio.gather(tieba.get_forum_detail(fname),
                                                    tieba.get_threads(fname, pn=pn, sort=sort))
+        if threads.err:
+            return await runtime_error_view(threads.err)
+        elif forum_info.err:
+            return await runtime_error_view(forum_info.err)
+
+
         if hasattr(forum_info, 'slogan'):
             forum_info = { 'avatar': extract_image_name(forum_info.origin_avatar),
                            'topic': forum_info.post_num, 'thread': forum_info.post_num,
@@ -217,10 +226,12 @@ async def user_view():
     async with aiotieba.Client() as tieba:
         try:
             hp = await tieba.get_homepage(i, pn)
+            if hp.err:
+                return await runtime_error_view(hp.err)
         except ValueError:
             return await render_template_c('error.html', msg='您已超过最后页')
 
-    if len(hp[1]) == 0 and pn > 1:
+    if not hp.objs and pn > 1:
         return await render_template_c('error.html', msg='您已超过最后页')
     
     return await render_template_c('user.html', hp=hp, pn=pn)
@@ -235,7 +246,7 @@ async def main_view():
 async def runtime_error_view(e):
     if hasattr(e, 'msg'):
         return await render_template_c('error.html', msg=e.msg)
-    return await render_template_c('error.html', msg='错误信息不可用')
+    return await render_template_c('error.html', msg=str(e) or '错误信息不可用')
 
 @app.errorhandler(Exception)
 async def general_error_view(e):
diff --git a/requirements.txt b/requirements.txt
index cd37921..405d31d 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,7 +1,7 @@
 aioflask
 flask==2.1.3
 Werkzeug==2.2.2
-aiotieba
+aiotieba>=4.0.0,<5.0.0
 aiohttp
 uvicorn[standard]
 gunicorn
diff --git a/templates/user.html b/templates/user.html
index 31ca312..3663d11 100644
--- a/templates/user.html
+++ b/templates/user.html
@@ -1,6 +1,6 @@
 <!DOCTYPE html>
 <head>
-  <title>{{ hp[0].show_name }}的个人资料 - RAT</title>
+  <title>{{ hp.user.show_name }}的个人资料 - RAT</title>
 
   <meta charset="utf-8">
   <meta name="viewport" content="width=device-width, initial-scale=1">
@@ -20,20 +20,20 @@
 
 <body>
   <header class="bar-nav">
-    <img src="/proxy/avatar/{{ hp[0].portrait }}"></nav>
+    <img src="/proxy/avatar/{{ hp.user.portrait }}"></nav>
 <div>
-  <div class="title">{{ hp[0].show_name }} <small>{{ hp[0].user_name }}</small></div>
-  <div class="description">{{ hp[0].sign }}</div>
+  <div class="title">{{ hp.user.show_name }} <small>{{ hp.user.user_name }}</small></div>
+  <div class="description">{{ hp.user.sign }}</div>
   <div class="stats">
-    <small>关注数: {{ hp[0].follow_num|intsep }}</small>
-    <small>粉丝数: {{ hp[0].fan_num|intsep }}</small>
-    <small>发帖数: {{ hp[0].post_num|intsep }}</small>
-    <small>关注贴吧数: {{ hp[0].forum_num|intsep }}</small>
+    <small>关注数: {{ hp.user.follow_num|intsep }}</small>
+    <small>粉丝数: {{ hp.user.fan_num|intsep }}</small>
+    <small>发帖数: {{ hp.user.post_num|intsep }}</small>
+    <small>关注贴吧数: {{ hp.user.forum_num|intsep }}</small>
   </div>
 </div>
 </header>
 <div class="list">
-  {% for t in hp[1] %}
+  {% for t in hp.objs %}
   <div class="thread">
     <div class="summary">
       <a href="/p/{{ t.tid }}">{{ t.text }} </a>
@@ -43,18 +43,18 @@
 </div>
   <div class="paginator">
     {% if pn > 1 %}
-    <a href="/home/main?id={{ hp[0].user_id }}&pn={{ 1 }}">首页</a>
+    <a href="/home/main?id={{ hp.user.user_id }}&pn={{ 1 }}">首页</a>
     {% endif %}
     
     {% for i in range(5) %}
     {% set np = pn - 5 + i %}
     {% if np > 0 %}
-    <a href="/home/main?id={{ hp[0].user_id }}&pn={{ np }}">{{ np }}</a>
+    <a href="/home/main?id={{ hp.user.user_id }}&pn={{ np }}">{{ np }}</a>
     {% endif %}
     {% endfor %}
     
     <a>{{ pn }}</a>
-    <a href="/home/main?id={{ hp[0].user_id }}&pn={{ pn+1 }}">下一页</a>
+    <a href="/home/main?id={{ hp.user.user_id }}&pn={{ pn+1 }}">下一页</a>
   </div>
 <footer>
   <div><a href="/">RAT Ain't Tieba</a></div>
-- 
GitLab