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, subprocess |
| 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 _hg(args): |
| 26 return subprocess.Popen(["hg"] + args, stdout = subprocess.PIPE) |
| 27 |
| 28 def _extract_sites(easylist_dir): |
| 29 os.chdir(easylist_dir) |
| 30 process = _hg(["log", "--template", "{desc}\n"]) |
| 31 urls = set([]) |
| 32 |
| 33 for line in process.stdout: |
| 34 match = re.search(r"\b(https?://\S*)", line) |
| 35 if not match: |
| 36 continue |
| 37 |
| 38 url = match.group(1).strip() |
| 39 urls.add(url) |
| 40 |
| 41 return urls |
| 42 |
| 43 def _insert_sites(site_urls): |
| 44 cursor = _get_cursor() |
| 45 for url in site_urls: |
| 46 cursor.execute("INSERT INTO crawler_sites (url) VALUES (%s)", url) |
| 47 |
| 48 if __name__ == "__main__": |
| 49 easylist_dir = get_config().get("crawler", "easylist_repository") |
| 50 site_urls = _extract_sites(easylist_dir) |
| 51 _insert_sites(site_urls) |
| 52 |
OLD | NEW |