Skip to content

Commit

Permalink
make SA handler embed post bodies when linked directly
Browse files Browse the repository at this point in the history
  • Loading branch information
rmmh committed Jun 28, 2020
1 parent 38e6941 commit 62c14d8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
34 changes: 29 additions & 5 deletions plugins/somethingawful.py
Expand Up @@ -5,7 +5,7 @@
from util import hook, http


SA_THREAD_RE = r"(?i)forums\.somethingawful\.com/\S*\?(?:\S+&)?threadid=(\d+)"
SA_THREAD_RE = r"(?i)forums\.somethingawful\.com/\S*\?(?:\S+&)?threadid=(\d+)\S*"
SA_PROFILE_RE = r"(?i)forums\.somethingawful\.com/member.php\?\S+(userid|username)=([^&]+)"

LOGIN_URL = "https://forums.somethingawful.com/account.php"
Expand Down Expand Up @@ -159,18 +159,25 @@ def parse_thread_html(document):
else:
post_count = 1

posts = {x.attrib['id']: (
x.xpath('.//dt[contains(@class, "author")]')[0].text_content(),
x.xpath('.//*[@class="postdate"]')[0].text_content().strip('\n #?'),
x.xpath('.//*[@class="postbody"]')[0].text_content().strip())
for x in document.xpath('//table[contains(@class, "post")]')}

return {
"id": thread_id,
"breadcrumbs": breadcrumbs,
"forum_title": forum_title,
"thread_title": thread_title,
"author": author,
"post_count": post_count,
"thread_link": http.prepare_url(THREAD_URL, {'threadid': thread_id})
"posts": posts,
"thread_link": http.prepare_url(THREAD_URL, {'threadid': thread_id}),
}


def get_thread_by_id(credentials, id):
def get_thread_by_id(credentials, id, params=None):
"""
Get thread data via the ID of the thread
Expand All @@ -184,7 +191,7 @@ def get_thread_by_id(credentials, id):
thread_document = http.get_html(
THREAD_URL,
cookies=True,
query_params={
query_params=params or {
"noseen": 1,
"threadid": id,
"perpage": 1
Expand Down Expand Up @@ -316,7 +323,14 @@ def thread_link(inp, api_key=None):
if not inp:
return

thread = get_thread_by_id(api_key, inp.group(1))
post = None
if '#post' in inp.group(0):
parsed = http.urlparse(inp.group(0))
thread = get_thread_by_id(api_key, inp.group(1), params=dict(http.parse_qsl(parsed.query)))
post = thread['posts'].get(parsed.fragment)
print(post)
else:
thread = get_thread_by_id(api_key, inp.group(1))

if not thread:
return
Expand All @@ -326,6 +340,16 @@ def thread_link(inp, api_key=None):

thread['post_count_word'] = 'posts' if thread['post_count'] > 1 else 'post'

if post:
author, date, content = post
content = re.sub(r'\n+', ' // ', content)
if len(content) > 400:
content = content[:400] + '...'
return (
"\x02{forum_title}\x02 > \x02{thread_title}\x02: "
"\x02{poster}\x02 on {date}: {content}"
).format(poster=author, date=date, content=content, **thread)

return (
"\x02{forum_title}\x02 > \x02{thread_title}\x02 by \x02{author}\x02, "
"\x02{post_count}\x02 {post_count_word}"
Expand Down
2 changes: 1 addition & 1 deletion plugins/util/http.py
Expand Up @@ -22,7 +22,7 @@
with hooks():
import urllib.request, urllib.parse, urllib.error

from urllib.parse import quote, unquote, urlencode, quote_plus as _quote_plus
from urllib.parse import quote, unquote, urlencode, urlparse, parse_qsl, quote_plus as _quote_plus
from urllib.error import HTTPError, URLError


Expand Down

0 comments on commit 62c14d8

Please sign in to comment.