2015-08-02 03:51:37 +00:00
|
|
|
import os
|
2015-02-05 03:43:50 +00:00
|
|
|
import functools
|
|
|
|
import hashlib
|
|
|
|
|
|
|
|
from wpull.database.sqltable import SQLiteURLTable
|
|
|
|
from wpull.document.html import HTMLReader
|
|
|
|
import wpull.processor.rule
|
|
|
|
|
2015-07-18 10:29:49 +00:00
|
|
|
from libgrabsite import dupespotter
|
2015-08-10 13:00:37 +00:00
|
|
|
from libgrabsite.dupes import DupesOnDisk
|
2015-02-05 03:43:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class NoFsyncSQLTable(SQLiteURLTable):
|
2015-07-18 11:00:08 +00:00
|
|
|
@classmethod
|
|
|
|
def _apply_pragmas_callback(cls, connection, record):
|
|
|
|
super()._apply_pragmas_callback(connection, record)
|
|
|
|
connection.execute('PRAGMA synchronous=OFF')
|
2015-02-05 03:43:50 +00:00
|
|
|
|
|
|
|
|
2015-07-18 11:00:33 +00:00
|
|
|
class DupeSpottingProcessingRule(wpull.processor.rule.ProcessingRule):
|
2015-07-18 11:00:08 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.dupes_db = kwargs.pop('dupes_db', None)
|
|
|
|
super().__init__(*args, **kwargs)
|
2015-02-05 03:43:50 +00:00
|
|
|
|
2015-07-18 11:00:08 +00:00
|
|
|
def scrape_document(self, request, response, url_item):
|
|
|
|
if response.body.size() < 30*1024*1024:
|
|
|
|
dupes_db = self.dupes_db
|
|
|
|
body = response.body.content()
|
|
|
|
if HTMLReader.is_response(response):
|
|
|
|
body = dupespotter.process_body(body, response.request.url)
|
|
|
|
digest = hashlib.md5(body).digest()
|
|
|
|
if dupes_db is not None:
|
|
|
|
dupe_of = dupes_db.get_old_url(digest)
|
|
|
|
else:
|
|
|
|
dupe_of = None
|
|
|
|
if dupe_of is not None:
|
|
|
|
# Don't extract links from pages we've already seen
|
|
|
|
# to avoid loops that descend a directory endlessly
|
2015-07-28 12:33:59 +00:00
|
|
|
print("DUPE {}\n OF {}".format(response.request.url, dupe_of))
|
2015-07-18 11:00:08 +00:00
|
|
|
return
|
|
|
|
else:
|
|
|
|
if dupes_db is not None:
|
|
|
|
dupes_db.set_old_url(digest, response.request.url)
|
2015-02-05 03:43:50 +00:00
|
|
|
|
2015-07-18 11:00:08 +00:00
|
|
|
super().scrape_document(request, response, url_item)
|
2015-02-05 03:43:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
wpull_plugin.factory.class_map['URLTableImplementation'] = NoFsyncSQLTable
|
2015-09-30 22:16:56 +00:00
|
|
|
if int(os.environ["DUPESPOTTER_ENABLED"]):
|
|
|
|
wpull_plugin.factory.class_map['ProcessingRule'] = functools.partial(
|
|
|
|
DupeSpottingProcessingRule,
|
|
|
|
dupes_db=DupesOnDisk(
|
|
|
|
os.path.join(os.environ["GRAB_SITE_WORKING_DIR"], "dupes_db"))
|
|
|
|
)
|