def post_auto_comment(msg, user, url=None, ids=None): if not GlobalVars.metasmoke_key: log('info', 'Ignoring auto-comment') return response = None if url is not None: params = {"key": GlobalVars.metasmoke_key, "urls": url, "filter": "GFGJGHFJNFGNHKNIKHGGOMILHKLJIFFN"} response = requests.get(GlobalVars.metasmoke_host + "/api/v2.0/posts/urls", params=params).json() elif ids is not None: post_id, site = ids site = api_parameter_from_link(site) params = {"key": GlobalVars.metasmoke_key, "filter": "GFGJGHFJNFGNHKNIKHGGOMILHKLJIFFN"} response = requests.get("{}/api/v2.0/posts/uid/{}/{}".format(GlobalVars.metasmoke_host, site, post_id), params=params).json() if response and "items" in response and len(response["items"]) > 0: ms_id = response["items"][0]["id"] params = {"key": GlobalVars.metasmoke_key, "text": msg, "chat_user_id": user.id, "chat_host": user._client.host} requests.post("{}/api/v2.0/comments/post/{}".format(GlobalVars.metasmoke_host, ms_id), params=params)
def check_if_spam(post): # if not post.body: # body = "" # test, why = FindSpam.test_post(title, body, user_name, post_site, # is_answer, body_is_summary, owner_rep, post_score) test, why = FindSpam.test_post(post) if datahandling.is_blacklisted_user( parsing.get_user_from_url(post.user_url)): test.append("blacklisted user") blacklisted_user_data = datahandling.get_blacklisted_user_data( parsing.get_user_from_url(post.user_url)) if len(blacklisted_user_data) > 1: if blacklisted_user_data[1] == "metasmoke": blacklisted_by = "the metasmoke API" else: blacklisted_by = blacklisted_user_data[1] blacklisted_post_url = blacklisted_user_data[2] if blacklisted_post_url: rel_url = blacklisted_post_url.replace("http:", "", 1) why += u"\nBlacklisted user - blacklisted for {} (" \ u"https://m.erwaysoftware.com/posts/uid/{}/{}) by {}".format( blacklisted_post_url, api_parameter_from_link(rel_url), post_id_from_link(rel_url), blacklisted_by ) else: why += u"\n" + u"Blacklisted user - blacklisted by {}".format( blacklisted_by) if 0 < len(test): if datahandling.has_already_been_posted(post.post_site, post.post_id, post.title) \ or datahandling.is_false_positive((post.post_id, post.post_site)) \ or should_whitelist_prevent_alert(post.user_url, test) \ or datahandling.is_ignored_post((post.post_id, post.post_site)) \ or datahandling.is_auto_ignored_post((post.post_id, post.post_site)): return False, None, "" # Don't repost. Reddit will hate you. return True, test, why return False, None, ""
def handle_spam(post, reasons, why): post_url = parsing.to_protocol_relative( parsing.url_to_shortlink(post.post_url)) poster_url = parsing.to_protocol_relative( parsing.user_url_to_shortlink(post.user_url)) shortened_site = post.post_site.replace( "stackexchange.com", "SE") # site.stackexchange.com -> site.SE datahandling.append_to_latest_questions( post.post_site, post.post_id, post.title if not post.is_answer else "") if len(reasons) == 1 and ("all-caps title" in reasons or "repeating characters in title" in reasons or "repeating characters in body" in reasons or "repeating characters in answer" in reasons or "repeating words in title" in reasons or "repeating words in body" in reasons or "repeating words in answer" in reasons): datahandling.add_auto_ignored_post( (post.post_id, post.post_site, datetime.now())) if why is not None and why != "": datahandling.add_why(post.post_site, post.post_id, why) if post.is_answer and post.post_id is not None and post.post_id is not "": datahandling.add_post_site_id_link( (post.post_id, post.post_site, "answer"), post.parent.post_id) try: # If the post is an answer type post, the 'title' is going to be blank, so when posting the # message contents we need to set the post title to the *parent* title, so the message in the # chat is properly constructed with parent title instead. This will make things 'print' # in a proper way in chat messages. sanitized_title = parsing.sanitize_title( post.title if not post.is_answer else post.parent.title) prefix = u"[ [SmokeDetector](//goo.gl/eLDYqh) ]" if GlobalVars.metasmoke_key: prefix_ms = u"[ [SmokeDetector](//goo.gl/eLDYqh) | [MS](//m.erwaysoftware.com/posts/uid/{}/{}) ]".format( api_parameter_from_link(post_url), post.post_id) else: prefix_ms = prefix # We'll insert reason list later if not post.user_name.strip() or (not poster_url or poster_url.strip() == ""): s = u" {{}}: [{}]({}) by a deleted user on `{}`".format( sanitized_title, post_url, shortened_site) username = "" else: s = u" {{}}: [{}]({}) by [{}]({}) on `{}`".format( sanitized_title, post_url, post.user_name.strip(), poster_url, shortened_site) username = post.user_name.strip() Tasks.do(metasmoke.Metasmoke.send_stats_on_post, post.title_ignore_type, post_url, reasons, post.body, username, post.user_link, why, post.owner_rep, post.post_score, post.up_vote_count, post.down_vote_count) log('debug', GlobalVars.parser.unescape(s).encode('ascii', errors='replace')) GlobalVars.deletion_watcher.subscribe(post_url) reason = message = None for reason_count in range(5, 2, -1): # Try 5 reasons, then 4, then 3 reason = ", ".join(reasons[:reason_count]) if len(reasons) > reason_count: reason += ", +{} more".format(len(reasons) - reason_count) reason = reason[:1].upper() + reason[ 1:] # reason is capitalised, unlike the entries of reasons list message = prefix_ms + s.format(reason) # Insert reason list if len(message) <= 500: break # Problem solved, stop attempting s = s.format(reason) # Later code needs this variable if len(message) > 500: message = (prefix_ms + s)[:500] # Truncate directly and keep MS link without_roles = tuple( "no-" + reason for reason in reasons) + ("site-no-" + post.post_site, ) if set(reasons) - GlobalVars.experimental_reasons == set(): chatcommunicate.tell_rooms(message, ("experimental", ), without_roles, notify_site=post.post_site, report_data=(post_url, poster_url)) else: chatcommunicate.tell_rooms(message, ("all", "site-" + post.post_site), without_roles, notify_site=post.post_site, report_data=(post_url, poster_url)) except: exc_type, exc_obj, exc_tb = sys.exc_info() excepthook.uncaught_exception(exc_type, exc_obj, exc_tb)
def test_api_parameter_from_link(link, param): assert helpers.api_parameter_from_link(link) == param