def api_get_post(post_url): d = fetch_post_id_and_site_from_url(post_url) if d is None: return None post_id, site, post_type = d if post_type == "answer": api_filter = "!1zSl_EE)(nuF4Xn(2sDLC" req_url = "http://api.stackexchange.com/2.2/answers/" + post_id +\ "?site=" + site + "&filter=" + api_filter +\ "&key=IAkbitmze4B8KpacUfLqkw((" resp_json = requests.get(req_url).json() else: assert post_type == "question" api_filter = "!gB6tXYzgnc3pG)x0n*03eR9*kZWXReH54Qb" req_url = "http://api.stackexchange.com/2.2/questions/" + post_id +\ "?site=" + site + "&filter=" + api_filter +\ "&key=IAkbitmze4B8KpacUfLqkw((" resp_json = requests.get(req_url).json() if 'items' not in resp_json or len(resp_json['items']) == 0: return False item = resp_json['items'][0] post_data = PostData() post_data.post_id = post_id post_data.post_url = url_to_shortlink(item['link']) post_data.post_type = post_type h = HTMLParser.HTMLParser() post_data.title = h.unescape(item['title']) if 'owner' in item and 'owner' is not None: post_data.owner_name = item['owner']['display_name'] post_data.owner_url = item['owner']['link'] post_data.site = site return post_data
def api_get_post(post_url): GlobalVars.api_request_lock.acquire() # Respect backoff, if we were given one if GlobalVars.api_backoff_time > time.time(): time.sleep(GlobalVars.api_backoff_time - time.time() + 2) d = parsing.fetch_post_id_and_site_from_url(post_url) if d is None: GlobalVars.api_request_lock.release() return None post_id, site, post_type = d if post_type == "answer": api_filter = r"!FdmhxNRjn0vYtGOu3FfS5xSwvL" else: assert post_type == "question" api_filter = r"!DEPw4-PqDduRmCwMBNAxrCdSZl81364qitC3TebCzqyF4-y*r2L" request_url = "https://api.stackexchange.com/2.2/{}s/{}".format( post_type, post_id) params = { 'filter': api_filter, 'key': 'IAkbitmze4B8KpacUfLqkw((', 'site': site } response = requests.get(request_url, params=params).json() if "backoff" in response: if GlobalVars.api_backoff_time < time.time() + response["backoff"]: GlobalVars.api_backoff_time = time.time() + response["backoff"] if 'items' not in response or len(response['items']) == 0: GlobalVars.api_request_lock.release() return False GlobalVars.api_request_lock.release() item = response['items'][0] post_data = PostData() post_data.post_id = post_id post_data.post_url = parsing.url_to_shortlink(item['link']) post_data.post_type = post_type post_data.title = html.unescape(item['title']) if 'owner' in item and 'link' in item['owner']: post_data.owner_name = html.unescape(item['owner']['display_name']) post_data.owner_url = item['owner']['link'] post_data.owner_rep = item['owner']['reputation'] else: post_data.owner_name = "" post_data.owner_url = "" post_data.owner_rep = 1 post_data.site = site post_data.body = item['body'] post_data.score = item['score'] post_data.up_vote_count = item['up_vote_count'] post_data.down_vote_count = item['down_vote_count'] post_data.creation_date = item['creation_date'] try: post_data.last_edit_date = item['last_edit_date'] except KeyError: post_data.last_edit_date = post_data.creation_date # Key not present = not edited if post_type == "answer": post_data.question_id = item['question_id'] return post_data
def api_get_post(post_url): d = fetch_post_id_and_site_from_url(post_url) if d is None: return None post_id, site, post_type = d if post_type == "answer": api_filter = "!)Q7pIaQiHdpLxolL4GBob2gX" req_url = "http://api.stackexchange.com/2.2/answers/" + post_id +\ "?site=" + site + "&filter=" + api_filter +\ "&key=IAkbitmze4B8KpacUfLqkw((" resp_json = requests.get(req_url).json() else: assert post_type == "question" api_filter = "!5-dwy(WuZ4NTyJamA*kR8slbR(yU(eC-1U_sEK" req_url = "http://api.stackexchange.com/2.2/questions/" + post_id +\ "?site=" + site + "&filter=" + api_filter +\ "&key=IAkbitmze4B8KpacUfLqkw((" resp_json = requests.get(req_url).json() if 'items' not in resp_json or len(resp_json['items']) == 0: return False item = resp_json['items'][0] post_data = PostData() post_data.post_id = post_id post_data.post_url = url_to_shortlink(item['link']) post_data.post_type = post_type h = HTMLParser.HTMLParser() post_data.title = h.unescape(item['title']) if 'owner' in item and 'owner' is not None: post_data.owner_name = item['owner']['display_name'] post_data.owner_url = item['owner']['link'] post_data.site = site post_data.body = item['body'] return post_data
def handle_spam(post, reasons, why): 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.utcnow())) 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 != "": datahandling.add_post_site_id_link((post.post_id, post.post_site, "answer"), post.parent.post_id) try: 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)) if not post.user_name.strip() or (not poster_url or poster_url.strip() == ""): username = "" else: 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) offensive_mask = 'offensive title detected' in reasons message = build_message(post, reasons) if offensive_mask: post.title = "(potentially offensive title -- see MS for details)" clean_message = build_message(post, reasons) log('debug', GlobalVars.parser.unescape(message).encode('ascii', errors='replace')) GlobalVars.deletion_watcher.subscribe(post_url) without_roles = tuple(["no-" + reason for reason in reasons]) + ("site-no-" + post.post_site,) if set(reasons) - GlobalVars.experimental_reasons == set() and \ not why.startswith("Post manually "): chatcommunicate.tell_rooms(message, ("experimental-all-sites", "experimental-site-" + post.post_site), without_roles, notify_site=post.post_site, report_data=(post_url, poster_url)) else: if offensive_mask: chatcommunicate.tell_rooms(message, ("all-sites", "site-" + post.post_site), without_roles + ("offensive-mask",), notify_site=post.post_site, report_data=(post_url, poster_url)) chatcommunicate.tell_rooms(clean_message, ("all-sites", "site-" + post.post_site), without_roles + ("no-offensive-mask",), notify_site=post.post_site, report_data=(post_url, poster_url)) else: chatcommunicate.tell_rooms(message, ("all-sites", "site-" + post.post_site), without_roles, notify_site=post.post_site, report_data=(post_url, poster_url)) except Exception as e: excepthook.uncaught_exception(*sys.exc_info())
def api_get_post(post_url): GlobalVars.api_request_lock.acquire() # Respect backoff, if we were given one if GlobalVars.api_backoff_time > time.time(): time.sleep(GlobalVars.api_backoff_time - time.time() + 2) d = parsing.fetch_post_id_and_site_from_url(post_url) if d is None: GlobalVars.api_request_lock.release() return None post_id, site, post_type = d if post_type == "answer": api_filter = r"!FdmhxNRjn0vYtGOu3FfS5xSwvL" else: assert post_type == "question" api_filter = r"!DEPw4-PqDduRmCwMBNAxrCdSZl81364qitC3TebCzqyF4-y*r2L" request_url = "https://api.stackexchange.com/2.2/{}s/{}".format(post_type, post_id) params = { 'filter': api_filter, 'key': 'IAkbitmze4B8KpacUfLqkw((', 'site': site } response = requests.get(request_url, params=params).json() if "backoff" in response: if GlobalVars.api_backoff_time < time.time() + response["backoff"]: GlobalVars.api_backoff_time = time.time() + response["backoff"] if 'items' not in response or len(response['items']) == 0: GlobalVars.api_request_lock.release() return False GlobalVars.api_request_lock.release() item = response['items'][0] post_data = PostData() post_data.post_id = post_id post_data.post_url = parsing.url_to_shortlink(item['link']) post_data.post_type = post_type post_data.title = html.unescape(item['title']) if 'owner' in item and 'link' in item['owner']: post_data.owner_name = html.unescape(item['owner']['display_name']) post_data.owner_url = item['owner']['link'] post_data.owner_rep = item['owner']['reputation'] else: post_data.owner_name = "" post_data.owner_url = "" post_data.owner_rep = 1 post_data.site = site post_data.body = item['body'] post_data.score = item['score'] post_data.up_vote_count = item['up_vote_count'] post_data.down_vote_count = item['down_vote_count'] post_data.creation_date = item['creation_date'] try: post_data.last_edit_date = item['last_edit_date'] except KeyError: post_data.last_edit_date = post_data.creation_date # Key not present = not edited if post_type == "answer": post_data.question_id = item['question_id'] return post_data
def handle_spam(title, body, poster, site, post_url, poster_url, post_id, reasons, is_answer, why=""): post_url = to_protocol_relative(url_to_shortlink(post_url)) poster_url = to_protocol_relative(poster_url) reason = ", ".join(reasons).capitalize() if "Url" in reason: reason = reason.replace("Url", "URL") append_to_latest_questions(site, post_id, title if not 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): add_auto_ignored_post((post_id, site, datetime.now())) if why is not None and why != "": add_why(site, post_id, why) try: owner = poster_url users_file = open("users.txt", "a") users_file.write(site + " " + owner + " " + title + " " + post_url + "\n") users_file.close() except Exception as e: print e try: title = escape_special_chars_in_title(title) if not poster.strip(): s = u"[ [SmokeDetector](https://github.com/Charcoal-SE/SmokeDetector) ] {}: [{}]({}) by a deleted user on `{}`" \ .format(reason, title.strip(), post_url, site) username = "" user_link = "" else: s = u"[ [SmokeDetector](https://github.com/Charcoal-SE/SmokeDetector) ] {}: [{}]({}) by [{}]({}) on `{}`" \ .format(reason, title.strip(), post_url, poster.strip(), poster_url, site) username = poster.strip() user_link = poster_url t_metasmoke = Thread(target=Metasmoke.send_stats_on_post, args=(title, post_url, reason.split(", "), body, username, user_link)) t_metasmoke.start() print GlobalVars.parser.unescape(s).encode('ascii', errors='replace') if time.time() >= GlobalVars.blockedTime: append_to_latest_questions(site, post_id, title) if reason not in GlobalVars.experimental_reasons: GlobalVars.charcoal_hq.send_message(s) GlobalVars.tavern_on_the_meta.send_message(s) if site == "stackoverflow.com": GlobalVars.socvr.send_message(s) for specialroom in GlobalVars.specialrooms: sites = specialroom["sites"] if site in sites and reason not in specialroom["unwantedReasons"]: specialroom["room"].send_message(s) except: print "NOP"
def handle_spam(title, poster, site, post_url, poster_url, post_id, reasons, is_answer, why=""): post_url = to_protocol_relative(url_to_shortlink(post_url)) poster_url = to_protocol_relative(poster_url) reason = ", ".join(reasons).capitalize() if "Url" in reason: reason = reason.replace("Url", "URL") append_to_latest_questions(site, post_id, title if not 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): add_auto_ignored_post((post_id, site, datetime.now())) if why is not None and why != "": add_why(site, post_id, why) try: owner = poster_url users_file = open("users.txt", "a") users_file.write(site + " " + owner + " " + title + " " + post_url + "\n") users_file.close() except Exception as e: print e try: title = escape_special_chars_in_title(title) if not poster.strip(): s = "[ [SmokeDetector](https://github.com/Charcoal-SE/SmokeDetector) ] %s: [%s](%s) by a deleted user on `%s`" % \ (reason, title.strip(), post_url, site) else: s = "[ [SmokeDetector](https://github.com/Charcoal-SE/SmokeDetector) ] %s: [%s](%s) by [%s](%s) on `%s`" % \ (reason, title.strip(), post_url, poster.strip(), poster_url, site) print GlobalVars.parser.unescape(s).encode('ascii', errors='replace') if time.time() >= GlobalVars.blockedTime: append_to_latest_questions(site, post_id, title) if reason not in GlobalVars.experimental_reasons: GlobalVars.charcoal_hq.send_message(s) GlobalVars.tavern_on_the_meta.send_message(s) if site == "stackoverflow.com": GlobalVars.socvr.send_message(s) for specialroom in GlobalVars.specialrooms: sites = specialroom["sites"] if site in sites and reason not in specialroom[ "unwantedReasons"]: specialroom["room"].send_message(s) except: print "NOP"
def api_get_post(post_url): GlobalVars.api_request_lock.acquire() # Respect backoff, if we were given one if GlobalVars.api_backoff_time > time.time(): time.sleep(GlobalVars.api_backoff_time - time.time() + 2) d = parsing.fetch_post_id_and_site_from_url(post_url) if d is None: GlobalVars.api_request_lock.release() return None post_id, site, post_type = d if post_type == "answer": api_filter = "!FdmhxNQy0ZXsmxUOvWMVSbuktT" else: assert post_type == "question" api_filter = "!)Ehu.SHRfXhu2eCP4p6wd*Wxyw1XouU5qO83b7X5GQK6ciVat" request_url = "http://api.stackexchange.com/2.2/{type}s/{post_id}?site={site}&filter={api_filter}&" \ "key=IAkbitmze4B8KpacUfLqkw((".format(type=post_type, post_id=post_id, site=site, api_filter=api_filter) response = requests.get(request_url).json() if "backoff" in response: if GlobalVars.api_backoff_time < time.time() + response["backoff"]: GlobalVars.api_backoff_time = time.time() + response["backoff"] if 'items' not in response or len(response['items']) == 0: GlobalVars.api_request_lock.release() return False GlobalVars.api_request_lock.release() item = response['items'][0] post_data = PostData() post_data.post_id = post_id post_data.post_url = parsing.url_to_shortlink(item['link']) post_data.post_type = post_type h = HTMLParser.HTMLParser() post_data.title = h.unescape(item['title']) if 'owner' in item and 'link' in item['owner']: post_data.owner_name = h.unescape(item['owner']['display_name']) post_data.owner_url = item['owner']['link'] post_data.owner_rep = item['owner']['reputation'] else: post_data.owner_name = "" post_data.owner_url = "" post_data.owner_rep = 1 post_data.site = site post_data.body = item['body'] post_data.score = item['score'] post_data.up_vote_count = item['up_vote_count'] post_data.down_vote_count = item['down_vote_count'] if post_type == "answer": post_data.question_id = item['question_id'] return post_data
def api_get_post(post_url): GlobalVars.api_request_lock.acquire() # Respect backoff, if we were given one if GlobalVars.api_backoff_time > time.time(): time.sleep(GlobalVars.api_backoff_time - time.time() + 2) d = parsing.fetch_post_id_and_site_from_url(post_url) if d is None: GlobalVars.api_request_lock.release() return None post_id, site, post_type = d if post_type == "answer": api_filter = "!FdmhxNQy0ZXsmxUOvWMVSbuktT" else: assert post_type == "question" api_filter = "!)Ehu.SHRfXhu2eCP4p6wd*Wxyw1XouU5qO83b7X5GQK6ciVat" request_url = "http://api.stackexchange.com/2.2/{type}s/{post_id}?site={site}&filter={api_filter}&" \ "key=IAkbitmze4B8KpacUfLqkw((".format(type=post_type, post_id=post_id, site=site, api_filter=api_filter) response = requests.get(request_url).json() if "backoff" in response: if GlobalVars.api_backoff_time < time.time() + response["backoff"]: GlobalVars.api_backoff_time = time.time() + response["backoff"] if 'items' not in response or len(response['items']) == 0: GlobalVars.api_request_lock.release() return False GlobalVars.api_request_lock.release() item = response['items'][0] post_data = PostData() post_data.post_id = post_id post_data.post_url = parsing.url_to_shortlink(item['link']) post_data.post_type = post_type post_data.title = html.unescape(item['title']) if 'owner' in item and 'link' in item['owner']: post_data.owner_name = html.unescape(item['owner']['display_name']) post_data.owner_url = item['owner']['link'] post_data.owner_rep = item['owner']['reputation'] else: post_data.owner_name = "" post_data.owner_url = "" post_data.owner_rep = 1 post_data.site = site post_data.body = item['body'] post_data.score = item['score'] post_data.up_vote_count = item['up_vote_count'] post_data.down_vote_count = item['down_vote_count'] if post_type == "answer": post_data.question_id = item['question_id'] return post_data
def api_get_post(post_url): # Respect backoff, if we were given one if GlobalVars.api_backoff_time > time.time(): time.sleep(GlobalVars.api_backoff_time - time.time() + 2) d = fetch_post_id_and_site_from_url(post_url) if d is None: return None post_id, site, post_type = d if post_type == "answer": api_filter = "!FdmhxNQy0ZXsmxUOvWMVSbuktT" req_url = "http://api.stackexchange.com/2.2/answers/" + post_id +\ "?site=" + site + "&filter=" + api_filter +\ "&key=IAkbitmze4B8KpacUfLqkw((" resp_json = requests.get(req_url).json() else: assert post_type == "question" api_filter = "!)Ehu.SHRfXhu2eCP4p6wd*Wxyw1XouU5qO83b7X5GQK6ciVat" req_url = "http://api.stackexchange.com/2.2/questions/" + post_id +\ "?site=" + site + "&filter=" + api_filter +\ "&key=IAkbitmze4B8KpacUfLqkw((" resp_json = requests.get(req_url).json() if "backoff" in resp_json: if GlobalVars.api_backoff_time < time.time() + resp_json["backoff"]: GlobalVars.api_backoff_time = time.time() + resp_json["backoff"] if 'items' not in resp_json or len(resp_json['items']) == 0: return False item = resp_json['items'][0] post_data = PostData() post_data.post_id = post_id post_data.post_url = url_to_shortlink(item['link']) post_data.post_type = post_type h = HTMLParser.HTMLParser() post_data.title = h.unescape(item['title']) if 'owner' in item and 'owner' is not None: post_data.owner_name = item['owner']['display_name'] post_data.owner_url = item['owner']['link'] post_data.owner_rep = item['owner']['reputation'] post_data.site = site post_data.body = item['body'] post_data.score = item['score'] post_data.up_vote_count = item['up_vote_count'] post_data.down_vote_count = item['down_vote_count'] if post_type == "answer": post_data.question_id = item['question_id'] return post_data
def api_get_post(post_url): # Respect backoff, if we were given one if GlobalVars.api_backoff_time > time.time(): time.sleep(GlobalVars.api_backoff_time - time.time() + 2) d = fetch_post_id_and_site_from_url(post_url) if d is None: return None post_id, site, post_type = d if post_type == "answer": api_filter = "!FdmhxNQy0ZXsmxUOvWMVSbuktT" req_url = "http://api.stackexchange.com/2.2/answers/" + post_id +\ "?site=" + site + "&filter=" + api_filter +\ "&key=IAkbitmze4B8KpacUfLqkw((" resp_json = requests.get(req_url).json() else: assert post_type == "question" api_filter = "!)Ehu.SHRfXhu2eCP4p6wd*Wxyw1XouU5qO83b7X5GQK6ciVat" req_url = "http://api.stackexchange.com/2.2/questions/" + post_id +\ "?site=" + site + "&filter=" + api_filter +\ "&key=IAkbitmze4B8KpacUfLqkw((" resp_json = requests.get(req_url).json() if "backoff" in resp_json: if GlobalVars.api_backoff_time < time.time() + resp_json["backoff"]: GlobalVars.api_backoff_time = time.time() + resp_json["backoff"] if 'items' not in resp_json or len(resp_json['items']) == 0: return False item = resp_json['items'][0] post_data = PostData() post_data.post_id = post_id post_data.post_url = url_to_shortlink(item['link']) post_data.post_type = post_type h = HTMLParser.HTMLParser() post_data.title = h.unescape(item['title']) if 'owner' in item and 'owner' is not None: post_data.owner_name = item['owner']['display_name'] post_data.owner_url = item['owner']['link'] post_data.owner_rep = item['owner']['reputation'] post_data.site = site post_data.body = item['body'] post_data.score = item['score'] post_data.up_vote_count = item['up_vote_count'] post_data.down_vote_count = item['down_vote_count'] return post_data
def handle_spam(title, poster, site, post_url, poster_url, post_id, reasons, is_answer): post_url = url_to_shortlink(post_url) reasons = list(set(reasons)) reasons.sort() reason = ", ".join(reasons).capitalize() append_to_latest_questions(site, post_id, title if not 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): add_auto_ignored_post((post_id, site, datetime.now())) try: owner = poster_url users_file = open("users.txt", "a") users_file.write(site + " " + owner + " " + title + " " + post_url + "\n") users_file.close() except Exception as e: print e try: title = escape_special_chars_in_title(title) if not poster.strip(): s = "[ [SmokeDetector](https://github.com/Charcoal-SE/SmokeDetector) ] %s: [%s](%s) by a deleted user on `%s`" % \ (reason, title.strip(), post_url, site) else: s = "[ [SmokeDetector](https://github.com/Charcoal-SE/SmokeDetector) ] %s: [%s](%s) by [%s](%s) on `%s`" % \ (reason, title.strip(), post_url, poster.strip(), poster_url, site) print GlobalVars.parser.unescape(s).encode('ascii', errors='replace') if time.time() >= GlobalVars.blockedTime: append_to_latest_questions(site, post_id, title) if reason not in GlobalVars.experimental_reasons: GlobalVars.charcoal_hq.send_message(s) GlobalVars.tavern_on_the_meta.send_message(s) for specialroom in GlobalVars.specialrooms: sites = specialroom["sites"] if site in sites and reason not in specialroom["unwantedReasons"]: specialroom["room"].send_message(s) except: print "NOP"
def handle_spam(title, body, poster, site, post_url, poster_url, post_id, reasons, is_answer, why="", owner_rep=None, post_score=None, up_vote_count=None, down_vote_count=None, question_id=None): post_url = to_protocol_relative(url_to_shortlink(post_url)) poster_url = to_protocol_relative(user_url_to_shortlink(poster_url)) reason = ", ".join(reasons) reason = reason[:1].upper() + reason[ 1:] # reason is capitalised, unlike the entries of reasons list append_to_latest_questions(site, post_id, title if not 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): add_auto_ignored_post((post_id, site, datetime.now())) if why is not None and why != "": add_why(site, post_id, why) if is_answer and question_id is not None: add_post_site_id_link((post_id, site, "answer"), question_id) try: title = escape_special_chars_in_title(title) if not poster.strip(): s = u"[ [SmokeDetector](//git.io/vgx7b) ] {}: [{}]({}) by a deleted user on `{}`" \ .format(reason, title.strip(), post_url, site) username = "" user_link = "" else: s = u"[ [SmokeDetector](//git.io/vgx7b) ] {}: [{}]({}) by [{}]({}) on `{}`" \ .format(reason, title.strip(), post_url, poster.strip(), poster_url, site) username = poster.strip() user_link = poster_url t_metasmoke = Thread(target=Metasmoke.send_stats_on_post, args=(title, post_url, reason.split(", "), body, username, user_link, why, owner_rep, post_score, up_vote_count, down_vote_count)) t_metasmoke.start() print GlobalVars.parser.unescape(s).encode('ascii', errors='replace') if time.time() >= GlobalVars.blockedTime["all"]: append_to_latest_questions(site, post_id, title) if reason not in GlobalVars.experimental_reasons: metasmoke_link = u" [\u2026](//metasmoke.erwaysoftware.com/posts/by-url?url=" + post_url + ")" if time.time() >= GlobalVars.blockedTime[ GlobalVars.charcoal_room_id]: chq_pings = get_user_names_on_notification_list( "stackexchange.com", GlobalVars.charcoal_room_id, site, GlobalVars.wrap) chq_msg = append_pings(s, chq_pings) chq_msg_ms = chq_msg + metasmoke_link GlobalVars.charcoal_hq.send_message( chq_msg_ms if len(chq_msg_ms) <= 500 else chq_msg if len(chq_msg) <= 500 else s[0:500]) if reason not in GlobalVars.non_tavern_reasons and site not in GlobalVars.non_tavern_sites and time.time( ) >= GlobalVars.blockedTime[GlobalVars.meta_tavern_room_id]: tavern_pings = get_user_names_on_notification_list( "meta.stackexchange.com", GlobalVars.meta_tavern_room_id, site, GlobalVars.wrapm) tavern_msg = append_pings(s, tavern_pings) tavern_msg_ms = tavern_msg + metasmoke_link msg_to_send = tavern_msg_ms if len( tavern_msg_ms) <= 500 else tavern_msg if len( tavern_msg) <= 500 else s[0:500] t_check_websocket = Thread( target=DeletionWatcher.post_message_if_not_deleted, args=((post_id, site, "answer" if is_answer else "question"), post_url, msg_to_send, GlobalVars.tavern_on_the_meta)) t_check_websocket.daemon = True t_check_websocket.start() if site == "stackoverflow.com" and reason not in GlobalVars.non_socvr_reasons and time.time( ) >= GlobalVars.blockedTime[GlobalVars.socvr_room_id]: socvr_pings = get_user_names_on_notification_list( "stackoverflow.com", GlobalVars.socvr_room_id, site, GlobalVars.wrapso) socvr_msg = append_pings(s, socvr_pings) socvr_msg_ms = socvr_msg + metasmoke_link GlobalVars.socvr.send_message( socvr_msg_ms if len(socvr_msg_ms) <= 500 else socvr_msg if len(socvr_msg) <= 500 else s[0:500]) for specialroom in GlobalVars.specialrooms: sites = specialroom["sites"] if site in sites and reason not in specialroom[ "unwantedReasons"]: room = specialroom["room"] if room.id not in GlobalVars.blockedTime or time.time( ) >= GlobalVars.blockedTime[room.id]: room_site = room._client.host room_id = int(room.id) room_pings = get_user_names_on_notification_list( room_site, room_id, site, room._client) room_msg = append_pings(s, room_pings) room_msg_ms = room_msg + metasmoke_link specialroom["room"].send_message( room_msg_ms if len(room_msg_ms) <= 500 else room_msg if len(room_msg) <= 500 else s[0:500]) except: exc_type, exc_obj, exc_tb = sys.exc_info() excepthook.uncaught_exception(exc_type, exc_obj, exc_tb)
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) if GlobalVars.reason_weights or GlobalVars.metasmoke_key: reason_weight = sum_weight(reasons) if reason_weight >= 1000: reason_weight_s = " (**{:,}**)".format(reason_weight) else: reason_weight_s = " ({:,})".format(reason_weight) else: # No reason weight if neither cache nor MS reason_weight_s = "" 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) sanitized_title = escape_format(sanitized_title).strip() prefix = u"[ [SmokeDetector](//goo.gl/eLDYqh) ]" if GlobalVars.metasmoke_key: prefix_ms = u"[ [SmokeDetector](//goo.gl/eLDYqh) | [MS]({}) ]".format( to_metasmoke_link(post_url, protocol=False)) else: prefix_ms = prefix # We'll insert reason list later edited = '' if not post.edited else ' \u270F\uFE0F' if not post.user_name.strip() or (not poster_url or poster_url.strip() == ""): s = " {{}}{}: [{}]({}){} by a deleted user on `{}`".format( reason_weight_s, sanitized_title, post_url, edited, shortened_site) username = "" else: username = post.user_name.strip() escaped_username = escape_format(parsing.escape_markdown(username)) s = " {{}}{}: [{}]({}){} by [{}]({}) on `{}`".format( reason_weight_s, sanitized_title, post_url, edited, escaped_username, poster_url, shortened_site) 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, 0, -1): # Try 5 reasons and all the way down to 1 reason = ", ".join(reasons[:reason_count]) if len(reasons) > reason_count: reason += ", +{} more".format(len(reasons) - reason_count) reason = reason.capitalize() 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() and \ not why.startswith("Post manually "): 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 Exception as e: excepthook.uncaught_exception(*sys.exc_info())
def build_message(post, reasons): # This is the main report format. Username and user link are deliberately not separated as with title and post # link, because we may want to use "by a deleted user" rather than a username+link. message_format = "{prefix_ms} {{reasons}} ({reason_weight}): [{title}]({post_url}) by {user} on `{site}`" # Post URL, user URL, and site details are all easy - just data from the post object, transformed a bit # via datahandling. 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 # Message prefix. There's always a link to SmokeDetector; if we have a metasmoke key, there's also a link to the # post's MS record. If we *don't* have a MS key, it's a fair assumption that the post won't be in metasmoke as # we didn't have a key to create a record for it. prefix = u"[ [SmokeDetector](//git.io/vyDZv) ]" if GlobalVars.metasmoke_key: prefix = u"[ [SmokeDetector](//git.io/vyDZv) | [MS]({}) ]".format( to_metasmoke_link(post_url, protocol=False)) # If we have reason weights cached (GlobalVars.reason_weights) we can calculate total weight for this report; # likewise, if we have a MS key, we can fetch the weights and then calculate. If we have neither, tough luck. if GlobalVars.reason_weights or GlobalVars.metasmoke_key: reason_weight = sum_weight(reasons) if reason_weight >= 1000: reason_weight = "**{}**".format(reason_weight) else: reason_weight = "{}".format(reason_weight) else: reason_weight = "" # If the post is an answer, it doesn't have a title, so we use the question's title instead. Either way, we # make sure it's escaped. We also add the edited indicator here. sanitized_title = parsing.sanitize_title( post.title if not post.is_answer else post.parent.title) sanitized_title = escape_format(sanitized_title).strip() if post.edited: # Append a pencil emoji for edited posts sanitized_title += ' \u270F\uFE0F' # If we have user details available, we'll linkify the username. If we don't, we call it a deleted user. if not post.user_name.strip() or (not poster_url or poster_url.strip() == ""): user = "******" else: username = post.user_name.strip() escaped_username = escape_format(parsing.escape_markdown(username)) user = "******".format(escaped_username, poster_url) # Build the main body of the message. The next step is to insert the reason list while keeping the message # under 500 characters long. message = message_format.format(prefix_ms=prefix, reason_weight=reason_weight, title=sanitized_title, post_url=post_url, user=user, site=shortened_site) for reason_count in range(5, 0, -1): reason = ", ".join(reasons[:reason_count]) if len(reasons) > reason_count: reason += ", +{} more".format(len(reasons) - reason_count) reason = reason.capitalize() attempt = message.format(reasons=reason) if len(attempt) <= 500: message = attempt break # If the message is still longer than 500 chars after trying to reduce the reason list, we're out of options, # so just cut the end of the message off. if len(message) > 500: message = message[:500] return message
def handle_spam(title, body, poster, site, post_url, poster_url, post_id, reasons, is_answer, why="", owner_rep=None, post_score=None, up_vote_count=None, down_vote_count=None): post_url = to_protocol_relative(url_to_shortlink(post_url)) poster_url = to_protocol_relative(poster_url) reason = ", ".join(reasons) reason = reason[:1].upper() + reason[1:] # reason is capitalised, unlike the entries of reasons list append_to_latest_questions(site, post_id, title if not 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): add_auto_ignored_post((post_id, site, datetime.now())) if why is not None and why != "": add_why(site, post_id, why) try: title = escape_special_chars_in_title(title) if not poster.strip(): s = u"[ [SmokeDetector](https://github.com/Charcoal-SE/SmokeDetector) ] {}: [{}]({}) by a deleted user on `{}`" \ .format(reason, title.strip(), post_url, site) username = "" user_link = "" else: s = u"[ [SmokeDetector](https://github.com/Charcoal-SE/SmokeDetector) ] {}: [{}]({}) by [{}]({}) on `{}`" \ .format(reason, title.strip(), post_url, poster.strip(), poster_url + "?tab=topactivity", site) username = poster.strip() user_link = poster_url t_metasmoke = Thread(target=Metasmoke.send_stats_on_post, args=(title, post_url, reason.split(", "), body, username, user_link, why, owner_rep, post_score, up_vote_count, down_vote_count)) t_metasmoke.start() print GlobalVars.parser.unescape(s).encode('ascii', errors='replace') if time.time() >= GlobalVars.blockedTime: append_to_latest_questions(site, post_id, title) if reason not in GlobalVars.experimental_reasons: chq_pings = get_user_names_on_notification_list("stackexchange.com", GlobalVars.charcoal_room_id, site, GlobalVars.wrap) chq_msg = append_pings(s, chq_pings) GlobalVars.charcoal_hq.send_message(chq_msg if len(chq_msg) <= 500 else s) if reason not in GlobalVars.non_tavern_reasons and site not in GlobalVars.non_tavern_sites: tavern_pings = get_user_names_on_notification_list("meta.stackexchange.com", GlobalVars.meta_tavern_room_id, site, GlobalVars.wrapm) tavern_msg = append_pings(s, tavern_pings) GlobalVars.tavern_on_the_meta.send_message(tavern_msg if len(tavern_msg) <= 500 else s) if site == "stackoverflow.com": socvr_pings = get_user_names_on_notification_list("stackoverflow.com", GlobalVars.socvr_room_id, site, GlobalVars.wrapso) socvr_msg = append_pings(s, socvr_pings) GlobalVars.socvr.send_message(socvr_msg if len(socvr_msg) <= 500 else s) for specialroom in GlobalVars.specialrooms: sites = specialroom["sites"] if site in sites and reason not in specialroom["unwantedReasons"]: room = specialroom["room"] room_site = room._client.host room_id = int(room.id) room_pings = get_user_names_on_notification_list(room_site, room_id, site, room._client) room_msg = append_pings(s, room_pings) specialroom["room"].send_message(room_msg if len(room_msg) <= 500 else s) except: exc_type, exc_obj, exc_tb = sys.exc_info() excepthook.uncaught_exception(exc_type, exc_obj, exc_tb)
def api_get_post(post_url): GlobalVars.api_request_lock.acquire() # Respect backoff, if we were given one if GlobalVars.api_backoff_time > time.time(): time.sleep(GlobalVars.api_backoff_time - time.time() + 2) d = parsing.fetch_post_id_and_site_from_url(post_url) if d is None: GlobalVars.api_request_lock.release() return None post_id, site, post_type = d if post_type == "answer": api_filter = r"!4z6S)cPO)zvpuDWsWTAUW(kaV6K6thsqi1tlYa" elif post_type == "question": api_filter = r"!m)9.UaQrI5-DZXtlTpWhv2HroYRgS3dPhv.2vxV7fpGT*27rEHM.BKV1" else: raise ValueError("Unknown post type: {}".format(post_type)) request_url = "https://api.stackexchange.com/2.2/{}s/{}".format( post_type, post_id) params = { 'filter': api_filter, 'key': 'IAkbitmze4B8KpacUfLqkw((', 'site': site } response = requests.get(request_url, params=params).json() if "backoff" in response: if GlobalVars.api_backoff_time < time.time() + response["backoff"]: GlobalVars.api_backoff_time = time.time() + response["backoff"] if 'items' not in response or len(response['items']) == 0: GlobalVars.api_request_lock.release() return False GlobalVars.api_request_lock.release() item = response['items'][0] post_data = PostData() post_data.post_id = post_id post_data.post_url = parsing.url_to_shortlink(item['link']) post_data.post_type = post_type post_data.title = html.unescape(item['title']) if 'owner' in item and 'link' in item['owner']: post_data.owner_name = html.unescape(item['owner']['display_name']) post_data.owner_url = item['owner']['link'] post_data.owner_rep = item['owner']['reputation'] else: post_data.owner_name = "" post_data.owner_url = "" post_data.owner_rep = 1 post_data.site = site post_data.body = item['body'] post_data.body_markdown = item['body_markdown'] post_data.score = item['score'] post_data.up_vote_count = item['up_vote_count'] post_data.down_vote_count = item['down_vote_count'] post_data.creation_date = item['creation_date'] try: post_data.last_edit_date = item['last_edit_date'] except KeyError: post_data.last_edit_date = post_data.creation_date # Key not present = not edited if post_type == "answer": post_data.question_id = item['question_id'] return post_data
def handle_spam(title, body, poster, site, post_url, poster_url, post_id, reasons, is_answer, why="", owner_rep=None, post_score=None, up_vote_count=None, down_vote_count=None, question_id=None): post_url = parsing.to_protocol_relative(parsing.url_to_shortlink(post_url)) poster_url = parsing.to_protocol_relative(parsing.user_url_to_shortlink(poster_url)) reason = ", ".join(reasons) reason = reason[:1].upper() + reason[1:] # reason is capitalised, unlike the entries of reasons list datahandling.append_to_latest_questions(site, post_id, title if not 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_id, site, datetime.now())) if why is not None and why != "": datahandling.add_why(site, post_id, why) if is_answer and question_id is not None: datahandling.add_post_site_id_link((post_id, site, "answer"), question_id) try: title = parsing.escape_special_chars_in_title(title) sanitized_title = regex.sub('https?://', '', title) prefix = u"[ [SmokeDetector](//git.io/vgx7b) ]" if GlobalVars.metasmoke_key: prefix_ms = u"[ [SmokeDetector](//git.io/vgx7b) | [MS](//m.erwaysoftware.com/posts/by-url?url=" + post_url + ") ]" else: prefix_ms = prefix if not poster.strip(): s = u" {}: [{}]({}) by a deleted user on `{}`".format(reason, sanitized_title.strip(), post_url, site) username = "" user_link = "" else: s = u" {}: [{}]({}) by [{}]({}) on `{}`" .format(reason, sanitized_title.strip(), post_url, poster.strip(), poster_url, site) username = poster.strip() user_link = poster_url t_metasmoke = Thread(target=metasmoke.Metasmoke.send_stats_on_post, args=(title, post_url, reason.split(", "), body, username, user_link, why, owner_rep, post_score, up_vote_count, down_vote_count)) t_metasmoke.start() print GlobalVars.parser.unescape(s).encode('ascii', errors='replace') if time.time() >= GlobalVars.blockedTime["all"]: datahandling.append_to_latest_questions(site, post_id, title) if reason not in GlobalVars.experimental_reasons: if time.time() >= GlobalVars.blockedTime[GlobalVars.charcoal_room_id]: chq_pings = datahandling.get_user_names_on_notification_list("stackexchange.com", GlobalVars.charcoal_room_id, site, GlobalVars.wrap) chq_msg = prefix + s chq_msg_pings = prefix + datahandling.append_pings(s, chq_pings) chq_msg_pings_ms = prefix_ms + datahandling.append_pings(s, chq_pings) msg_to_send = chq_msg_pings_ms if len(chq_msg_pings_ms) <= 500 else chq_msg_pings if len(chq_msg_pings) <= 500 else chq_msg[0:500] GlobalVars.charcoal_hq.send_message(msg_to_send) if not should_reasons_prevent_tavern_posting(reasons) and site not in GlobalVars.non_tavern_sites and time.time() >= GlobalVars.blockedTime[GlobalVars.meta_tavern_room_id]: tavern_pings = datahandling.get_user_names_on_notification_list("meta.stackexchange.com", GlobalVars.meta_tavern_room_id, site, GlobalVars.wrapm) tavern_msg = prefix + s tavern_msg_pings = prefix + datahandling.append_pings(s, tavern_pings) tavern_msg_pings_ms = prefix_ms + datahandling.append_pings(s, tavern_pings) msg_to_send = tavern_msg_pings_ms if len(tavern_msg_pings_ms) <= 500 else tavern_msg_pings if len(tavern_msg_pings) <= 500 else tavern_msg[0:500] t_check_websocket = Thread(target=deletionwatcher.DeletionWatcher.post_message_if_not_deleted, args=((post_id, site, "answer" if is_answer else "question"), post_url, msg_to_send, GlobalVars.tavern_on_the_meta)) t_check_websocket.daemon = True t_check_websocket.start() if site == "stackoverflow.com" and reason not in GlobalVars.non_socvr_reasons and time.time() >= GlobalVars.blockedTime[GlobalVars.socvr_room_id]: socvr_pings = datahandling.get_user_names_on_notification_list("stackoverflow.com", GlobalVars.socvr_room_id, site, GlobalVars.wrapso) socvr_msg = prefix + s socvr_msg_pings = prefix + datahandling.append_pings(s, socvr_pings) socvr_msg_pings_ms = prefix_ms + datahandling.append_pings(s, socvr_pings) msg_to_send = socvr_msg_pings_ms if len(socvr_msg_pings_ms) <= 500 else socvr_msg_pings if len(socvr_msg_pings) <= 500 else socvr_msg[0:500] GlobalVars.socvr.send_message(msg_to_send) for specialroom in GlobalVars.specialrooms: sites = specialroom["sites"] if site in sites and reason not in specialroom["unwantedReasons"]: room = specialroom["room"] if room.id not in GlobalVars.blockedTime or time.time() >= GlobalVars.blockedTime[room.id]: room_site = room._client.host room_id = int(room.id) room_pings = datahandling.get_user_names_on_notification_list(room_site, room_id, site, room._client) room_msg = prefix + s room_msg_pings = prefix + datahandling.append_pings(s, room_pings) room_msg_pings_ms = prefix_ms + datahandling.append_pings(s, room_pings) msg_to_send = room_msg_pings_ms if len(room_msg_pings_ms) <= 500 else room_msg_pings if len(room_msg_pings) <= 500 else room_msg[0:500] specialroom["room"].send_message(msg_to_send) except: exc_type, exc_obj, exc_tb = sys.exc_info() excepthook.uncaught_exception(exc_type, exc_obj, exc_tb)
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)) reason = ", ".join(reasons[:5]) if len(reasons) > 5: reason += ", +{} more".format(len(reasons) - 5) reason = reason[:1].upper() + reason[1:] # reason is capitalised, unlike the entries of reasons list 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: post._title = parsing.escape_special_chars_in_title(post.title) if post.is_answer: # 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 = regex.sub('(https?://|\n)', '', post.parent.title) else: sanitized_title = regex.sub('(https?://|\n)', '', post.title) sanitized_title = regex.sub(r'([\]*`])', r'\\$1', sanitized_title).replace('\n', u'\u23CE') prefix = u"[ [SmokeDetector](//goo.gl/eLDYqh) ]" if GlobalVars.metasmoke_key: prefix_ms = u"[ [SmokeDetector](//goo.gl/eLDYqh) | [MS](//m.erwaysoftware.com/posts/by-url?url=" + \ post_url + ") ]" else: prefix_ms = prefix if not post.user_name.strip() or (not poster_url or poster_url.strip() == ""): s = u" {}: [{}]({}) by a deleted user on `{}`".format(reason, sanitized_title.strip(), post_url, shortened_site) username = "" else: s = u" {}: [{}]({}) by [{}]({}) on `{}`".format(reason, sanitized_title.strip(), post_url, post.user_name.strip(), poster_url, shortened_site) username = post.user_name.strip() t_metasmoke = Thread(name="metasmoke send post", target=metasmoke.Metasmoke.send_stats_on_post, args=(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)) t_metasmoke.start() log('debug', GlobalVars.parser.unescape(s).encode('ascii', errors='replace')) if time.time() >= GlobalVars.blockedTime["all"]: datahandling.append_to_latest_questions(post.post_site, post.post_id, post.title) if set(reasons).intersection(GlobalVars.experimental_reasons) != set(reasons): if time.time() >= GlobalVars.blockedTime[GlobalVars.charcoal_room_id]: chq_pings = datahandling.get_user_names_on_notification_list("stackexchange.com", GlobalVars.charcoal_room_id, post.post_site, GlobalVars.wrap) chq_msg = prefix + s chq_msg_pings = prefix + datahandling.append_pings(s, chq_pings) chq_msg_pings_ms = prefix_ms + datahandling.append_pings(s, chq_pings) msg_to_send = chq_msg_pings_ms if len(chq_msg_pings_ms) <= 500 else chq_msg_pings \ if len(chq_msg_pings) <= 500 else chq_msg[0:500] try: GlobalVars.charcoal_hq.send_message(msg_to_send) except AttributeError: # In our Test Suite pass if not should_reasons_prevent_tavern_posting(reasons) \ and post.post_site not in GlobalVars.non_tavern_sites \ and time.time() >= GlobalVars.blockedTime[GlobalVars.meta_tavern_room_id]: tavern_pings = datahandling.get_user_names_on_notification_list("meta.stackexchange.com", GlobalVars.meta_tavern_room_id, post.post_site, GlobalVars.wrapm) tavern_msg = prefix + s tavern_msg_pings = prefix + datahandling.append_pings(s, tavern_pings) tavern_msg_pings_ms = prefix_ms + datahandling.append_pings(s, tavern_pings) msg_to_send = tavern_msg_pings_ms if len(tavern_msg_pings_ms) <= 500 else tavern_msg_pings \ if len(tavern_msg_pings) <= 500 else tavern_msg[0:500] t_check_websocket = Thread(name="deletionwatcher post message if not deleted", target=deletionwatcher.DeletionWatcher.post_message_if_not_deleted, args=((post.post_id, post.post_site, "answer" if post.is_answer else "question"), post_url, msg_to_send, GlobalVars.tavern_on_the_meta)) t_check_websocket.daemon = True t_check_websocket.start() if post.post_site == "stackoverflow.com" and reason not in GlobalVars.non_socvr_reasons \ and time.time() >= GlobalVars.blockedTime[GlobalVars.socvr_room_id]: socvr_pings = datahandling.get_user_names_on_notification_list("stackoverflow.com", GlobalVars.socvr_room_id, post.post_site, GlobalVars.wrapso) socvr_msg = prefix + s socvr_msg_pings = prefix + datahandling.append_pings(s, socvr_pings) socvr_msg_pings_ms = prefix_ms + datahandling.append_pings(s, socvr_pings) msg_to_send = socvr_msg_pings_ms if len(socvr_msg_pings_ms) <= 500 else socvr_msg_pings \ if len(socvr_msg_pings) <= 500 else socvr_msg[0:500] try: GlobalVars.socvr.send_message(msg_to_send) except AttributeError: # In test Suite pass for specialroom in GlobalVars.specialrooms: sites = specialroom["sites"] if post.post_site in sites and reason not in specialroom["unwantedReasons"]: room = specialroom["room"] if room.id not in GlobalVars.blockedTime or time.time() >= GlobalVars.blockedTime[room.id]: room_site = room._client.host room_id = int(room.id) room_pings = datahandling.get_user_names_on_notification_list(room_site, room_id, post.post_site, room._client) room_msg = prefix + s room_msg_pings = prefix + datahandling.append_pings(s, room_pings) room_msg_pings_ms = prefix_ms + datahandling.append_pings(s, room_pings) msg_to_send = room_msg_pings_ms if len(room_msg_pings_ms) <= 500 else room_msg_pings \ if len(room_msg_pings) <= 500 else room_msg[0:500] specialroom["room"].send_message(msg_to_send) except: exc_type, exc_obj, exc_tb = sys.exc_info() excepthook.uncaught_exception(exc_type, exc_obj, exc_tb)
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)) reason = ", ".join(reasons[:5]) if len(reasons) > 5: reason += ", +{} more".format(len(reasons) - 5) reason = reason[:1].upper() + reason[ 1:] # reason is capitalised, unlike the entries of reasons list 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: post._title = parsing.escape_special_chars_in_title(post.title) if post.is_answer: # 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 = regex.sub('(https?://|\n)', '', post.parent.title) else: sanitized_title = regex.sub('(https?://|\n)', '', post.title) sanitized_title = regex.sub(r'([\]*`])', r'\\$1', sanitized_title).replace('\n', u'\u23CE') prefix = u"[ [SmokeDetector](//goo.gl/eLDYqh) ]" if GlobalVars.metasmoke_key: prefix_ms = u"[ [SmokeDetector](//goo.gl/eLDYqh) | [MS](//m.erwaysoftware.com/posts/by-url?url=" + \ post_url + ") ]" else: prefix_ms = prefix if not post.user_name.strip() or (not poster_url or poster_url.strip() == ""): s = u" {}: [{}]({}) by a deleted user on `{}`".format( reason, sanitized_title.strip(), post_url, shortened_site) username = "" else: s = u" {}: [{}]({}) by [{}]({}) on `{}`".format( reason, sanitized_title.strip(), post_url, post.user_name.strip(), poster_url, shortened_site) username = post.user_name.strip() t_metasmoke = Thread(name="metasmoke send post", target=metasmoke.Metasmoke.send_stats_on_post, args=(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)) t_metasmoke.start() log('debug', GlobalVars.parser.unescape(s).encode('ascii', errors='replace')) if time.time() >= GlobalVars.blockedTime["all"]: datahandling.append_to_latest_questions(post.post_site, post.post_id, post.title) if set(reasons).intersection( GlobalVars.experimental_reasons) != set(reasons): if time.time() >= GlobalVars.blockedTime[ GlobalVars.charcoal_room_id]: chq_pings = datahandling.get_user_names_on_notification_list( "stackexchange.com", GlobalVars.charcoal_room_id, post.post_site, GlobalVars.wrap) chq_msg = prefix + s chq_msg_pings = prefix + datahandling.append_pings( s, chq_pings) chq_msg_pings_ms = prefix_ms + datahandling.append_pings( s, chq_pings) msg_to_send = chq_msg_pings_ms if len(chq_msg_pings_ms) <= 500 else chq_msg_pings \ if len(chq_msg_pings) <= 500 else chq_msg[0:500] try: GlobalVars.charcoal_hq.send_message(msg_to_send) except AttributeError: # In our Test Suite pass if not should_reasons_prevent_tavern_posting(reasons) \ and post.post_site not in GlobalVars.non_tavern_sites \ and time.time() >= GlobalVars.blockedTime[GlobalVars.meta_tavern_room_id]: tavern_pings = datahandling.get_user_names_on_notification_list( "meta.stackexchange.com", GlobalVars.meta_tavern_room_id, post.post_site, GlobalVars.wrapm) tavern_msg = prefix + s tavern_msg_pings = prefix + datahandling.append_pings( s, tavern_pings) tavern_msg_pings_ms = prefix_ms + datahandling.append_pings( s, tavern_pings) msg_to_send = tavern_msg_pings_ms if len(tavern_msg_pings_ms) <= 500 else tavern_msg_pings \ if len(tavern_msg_pings) <= 500 else tavern_msg[0:500] t_check_websocket = Thread( name="deletionwatcher post message if not deleted", target=deletionwatcher.DeletionWatcher. post_message_if_not_deleted, args=((post.post_id, post.post_site, "answer" if post.is_answer else "question"), post_url, msg_to_send, GlobalVars.tavern_on_the_meta)) t_check_websocket.daemon = True t_check_websocket.start() if post.post_site == "stackoverflow.com" and reason not in GlobalVars.non_socvr_reasons \ and time.time() >= GlobalVars.blockedTime[GlobalVars.socvr_room_id]: socvr_pings = datahandling.get_user_names_on_notification_list( "stackoverflow.com", GlobalVars.socvr_room_id, post.post_site, GlobalVars.wrapso) socvr_msg = prefix + s socvr_msg_pings = prefix + datahandling.append_pings( s, socvr_pings) socvr_msg_pings_ms = prefix_ms + datahandling.append_pings( s, socvr_pings) msg_to_send = socvr_msg_pings_ms if len(socvr_msg_pings_ms) <= 500 else socvr_msg_pings \ if len(socvr_msg_pings) <= 500 else socvr_msg[0:500] try: GlobalVars.socvr.send_message(msg_to_send) except AttributeError: # In test Suite pass for specialroom in GlobalVars.specialrooms: sites = specialroom["sites"] if post.post_site in sites and reason not in specialroom[ "unwantedReasons"]: room = specialroom["room"] if room.id not in GlobalVars.blockedTime or time.time( ) >= GlobalVars.blockedTime[room.id]: room_site = room._client.host room_id = int(room.id) room_pings = datahandling.get_user_names_on_notification_list( room_site, room_id, post.post_site, room._client) room_msg = prefix + s room_msg_pings = prefix + datahandling.append_pings( s, room_pings) room_msg_pings_ms = prefix_ms + datahandling.append_pings( s, room_pings) msg_to_send = room_msg_pings_ms if len(room_msg_pings_ms) <= 500 else room_msg_pings \ if len(room_msg_pings) <= 500 else room_msg[0:500] specialroom["room"].send_message(msg_to_send) except: exc_type, exc_obj, exc_tb = sys.exc_info() excepthook.uncaught_exception(exc_type, exc_obj, exc_tb)
def handle_spam(title, body, poster, site, post_url, poster_url, post_id, reasons, is_answer, why="", owner_rep=None, post_score=None, up_vote_count=None, down_vote_count=None): post_url = to_protocol_relative(url_to_shortlink(post_url)) poster_url = to_protocol_relative(poster_url) reason = ", ".join(reasons) reason = reason[:1].upper() + reason[ 1:] # reason is capitalised, unlike the entries of reasons list append_to_latest_questions(site, post_id, title if not 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): add_auto_ignored_post((post_id, site, datetime.now())) if why is not None and why != "": add_why(site, post_id, why) try: title = escape_special_chars_in_title(title) if not poster.strip(): s = u"[ [SmokeDetector](https://github.com/Charcoal-SE/SmokeDetector) ] {}: [{}]({}) by a deleted user on `{}`" \ .format(reason, title.strip(), post_url, site) username = "" user_link = "" else: s = u"[ [SmokeDetector](https://github.com/Charcoal-SE/SmokeDetector) ] {}: [{}]({}) by [{}]({}) on `{}`" \ .format(reason, title.strip(), post_url, poster.strip(), poster_url + "?tab=topactivity", site) username = poster.strip() user_link = poster_url t_metasmoke = Thread(target=Metasmoke.send_stats_on_post, args=(title, post_url, reason.split(", "), body, username, user_link, why, owner_rep, post_score, up_vote_count, down_vote_count)) t_metasmoke.start() print GlobalVars.parser.unescape(s).encode('ascii', errors='replace') if time.time() >= GlobalVars.blockedTime: append_to_latest_questions(site, post_id, title) if reason not in GlobalVars.experimental_reasons: chq_pings = get_user_names_on_notification_list( "stackexchange.com", GlobalVars.charcoal_room_id, site, GlobalVars.wrap) chq_msg = append_pings(s, chq_pings) GlobalVars.charcoal_hq.send_message( chq_msg if len(chq_msg) <= 500 else s) if reason not in GlobalVars.non_tavern_reasons and site not in GlobalVars.non_tavern_sites: tavern_pings = get_user_names_on_notification_list( "meta.stackexchange.com", GlobalVars.meta_tavern_room_id, site, GlobalVars.wrapm) tavern_msg = append_pings(s, tavern_pings) GlobalVars.tavern_on_the_meta.send_message( tavern_msg if len(tavern_msg) <= 500 else s) if site == "stackoverflow.com": socvr_pings = get_user_names_on_notification_list( "stackoverflow.com", GlobalVars.socvr_room_id, site, GlobalVars.wrapso) socvr_msg = append_pings(s, socvr_pings) GlobalVars.socvr.send_message( socvr_msg if len(socvr_msg) <= 500 else s) for specialroom in GlobalVars.specialrooms: sites = specialroom["sites"] if site in sites and reason not in specialroom[ "unwantedReasons"]: room = specialroom["room"] room_site = room._client.host room_id = int(room.id) room_pings = get_user_names_on_notification_list( room_site, room_id, site, room._client) room_msg = append_pings(s, room_pings) specialroom["room"].send_message( room_msg if len(room_msg) <= 500 else s) except: exc_type, exc_obj, exc_tb = sys.exc_info() excepthook.uncaught_exception(exc_type, exc_obj, exc_tb)
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 handle_spam(title, body, poster, site, post_url, poster_url, post_id, reasons, is_answer, why="", owner_rep=None, post_score=None, up_vote_count=None, down_vote_count=None, question_id=None): post_url = parsing.to_protocol_relative(parsing.url_to_shortlink(post_url)) poster_url = parsing.to_protocol_relative( parsing.user_url_to_shortlink(poster_url)) reason = ", ".join(reasons[:5]) if len(reasons) > 5: reason += ", +{} more".format(len(reasons) - 5) reason = reason[:1].upper() + reason[ 1:] # reason is capitalised, unlike the entries of reasons list shortened_site = site.replace("stackexchange.com", "SE") # site.stackexchange.com -> site.SE datahandling.append_to_latest_questions(site, post_id, title if not 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_id, site, datetime.now())) if why is not None and why != "": datahandling.add_why(site, post_id, why) if is_answer and question_id is not None: datahandling.add_post_site_id_link((post_id, site, "answer"), question_id) try: title = parsing.escape_special_chars_in_title(title) sanitized_title = regex.sub('(https?://|\n)', '', title) prefix = u"[ [SmokeDetector](//goo.gl/eLDYqh) ]" if GlobalVars.metasmoke_key: prefix_ms = u"[ [SmokeDetector](//goo.gl/eLDYqh) | [MS](//m.erwaysoftware.com/posts/by-url?url=" + \ post_url + ") ]" else: prefix_ms = prefix if not poster.strip(): s = u" {}: [{}]({}) by a deleted user on `{}`".format( reason, sanitized_title.strip(), post_url, shortened_site) username = "" user_link = "" else: s = u" {}: [{}]({}) by [{}]({}) on `{}`".format( reason, sanitized_title.strip(), post_url, poster.strip(), poster_url, shortened_site) username = poster.strip() user_link = poster_url t_metasmoke = Thread(name="metasmoke send post", target=metasmoke.Metasmoke.send_stats_on_post, args=(title, post_url, reasons, body, username, user_link, why, owner_rep, post_score, up_vote_count, down_vote_count)) t_metasmoke.start() print GlobalVars.parser.unescape(s).encode('ascii', errors='replace') if time.time() >= GlobalVars.blockedTime["all"]: datahandling.append_to_latest_questions(site, post_id, title) if reason not in GlobalVars.experimental_reasons: if time.time() >= GlobalVars.blockedTime[ GlobalVars.charcoal_room_id]: chq_pings = datahandling.get_user_names_on_notification_list( "stackexchange.com", GlobalVars.charcoal_room_id, site, GlobalVars.wrap) chq_msg = prefix + s chq_msg_pings = prefix + datahandling.append_pings( s, chq_pings) chq_msg_pings_ms = prefix_ms + datahandling.append_pings( s, chq_pings) msg_to_send = chq_msg_pings_ms if len(chq_msg_pings_ms) <= 500 else chq_msg_pings \ if len(chq_msg_pings) <= 500 else chq_msg[0:500] GlobalVars.charcoal_hq.send_message(msg_to_send) if not should_reasons_prevent_tavern_posting(reasons) and site not in GlobalVars.non_tavern_sites \ and time.time() >= GlobalVars.blockedTime[GlobalVars.meta_tavern_room_id]: tavern_pings = datahandling.get_user_names_on_notification_list( "meta.stackexchange.com", GlobalVars.meta_tavern_room_id, site, GlobalVars.wrapm) tavern_msg = prefix + s tavern_msg_pings = prefix + datahandling.append_pings( s, tavern_pings) tavern_msg_pings_ms = prefix_ms + datahandling.append_pings( s, tavern_pings) msg_to_send = tavern_msg_pings_ms if len(tavern_msg_pings_ms) <= 500 else tavern_msg_pings \ if len(tavern_msg_pings) <= 500 else tavern_msg[0:500] t_check_websocket = Thread( name="deletionwatcher post message if not deleted", target=deletionwatcher.DeletionWatcher. post_message_if_not_deleted, args=((post_id, site, "answer" if is_answer else "question"), post_url, msg_to_send, GlobalVars.tavern_on_the_meta)) t_check_websocket.daemon = True t_check_websocket.start() if site == "stackoverflow.com" and reason not in GlobalVars.non_socvr_reasons \ and time.time() >= GlobalVars.blockedTime[GlobalVars.socvr_room_id]: socvr_pings = datahandling.get_user_names_on_notification_list( "stackoverflow.com", GlobalVars.socvr_room_id, site, GlobalVars.wrapso) socvr_msg = prefix + s socvr_msg_pings = prefix + datahandling.append_pings( s, socvr_pings) socvr_msg_pings_ms = prefix_ms + datahandling.append_pings( s, socvr_pings) msg_to_send = socvr_msg_pings_ms if len(socvr_msg_pings_ms) <= 500 else socvr_msg_pings \ if len(socvr_msg_pings) <= 500 else socvr_msg[0:500] GlobalVars.socvr.send_message(msg_to_send) for specialroom in GlobalVars.specialrooms: sites = specialroom["sites"] if site in sites and reason not in specialroom[ "unwantedReasons"]: room = specialroom["room"] if room.id not in GlobalVars.blockedTime or time.time( ) >= GlobalVars.blockedTime[room.id]: room_site = room._client.host room_id = int(room.id) room_pings = datahandling.get_user_names_on_notification_list( room_site, room_id, site, room._client) room_msg = prefix + s room_msg_pings = prefix + datahandling.append_pings( s, room_pings) room_msg_pings_ms = prefix_ms + datahandling.append_pings( s, room_pings) msg_to_send = room_msg_pings_ms if len(room_msg_pings_ms) <= 500 else room_msg_pings \ if len(room_msg_pings) <= 500 else room_msg[0:500] specialroom["room"].send_message(msg_to_send) except: exc_type, exc_obj, exc_tb = sys.exc_info() excepthook.uncaught_exception(exc_type, exc_obj, exc_tb)
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) if GlobalVars.reason_weights or GlobalVars.metasmoke_key: reason_weight = sum_weight(reasons) if reason_weight >= 1000: reason_weight_s = " (**{}**)".format(reason_weight) else: reason_weight_s = " ({})".format(reason_weight) else: # No reason weight if neither cache nor MS reason_weight_s = "" 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) sanitized_title = escape_format(sanitized_title).strip() prefix = u"[ [SmokeDetector](//git.io/vyDZv) ]" if GlobalVars.metasmoke_key: prefix_ms = u"[ [SmokeDetector](//git.io/vyDZv) | [MS]({}) ]".format( to_metasmoke_link(post_url, protocol=False)) else: prefix_ms = prefix # We'll insert reason list later edited = '' if not post.edited else ' \u270F\uFE0F' if not post.user_name.strip() or (not poster_url or poster_url.strip() == ""): s = " {{}}{}: [{}]({}){} by a deleted user on `{}`".format( reason_weight_s, sanitized_title, post_url, edited, shortened_site) username = "" else: username = post.user_name.strip() escaped_username = escape_format(parsing.escape_markdown(username)) s = " {{}}{}: [{}]({}){} by [{}]({}) on `{}`".format( reason_weight_s, sanitized_title, post_url, edited, escaped_username, poster_url, shortened_site) 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, 0, -1): # Try 5 reasons and all the way down to 1 reason = ", ".join(reasons[:reason_count]) if len(reasons) > reason_count: reason += ", +{} more".format(len(reasons) - reason_count) reason = reason.capitalize() 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() and \ not why.startswith("Post manually "): 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 Exception as e: excepthook.uncaught_exception(*sys.exc_info())
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)) reason = ", ".join(reasons[:5]) if len(reasons) > 5: reason += ", +{} more".format(len(reasons) - 5) reason = reason[:1].upper() + reason[ 1:] # reason is capitalised, unlike the entries of reasons list 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: post._title = parsing.escape_special_chars_in_title(post.title) if post.is_answer: # 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 = regex.sub('(https?://|\n)', '', post.parent.title) else: sanitized_title = regex.sub('(https?://|\n)', '', post.title) sanitized_title = regex.sub(r'([\]*`])', r'\\\1', sanitized_title).replace('\n', u'\u23CE') prefix = u"[ [SmokeDetector](//goo.gl/eLDYqh) ]" if GlobalVars.metasmoke_key: prefix_ms = u"[ [SmokeDetector](//goo.gl/eLDYqh) | [MS](//m.erwaysoftware.com/posts/by-url?url=" + \ post_url + ") ]" else: prefix_ms = prefix if not post.user_name.strip() or (not poster_url or poster_url.strip() == ""): s = u" {}: [{}]({}) by a deleted user on `{}`".format( reason, sanitized_title.strip(), post_url, shortened_site) username = "" else: s = u" {}: [{}]({}) by [{}]({}) on `{}`".format( reason, sanitized_title.strip(), 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')) datahandling.append_to_latest_questions(post.post_site, post.post_id, post.title) message = prefix_ms + s if len(message) > 500: message = (prefix + s)[:500] without_roles = tuple( "no-" + reason for reason in reasons) + ("site-no-" + post.post_site, ) if set(reason) & GlobalVars.experimental_reasons == {}: 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 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)) reason = ", ".join(reasons[:5]) if len(reasons) > 5: reason += ", +{} more".format(len(reasons) - 5) reason = reason[:1].upper() + reason[1:] # reason is capitalised, unlike the entries of reasons list 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: post._title = parsing.escape_special_chars_in_title(post.title) if post.is_answer: # 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 = regex.sub('(https?://|\n)', '', post.parent.title) else: sanitized_title = regex.sub('(https?://|\n)', '', post.title) sanitized_title = regex.sub(r'([\]*`])', r'\\\1', sanitized_title).replace('\n', u'\u23CE') prefix = u"[ [SmokeDetector](//goo.gl/eLDYqh) ]" if GlobalVars.metasmoke_key: prefix_ms = u"[ [SmokeDetector](//goo.gl/eLDYqh) | [MS](//m.erwaysoftware.com/posts/by-url?url=" + \ post_url + ") ]" else: prefix_ms = prefix if not post.user_name.strip() or (not poster_url or poster_url.strip() == ""): s = u" {}: [{}]({}) by a deleted user on `{}`".format(reason, sanitized_title.strip(), post_url, shortened_site) username = "" else: s = u" {}: [{}]({}) by [{}]({}) on `{}`".format(reason, sanitized_title.strip(), 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')) datahandling.append_to_latest_questions(post.post_site, post.post_id, post.title) message = prefix_ms + s if len(message) > 500: message = (prefix + s)[:500] without_roles = tuple("no-" + reason for reason in reasons) if set(reason) & GlobalVars.experimental_reasons == {}: 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)