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 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 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 article(request, article_id): a = get_object_or_404(Article, pk=article_id) # Check if the article is unpublished, and only show it to people with required permissions if # this turns out to be the case. if not a.published and not request.user.has_perm('mesoblog.change_article'): return HttpResponseForbidden() # Handle comment form submission if request.method == 'POST': if a.comments_open() is not True: messages.add_message(request, messages.ERROR, "Comments are now closed on this article.") else: f = CommentForm(request.POST) if f.is_valid(): ak = Akismet(blog_url=request.get_host(), api_key=settings.AKISMET_API_KEY, user_agent="Mesosphere/0.0.1") comment = f.save(commit=False) ak_dict = {'user_ip': request.META['REMOTE_ADDR'], 'user_agent': request.META['HTTP_USER_AGENT'], 'referrer': request.META['HTTP_REFERER'], 'comment_content': f.cleaned_data['contents'], 'comment_author': f.cleaned_data['name'], } if settings.DEBUG is True: ak_dict['is_test'] = 1 comment.is_spam = ak.check(ak_dict) comment.user_ip = request.META['REMOTE_ADDR'] comment.user_agent = request.META['HTTP_USER_AGENT'] comment.referer = request.META['HTTP_REFERER'] comment.save() messages.add_message(request, messages.SUCCESS, 'Your comment has been posted successfully.') f = CommentForm(instance=Comment(article=a)) else: messages.add_message(request, messages.ERROR, 'Your comment was not posted successfully.') else: f = CommentForm(instance=Comment(article=a)) # Construct Breadcrumbs b = [ Breadcrumb(name="Home",url=reverse('mesohome.views.index')), Breadcrumb(name="Blog",url=reverse('mesoblog.views.index')), Breadcrumb(name=a.primary_category.name,url=reverse('mesoblog.views.categoryFromSlug', args=(a.primary_category.slug,))), Breadcrumb(name=a.title) ] context = RequestContext(request, { 'article': a, 'breadcrumbs': b, 'comment_form': f, 'comments_open': a.comments_open(), }) return render(request, 'mesoblog/article.html', context_instance=context)
class PykismetTestCase(unittest.TestCase): # create an instance of the Akismet class def setUp(self): self.akismet_client = Akismet(blog_url="http://your.blog/url", user_agent="My Awesome Web App/0.0.1", api_key="testkey") # test that class raises an ExtraParametersError when extra params are passed def test_extra_parameters(self): params = {'blog': 'http://your.blog/url', 'user_ip': '1.1.1.1', 'user_agent': 'My Awesome Web App/0.0.1', 'referrer': 'http://your.blog/url', 'some_extra': 'extra' } with self.assertRaises(ExtraParametersError): self.akismet_client.check(params)
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 Zoolander 2 Full Movie http://sendalgiceng21.blogspot.com/2016/03/zoolsnder-2-full-movie.html Pirattes Of The Caribean {2017} http://sendalgiceng21.blogspot.com/2016/03/pirattes-of-caribean-2017.html Barbershoop http://sendalgiceng21.blogspot.com/2016/03/barbershoop.html The Jungle Book http://sendalgiceng21.blogspot.com/2016/03/the-jungle-book.html Warcraft The Movie http://sendalgiceng21.blogspot.com/2016/03/warcraft-movie.html Creed http://sendalgiceng21.blogspot.com/2016/03/creed.html Criminal http://sendalgiceng21.blogspot.com/2016/03/criminal.html Daredevil http://sendalgiceng21.blogspot.com/2016/03/daredevil.html Dead 7 http://sendalgiceng21.blogspot.com/2016/03/dead-7.html Fast 8 New Roads Ahead http://sendalgiceng21.blogspot.com/2016/03/fast-8-new-roads-ahead.html Gods Of Egypt http://sendalgiceng21.blogspot.com/2016/03/gods-of-egypt.html Guardian Of The Galaxy http://sendalgiceng21.blogspot.com/2016/03/guardian-of-galaxy.html Fifty Shades of Black http://sendalgiceng21.blogspot.com/2016/03/fifty-shades-of-black.html Jack Reacher http://sendalgiceng21.blogspot.com/2016/03/jack-reacher.html Mechanic http://sendalgiceng21.blogspot.com/2016/03/mechanic.html #100% free.. enjoy youre time.. just do it!!""", 'comment_author': 'Wicky', 'is_test': 1, }))