Skip to content
Snippets Groups Projects
Commit 687840b2 authored by roadscape's avatar roadscape
Browse files

combine mention methods

parent e4e37aa3
No related branches found
No related tags found
No related merge requests found
"""Methods for normalizing steemd post metadata.""" """Methods for normalizing steemd post metadata."""
#pylint: disable=line-too-long #pylint: disable=line-too-long
import logging
import re import re
import math import math
import ujson as json import ujson as json
...@@ -8,11 +9,41 @@ from funcy.seqs import first, distinct ...@@ -8,11 +9,41 @@ from funcy.seqs import first, distinct
from hive.utils.normalize import sbd_amount, rep_log10, safe_img_url, parse_time, utc_timestamp from hive.utils.normalize import sbd_amount, rep_log10, safe_img_url, parse_time, utc_timestamp
def mentions(body): log = logging.getLogger(__name__)
def mentions(post):
"""Given a post, return proper @-mentioned account names."""
# pylint: disable=invalid-name
detected = text_mentions(post['body'])
provided = _post_users(post)
d1 = detected - provided
d2 = provided - detected
url = '@' + post['author'] + '/' + post['permlink']
if d1: log.warning("%s detected - provided: %s", url, d1)
if d2: log.warning("%s provided - detected: %s", url, d1)
return detected & provided
def text_mentions(body):
"""Given a post body, return proper @-mentioned account names.""" """Given a post body, return proper @-mentioned account names."""
matches = re.findall('(?:^|[^a-zA-Z0-9_!#$%&*@])(:?@)([a-z\\d\\-.]+)', body) matches = re.findall('(?:^|[^a-zA-Z0-9_!#$%&*@])(:?@)([a-z\\d\\-.]+)', body)
names = [grp[1] for grp in matches] return {grp[1] for grp in matches}
return list(dict.fromkeys(names)) # uniq-ordered
def _post_users(post):
"""Retrieve `users` key from json_metadata."""
md = {}
try:
md = json.loads(post['json_metadata'])
if not isinstance(md, dict):
md = {}
except Exception:
pass
if 'users' in md and isinstance(md['users'], list):
return set(md['users'])
return set()
def post_basic(post): def post_basic(post):
"""Basic post normalization: json-md, tags, and flags.""" """Basic post normalization: json-md, tags, and flags."""
......
...@@ -3,6 +3,7 @@ from decimal import Decimal ...@@ -3,6 +3,7 @@ from decimal import Decimal
from hive.utils.post import ( from hive.utils.post import (
mentions, mentions,
text_mentions,
post_basic, post_basic,
post_legacy, post_legacy,
post_payout, post_payout,
...@@ -138,12 +139,20 @@ POST_2 = { ...@@ -138,12 +139,20 @@ POST_2 = {
"vote_rshares": 0 "vote_rshares": 0
} }
def test_body_mentions(): def test_mentions():
assert mentions('Hi @abc, meet @bob') == ['abc', 'bob'] post = {'body': 'Who is @abc and @foo and @bar',
assert mentions('Hi @abc, meet @abc') == ['abc'] 'json_metadata': '{"users":["foo","bar"]}'}
assert mentions('') == [] assert mentions(post) == {'foo', 'bar'}
assert mentions('@') == []
assert mentions('steemit.com/@apple') == [] def test_text_mentions():
# pylint: disable=invalid-name
m = text_mentions
assert m('Hi @abc, meet @bob') == {'abc', 'bob'}
assert m('Hi @abc, meet @abc') == {'abc'}
assert not m('')
assert not m('@')
assert m('steemit.com/@apple') == {'apple'}
assert not m('joe@apple.com')
def test_post_basic(): def test_post_basic():
ret = post_basic(POST_1) ret = post_basic(POST_1)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment