def clean_body(self): """ Check spam against Akismet. Backported from django-contact-form pre-1.0; 1.0 dropped built-in Akismet support. """ if 'body' in self.cleaned_data and getattr(settings, 'AKISMET_API_KEY', None): try: akismet_api = Akismet( api_key=settings.AKISMET_API_KEY, blog_url='http://%s/' % Site.objects.get_current().domain, user_agent='Django {}.{}.{}'.format(*django.VERSION)) akismet_data = { 'user_ip': self.request.META.get('REMOTE_ADDR', ''), 'user_agent': self.request.META.get('HTTP_USER_AGENT', ''), 'referrer': self.request.META.get('HTTP_REFERER', ''), 'comment_content': force_bytes(self.cleaned_data['body']), 'comment_author': self.cleaned_data['name'], } if getattr(settings, 'AKISMET_TESTING', None): # Adding test argument to the request in order to tell akismet that # they should ignore the request so that test runs affect the heuristics akismet_data['test'] = 1 if akismet_api.check(akismet_data): raise forms.ValidationError( "Akismet thinks this message is spam") except AkismetServerError: logger.error('Akismet server error') return self.cleaned_data['body']
def is_spam(request): from pykismet3 import Akismet import os a = Akismet(blog_url="http://wiseinit.com", user_agent="WiseInit System/0.0.1") a.api_key = get_setting('akismet') check = { 'user_ip': request.META['REMOTE_ADDR'], 'user_agent': request.META['HTTP_USER_AGENT'], 'referrer': request.META.get('HTTP_REFERER', 'unknown'), 'comment_content': request.POST['comment'], 'comment_author': request.POST['name'], } if request.POST['email'].strip(): check['comment_author_email'] = request.POST['email'] if request.POST['website'].strip(): website = request.POST['website'].strip() if website and not re.match('https?://.+', website): website = 'http://' + website check['comment_author_url'] = website return a.check(check)
def mark_it_spam(comment_id): comment = Comment.query.filter_by( id=comment_id, mark_for_delete=False).first() if comment is None: response = make_response( jsonify({'not found': 'No such comment'}), 404) else: a = Akismet(blog_url=comment.post_url, user_agent=comment.user_agent) a.api_key = app.config['AKISMET_API_KEY'] a.submit_spam({'user_ip': comment.user_ip, 'user_agent': comment.user_agent, 'referrer': comment.referrer, 'comment_type': comment.comment_type, 'comment_author': comment.comment_author, 'comment_author_email': comment.comment_author_email, 'comment_content': comment.comment_content, 'website': comment.website }) comment.mark_for_delete = True db.session.commit() app.logger.info("The comment was submitted as spam") response = make_response( jsonify({'success': 'The comment was submitted as spam'}), 200) return response
def spam_check(user_ip, user_agent, referrer, name, email, message, \ website, post_url): comment_type = "comment" akismet_api_key = app.config['AKISMET_API_KEY'] if not akismet_api_key: app.logger.info( "Required environment variable AKISMET_API_KEY missing") return (False, None) a = Akismet(blog_url=post_url, api_key=akismet_api_key, user_agent=user_agent) try: is_spam = a.check({'user_ip': user_ip, 'user_agent': user_agent, 'referrer': referrer, 'comment_type': comment_type, 'comment_author': name, 'comment_author_email': email, 'comment_content': message, 'website': website }) except AkismetError as e: app.logger.info( "Error in call to pykismet3.check(): {}".format(str(e))) return (False, None) return (True, is_spam)
def mark_it_valid(comment_id): comment = Comment.query.filter_by( id=comment_id, mark_for_delete=False).first() if comment is None: response = make_response( jsonify({'not found': 'No such comment'}), 404) else: a = Akismet(blog_url=comment.post_url, user_agent=comment.user_agent) a.api_key = app.config['AKISMET_API_KEY'] a.submit_ham({'user_ip': comment.user_ip, 'user_agent': comment.user_agent, 'referrer': comment.referrer, 'comment_type': comment.comment_type, 'comment_author': comment.comment_author, 'comment_author_email': comment.comment_author_email, 'comment_content': comment.comment_content, 'website': comment.website }) date_str = get_current_datetime_str(comment.submit_datetime) name = comment.comment_author email = comment.comment_author_email website_value = comment.website message = comment.comment_content slug = comment.slug github_token = app.config['GITHUB_TOKEN'] github_username = app.config['GITHUB_USERNAME'] github_repo_name = app.config['GITHUB_REPO_NAME'] if not create_github_pull_request(github_token, github_username, \ github_repo_name, slug, name, message, date_str, \ email, website_value): app.logger.info("Problem encountered during creation of pull request") response = make_response(jsonify({'error': 'Internal Error'}), 500) else: app.logger.info("Pull request created successfully") comment.mark_for_delete = True db.session.commit() response = make_response( jsonify({'success': 'The comment was submitted as valid'}), 200) return response
# -*- coding: utf-8 -*- from pykismet3 import Akismet import threading api_key = 'd7719929047a' blog_url = 'http://www.iloxp.com/' akismet = Akismet(api_key, blog_url, user_agent='None') def spamcheck(obj, referrer): comment_type = 'comment' if obj.parent: comment_type = 'reply' verify = akismet.check({ 'referrer': referrer, 'comment_type': comment_type, 'user_ip': obj.author_ip, 'user_agent': obj.agent, 'comment_content': obj.content, 'comment_author_email': obj.author_email, 'comment_author': obj.author_name, 'is_test': 1, }) if verify: obj.approved = 'spam' else: obj.approved = 'yes' obj.save() return obj.approved
#!/usr/bin/env python # -*- coding: utf-8 -*- import os from pykismet3 import Akismet from config.db import Config config = Config('akismet') a = Akismet(blog_url="", user_agent="Testing/Testing") a.api_key = config['api_key'] print( a.check({ 'user_ip': '127.0.0.1', 'user_agent': 'Mozilla/Firefox', 'referrer': 'unknown', 'comment_content': """Batman V Superman : Dawn of Justice http://sendalgiceng21.blogspot.com/2016/03/batman-v-superman-dawn-of-justice.html Captain America Civil War http://sendalgiceng21.blogspot.com/2016/03/captain-america-civil-war.html XXX : The Return Of Xander Cage http://sendalgiceng21.blogspot.com/2016/03/xxx-return-of-xander-cage.html Deadpool http://sendalgiceng21.blogspot.com/2016/03/ddeadpool.html
from pykismet3 import Akismet # code is wrong # defaultagent = 'akismettest python script from the Collective Intelligence book' defaultagent = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_5; en-us) ' defaultagent += 'AppleWebKit/525.27.1 (KHTML, like Gecko) ' defaultagent += 'Version/3.2.1 Safari/525.27.1' a = Akismet(blog_url='moneydboat.top', user_agent=defaultagent) a.api_key = 'e467cda4de57' def isspam(comment, author, ip, agent=defaultagent): try: result = a.check({ 'user_ip': ip, 'user_agent': agent, 'referrer': 'unknown', 'comment_content': comment, 'comment_author': author, 'is_test': 1, }) print(result) except Akismet.AkismetError as e: print(e.response, e.statuscode) return False