OLD | NEW |
1 # This file is part of the Adblock Plus web scripts, | 1 # This file is part of the Adblock Plus web scripts, |
2 # Copyright (C) 2006-2016 Eyeo GmbH | 2 # Copyright (C) 2006-2016 Eyeo GmbH |
3 # | 3 # |
4 # Adblock Plus is free software: you can redistribute it and/or modify | 4 # Adblock Plus is free software: you can redistribute it and/or modify |
5 # it under the terms of the GNU General Public License version 3 as | 5 # it under the terms of the GNU General Public License version 3 as |
6 # published by the Free Software Foundation. | 6 # published by the Free Software Foundation. |
7 # | 7 # |
8 # Adblock Plus is distributed in the hope that it will be useful, | 8 # Adblock Plus is distributed in the hope that it will be useful, |
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of | 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 # GNU General Public License for more details. | 11 # GNU General Public License for more details. |
12 # | 12 # |
13 # You should have received a copy of the GNU General Public License | 13 # You should have received a copy of the GNU General Public License |
14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
15 | 15 |
16 import os | 16 import os |
17 import MySQLdb | 17 import MySQLdb |
18 import json | 18 import json |
19 from urlparse import parse_qs | 19 from urlparse import parse_qs |
20 from sitescripts.web import url_handler | 20 from sitescripts.web import url_handler |
21 from sitescripts.utils import cached, get_config, setupStderr | 21 from sitescripts.utils import cached, get_config, setupStderr |
22 | 22 |
23 | 23 |
24 @url_handler("/submitData") | 24 @url_handler('/submitData') |
25 def submit_data(environ, start_response): | 25 def submit_data(environ, start_response): |
26 setupStderr(environ["wsgi.errors"]) | 26 setupStderr(environ['wsgi.errors']) |
27 | 27 |
28 if environ["REQUEST_METHOD"].upper() != "POST": | 28 if environ['REQUEST_METHOD'].upper() != 'POST': |
29 return showError("Unsupported request method", start_response) | 29 return showError('Unsupported request method', start_response) |
30 | 30 |
31 params = parse_qs(environ.get("QUERY_STRING", "")) | 31 params = parse_qs(environ.get('QUERY_STRING', '')) |
32 requestVersion = params.get("version", ["0"])[0] | 32 requestVersion = params.get('version', ['0'])[0] |
33 data = "{}" | 33 data = '{}' |
34 try: | 34 try: |
35 data_length = int(environ.get("CONTENT_LENGTH", "0")) | 35 data_length = int(environ.get('CONTENT_LENGTH', '0')) |
36 except ValueError: | 36 except ValueError: |
37 data_length = 0 | 37 data_length = 0 |
38 if data_length != 0: | 38 if data_length != 0: |
39 data = environ["wsgi.input"].read(data_length) | 39 data = environ['wsgi.input'].read(data_length) |
40 try: | 40 try: |
41 data = json.loads(data) | 41 data = json.loads(data) |
42 except json.decoder.JSONDecodeError: | 42 except json.decoder.JSONDecodeError: |
43 return showError("Error while parsing JSON data.", start_response) | 43 return showError('Error while parsing JSON data.', start_response) |
44 | 44 |
45 db = _get_db() | 45 db = _get_db() |
46 | 46 |
47 for domain, status in data.iteritems(): | 47 for domain, status in data.iteritems(): |
48 process_domain(db, domain, status) | 48 process_domain(db, domain, status) |
49 | 49 |
50 db.commit() | 50 db.commit() |
51 | 51 |
52 response_headers = [("Content-type", "text/plain")] | 52 response_headers = [('Content-type', 'text/plain')] |
53 start_response("200 OK", response_headers) | 53 start_response('200 OK', response_headers) |
54 return [] | 54 return [] |
55 | 55 |
56 | 56 |
57 def process_domain(db, domain, status): | 57 def process_domain(db, domain, status): |
58 domain_id = _get_domain_id(db, domain) | 58 domain_id = _get_domain_id(db, domain) |
59 _increment_entry(db, domain_id, status) | 59 _increment_entry(db, domain_id, status) |
60 | 60 |
61 | 61 |
62 def showError(message, start_response): | 62 def showError(message, start_response): |
63 start_response("400 Processing Error", [("Content-Type", "text/plain; charse
t=utf-8")]) | 63 start_response('400 Processing Error', [('Content-Type', 'text/plain; charse
t=utf-8')]) |
64 return [message.encode("utf-8")] | 64 return [message.encode('utf-8')] |
65 | 65 |
66 | 66 |
67 def _get_db(): | 67 def _get_db(): |
68 database = get_config().get("urlfixer", "database") | 68 database = get_config().get('urlfixer', 'database') |
69 dbuser = get_config().get("urlfixer", "dbuser") | 69 dbuser = get_config().get('urlfixer', 'dbuser') |
70 dbpasswd = get_config().get("urlfixer", "dbpassword") | 70 dbpasswd = get_config().get('urlfixer', 'dbpassword') |
71 if os.name == "nt": | 71 if os.name == 'nt': |
72 return MySQLdb.connect(user=dbuser, passwd=dbpasswd, db=database, | 72 return MySQLdb.connect(user=dbuser, passwd=dbpasswd, db=database, |
73 use_unicode=True, charset="utf8", named_pipe=True
) | 73 use_unicode=True, charset='utf8', named_pipe=True
) |
74 else: | 74 else: |
75 return MySQLdb.connect(user=dbuser, passwd=dbpasswd, db=database, | 75 return MySQLdb.connect(user=dbuser, passwd=dbpasswd, db=database, |
76 use_unicode=True, charset="utf8") | 76 use_unicode=True, charset='utf8') |
77 | 77 |
78 | 78 |
79 def _get_domain_id(db, domain): | 79 def _get_domain_id(db, domain): |
80 cursor = db.cursor(MySQLdb.cursors.DictCursor) | 80 cursor = db.cursor(MySQLdb.cursors.DictCursor) |
81 cursor.execute("SELECT id FROM domains WHERE domain = %s", (domain)) | 81 cursor.execute('SELECT id FROM domains WHERE domain = %s', (domain)) |
82 result = cursor.fetchone() | 82 result = cursor.fetchone() |
83 if result == None: | 83 if result == None: |
84 cursor.execute("INSERT INTO domains(domain) VALUES (%s)", (domain)) | 84 cursor.execute('INSERT INTO domains(domain) VALUES (%s)', (domain)) |
85 return db.insert_id() | 85 return db.insert_id() |
86 else: | 86 else: |
87 return result["id"] | 87 return result['id'] |
88 | 88 |
89 | 89 |
90 def _increment_entry(db, domain_id, status): | 90 def _increment_entry(db, domain_id, status): |
91 cursor = db.cursor(MySQLdb.cursors.DictCursor) | 91 cursor = db.cursor(MySQLdb.cursors.DictCursor) |
92 cursor.execute("INSERT INTO corrections(domain, status, curr_month, prev_mon
th, curr_year, prev_year) VALUES (%s, %s, %s, %s, %s, %s) ON DUPLICATE KEY UPDAT
E curr_month = curr_month + 1, curr_year = curr_year + 1", (domain_id, status, 1
, 0, 1, 0)) | 92 cursor.execute('INSERT INTO corrections(domain, status, curr_month, prev_mon
th, curr_year, prev_year) VALUES (%s, %s, %s, %s, %s, %s) ON DUPLICATE KEY UPDAT
E curr_month = curr_month + 1, curr_year = curr_year + 1', (domain_id, status, 1
, 0, 1, 0)) |
OLD | NEW |