1+ #!/usr/bin/python
2+ #coding=utf-8
3+
4+ from __future__ import unicode_literals
5+ try :
6+ from urllib .request import urlopen , Request
7+ from urllib .parse import urlparse
8+ from urllib .parse import urlencode
9+
10+ except ImportError :
11+ from urllib2 import urlopen , Request
12+ from urlparse import urlparse
13+ from urllib import urlencode
14+
15+ import json
16+
17+ class CleanTalk :
18+ """Python API for CleanTalk.org"""
19+ VERSION = 1.0
20+ ENCODING = 'utf-8'
21+ user_agent = 'Mozilla/5.0'
22+
23+ def __init__ (self ,
24+ auth_key ,
25+ server_url = 'http://moderate.cleantalk.ru' ,
26+ api_url = '/api2.0' ,
27+ connection_timeout = 3 ,
28+ method_name = 'check_message' ):
29+
30+ """
31+ This method constructs a new CleanTalk object and returns it.
32+ :param auth_key:
33+ :param server_url:
34+ :param api_url:
35+ :param connection_timeout:
36+ :param method_name:
37+ """
38+ self .__server_url = server_url
39+ self .__api_url = api_url
40+ self .__connection_timeout = connection_timeout
41+ self .__method_name = method_name
42+ self .__auth_key = auth_key
43+
44+ def request (self , message , sender_ip , sender_email , sender_nickname , submit_time , js_on , example = '' , method_name = None ):
45+ """
46+ This method will dispatch call to servers.
47+ Exceptions can be raised: ValueError on bad json, URLError on bad url, HTTPError, HTTPException on http-error
48+ :param message: Comment visitor to the site
49+ :param example: The text of the article to which visitor created a comment
50+ :param sender_ip: IP address of the visitor
51+ :param sender_email: Email IP of the visitor
52+ :param sender_nickname: Nickname of the visitor
53+ :param submit_time: The time taken to fill the comment form in seconds
54+ :param js_on: The presence of JavaScript for the site visitor, 0|1
55+ :param method_name:
56+ :return: dictionary, where:
57+ KEY VALUE
58+ ----------- --------------------
59+ allow 0|1 - spam or not comment/registration
60+ id MD5_HEX - unique request ID
61+ comment string - description about request from server
62+ stop_queue 0|1 - should comment move to site's moderation queue or not
63+ inactive 0|1 - should registration move to inactive state or not
64+ """
65+ if not method_name :
66+ method_name = self .__method_name
67+
68+ url = self .__server_url + self .__api_url
69+ headers = { 'User-Agent' : self .user_agent , 'content-type' :'application/json; charset=' + CleanTalk .ENCODING }
70+
71+ values = {
72+ 'auth_key' : self .__auth_key ,
73+ 'method_name' : method_name ,
74+ 'message' : message ,
75+ 'example' : example ,
76+ 'sender_ip' : sender_ip ,
77+ 'sender_email' : sender_email ,
78+ 'sender_nickname' : sender_nickname ,
79+ 'submit_time' : submit_time ,
80+ 'js_on' : js_on
81+ }
82+ data = json .dumps (values , separators = (',' ,':' ))
83+ request = Request (url , data .encode (CleanTalk .ENCODING ), headers )
84+ response = urlopen (request , timeout = self .__connection_timeout )
85+ response_bytes = response .read ()
86+ response_str = response_bytes .decode (CleanTalk .ENCODING )
87+ response_parsed = json .loads (response_str )
88+
89+ return response_parsed
0 commit comments