OLD | NEW |
(Empty) | |
| 1 # coding: utf-8 |
| 2 |
| 3 # This Source Code is subject to the terms of the Mozilla Public License |
| 4 # version 2.0 (the "License"). You can obtain a copy of the License at |
| 5 # http://mozilla.org/MPL/2.0/. |
| 6 |
| 7 import MySQLdb, os, re |
| 8 from sitescripts.utils import cached, get_config |
| 9 |
| 10 @cached(600) |
| 11 def _get_db(): |
| 12 database = get_config().get("crawler", "database") |
| 13 dbuser = get_config().get("crawler", "dbuser") |
| 14 dbpasswd = get_config().get("crawler", "dbpassword") |
| 15 if os.name == "nt": |
| 16 return MySQLdb.connect(user=dbuser, passwd=dbpasswd, db=database, |
| 17 use_unicode=True, charset="utf8", named_pipe=True) |
| 18 else: |
| 19 return MySQLdb.connect(user=dbuser, passwd=dbpasswd, db=database, |
| 20 use_unicode=True, charset="utf8") |
| 21 |
| 22 def _get_cursor(): |
| 23 return _get_db().cursor(MySQLdb.cursors.DictCursor) |
| 24 |
| 25 def _parse_hide_filter(line): |
| 26 match = re.search(r"^(.*?)##", line) |
| 27 return match.group(1).split(",") if match else [] |
| 28 |
| 29 def _parse_block_filter(line): |
| 30 match = re.search(r"domain=(.*)", line) |
| 31 if match: |
| 32 return match.group(1).split("|") |
| 33 |
| 34 match = re.search(r"^\|\|(.*?)[/\^]", line) |
| 35 return [match.group(1)] if match else [] |
| 36 |
| 37 def _remove_comment(line): |
| 38 exclamation_index = line.find("!") |
| 39 if exclamation_index != -1: |
| 40 return line[:exclamation_index] |
| 41 return line |
| 42 |
| 43 def _parse_filters(filter_path, parse_filter): |
| 44 filters = {} |
| 45 |
| 46 try: |
| 47 for line in open(filter_path): |
| 48 line = line.strip() |
| 49 line = _remove_comment(line) |
| 50 |
| 51 domains = parse_filter(line) |
| 52 for domain in domains: |
| 53 filters[line] = domain; |
| 54 |
| 55 except IOError: |
| 56 print >>sys.stderr, "Unable to read filters from '%s'" % file_path |
| 57 |
| 58 return filters |
| 59 |
| 60 def _extract_filters(easylist_dir): |
| 61 filter_files = {"easylist_specific_block.txt": _parse_block_filter, |
| 62 "easylist_specific_hide.txt": _parse_hide_filter} |
| 63 filters = {} |
| 64 for filter_file, parse_filter in filter_files.iteritems(): |
| 65 filter_path = easylist_dir + "/easylist/" + filter_file |
| 66 filters.update(_parse_filters(filter_path, parse_filter)) |
| 67 return filters |
| 68 |
| 69 def _insert_filters(filters): |
| 70 cursor = _get_cursor() |
| 71 filter_insert = """ |
| 72 INSERT INTO crawler_filters (filter, filter_hash) VALUES (%s, sha1(filter))""" |
| 73 domain_select = "SELECT id FROM crawler_domains WHERE domain = %s" |
| 74 domain_insert = "INSERT INTO crawler_domains (domain) VALUES (%s)" |
| 75 domain_filter_insert = """ |
| 76 INSERT INTO crawler_domain_filters (filter, domain) VALUES (%s, %s)""" |
| 77 |
| 78 for filter_line, domain in filters.iteritems(): |
| 79 cursor.execute(filter_insert, filter_line) |
| 80 filter_id = cursor.lastrowid |
| 81 |
| 82 cursor.execute(domain_select, domain) |
| 83 result = cursor.fetchone() |
| 84 if result: |
| 85 domain_id = result["id"] |
| 86 else: |
| 87 cursor.execute(domain_insert, domain) |
| 88 domain_id = cursor.lastrowid |
| 89 |
| 90 cursor.execute(domain_filter_insert, (domain_id, filter_id)) |
| 91 |
| 92 if __name__ == "__main__": |
| 93 easylist_dir = get_config().get("crawler", "easylist_repository") |
| 94 filters = _extract_filters(easylist_dir) |
| 95 _insert_filters(filters) |
OLD | NEW |