1212from html import escape
1313from io import BytesIO as StringIO
1414from os import path as os_path
15- from urllib .parse import unquote , urlparse
15+ from urllib .parse import unquote , urlparse , urlsplit
1616from xml .etree import ElementTree as ET
1717
1818from itsdangerous import SignatureExpired , BadSignature
@@ -84,6 +84,38 @@ def dec(self, env, req, *args, **kwargs):
8484 return dec
8585
8686
87+ def get_comment_id_from_url (comment_url ):
88+ """
89+ Extracts the comment ID from a given comment URL.
90+
91+ Args:
92+ comment_url (str): The URL of the comment.
93+
94+ Returns:
95+ int or None: The extracted comment ID if successful, None otherwise.
96+ """
97+ try :
98+ # Parse the comment URL to extract the comment ID from the fragment
99+ parsed_url = urlsplit (comment_url )
100+ except ValueError :
101+ # Handle malformed URL
102+ return None
103+
104+ fragment = parsed_url .fragment
105+ if not fragment or '-' not in fragment :
106+ # Handle missing fragment or fragment without hyphen
107+ return None
108+
109+ last_element = fragment .split ('-' )[- 1 ]
110+ try :
111+ comment_id = int (last_element )
112+ except ValueError :
113+ # Handle invalid comment ID
114+ return None
115+
116+ return comment_id
117+
118+
87119class API (object ):
88120
89121 FIELDS = set (['id' , 'parent' , 'text' , 'author' , 'website' ,
@@ -1376,6 +1408,8 @@ def login(self, env, req):
13761408 Comment ordering
13771409 @apiQuery {Number{0,1}} [asc=0]
13781410 Ascending
1411+ @apiQuery {String} comment_url
1412+ Search comment by URL
13791413
13801414 @apiExample {curl} Listing of published comments:
13811415 curl 'https://comments.example.com/admin/?mode=1&page=0&order_by=modified&asc=1' -b cookie.txt
@@ -1396,10 +1430,17 @@ def admin(self, env, req):
13961430 order_by = req .args .get ('order_by' , 'created' )
13971431 asc = int (req .args .get ('asc' , 0 ))
13981432 mode = int (req .args .get ('mode' , 2 ))
1399- comments = self .comments .fetchall (mode = mode , page = page ,
1400- limit = page_size ,
1401- order_by = order_by ,
1402- asc = asc )
1433+ comment_url = req .args .get ('comment_url' , '' )
1434+
1435+ # Search for a specific comment by URL
1436+ if comment_url :
1437+ comment_id = get_comment_id_from_url (comment_url )
1438+ comments = self .comments .fetchall (comment_id = comment_id , limit = 1 ) if comment_id else []
1439+ else :
1440+ comments = self .comments .fetchall (mode = mode , page = page ,
1441+ limit = page_size ,
1442+ order_by = order_by ,
1443+ asc = asc )
14031444 comments_enriched = []
14041445 for comment in list (comments ):
14051446 comment ['hash' ] = self .isso .sign (comment ['id' ])
@@ -1411,6 +1452,7 @@ def admin(self, env, req):
14111452 conf = self .conf , max_page = max_page ,
14121453 counts = comment_mode_count ,
14131454 order_by = order_by , asc = asc ,
1455+ comment_url = comment_url ,
14141456 isso_host_script = isso_host_script )
14151457 """
14161458 @api {get} /latest latest
0 commit comments