2023-03-05 14:44:49 +00:00
|
|
|
import pymongo
|
|
|
|
import trafilatura
|
|
|
|
import trafilatura.feeds
|
|
|
|
import trafilatura.sitemaps
|
|
|
|
import trafilatura.spider
|
2023-03-10 12:01:11 +00:00
|
|
|
import trafilatura.utils
|
2023-03-05 14:44:49 +00:00
|
|
|
import sys
|
2023-03-07 07:58:28 +00:00
|
|
|
import courlan
|
2023-03-11 13:14:39 +00:00
|
|
|
import urllib
|
2023-03-12 05:16:47 +00:00
|
|
|
from datetime import datetime
|
|
|
|
import click
|
2023-03-05 14:44:49 +00:00
|
|
|
|
2023-03-10 12:01:11 +00:00
|
|
|
LANGUAGE="sk"
|
2023-03-11 13:43:20 +00:00
|
|
|
DOMAIN = "sk"
|
2023-03-10 12:01:11 +00:00
|
|
|
BATCHSIZE=10
|
2023-03-11 13:14:39 +00:00
|
|
|
MINFILESIZE=300
|
2023-03-12 05:16:47 +00:00
|
|
|
MAXFILESIZE=10000000
|
|
|
|
MINTEXTSIZE=200
|
2023-03-05 17:53:14 +00:00
|
|
|
|
2023-03-07 15:18:32 +00:00
|
|
|
def calculate_checksums(text):
|
2023-03-05 17:53:14 +00:00
|
|
|
"""
|
|
|
|
@return fingerprints of a paragraphs in text. Paragraphs are separated by a blank line
|
|
|
|
"""
|
|
|
|
checksums = []
|
|
|
|
sizes = []
|
|
|
|
hval = 0
|
|
|
|
hsz = 0
|
|
|
|
sz = 0
|
|
|
|
for c in text:
|
|
|
|
cv = ord(c)
|
|
|
|
sz += 1
|
|
|
|
if cv > 64:
|
|
|
|
hval += (hval << 3) + cv
|
|
|
|
zv = hval >> 31
|
|
|
|
hval &= 0x7fffffff
|
|
|
|
hval += zv
|
|
|
|
hsz += 1
|
|
|
|
if c == "\n" and hsz > 0:
|
|
|
|
if hsz > 100:
|
|
|
|
checksums.append(hval)
|
|
|
|
sizes.append(sz)
|
|
|
|
sz = 0
|
|
|
|
hsz = 0
|
|
|
|
if hsz > 100:
|
|
|
|
checksums.append(hval)
|
|
|
|
sizes.append(sz)
|
|
|
|
return checksums, sizes
|
|
|
|
|
2023-03-11 10:30:30 +00:00
|
|
|
def is_robot_good(link,rules):
|
|
|
|
# check robots.txt rules
|
|
|
|
if rules is not None and not rules.can_fetch("*", llink):
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def is_link_good(link):
|
2023-03-11 13:14:39 +00:00
|
|
|
r = courlan.check_url(link,strict=True,language=LANGUAGE)
|
2023-03-11 10:30:30 +00:00
|
|
|
if r is None:
|
2023-03-11 13:14:39 +00:00
|
|
|
print(link)
|
2023-03-11 10:30:30 +00:00
|
|
|
return None
|
|
|
|
llink,ldomain = r
|
|
|
|
print(llink,ldomain)
|
|
|
|
# domain rules
|
2023-03-11 13:43:20 +00:00
|
|
|
if not ldomain.endswith(DOMAIN):
|
2023-03-11 10:30:30 +00:00
|
|
|
print("bad domain")
|
|
|
|
return None
|
|
|
|
if courlan.is_not_crawlable(llink):
|
|
|
|
print("not crawlable")
|
|
|
|
return None
|
2023-03-11 13:14:39 +00:00
|
|
|
return llink
|
2023-03-11 10:30:30 +00:00
|
|
|
|
|
|
|
def filter_links(links,rules=None):
|
2023-03-07 07:58:28 +00:00
|
|
|
out = set()
|
|
|
|
for link in links:
|
2023-03-11 10:30:30 +00:00
|
|
|
r = is_link_good(link)
|
2023-03-07 07:58:28 +00:00
|
|
|
if r is None:
|
|
|
|
continue
|
2023-03-10 12:01:11 +00:00
|
|
|
# check robots.txt rules
|
2023-03-11 10:30:30 +00:00
|
|
|
if rules is not None and not rules.can_fetch("*", r):
|
2023-03-10 12:01:11 +00:00
|
|
|
continue
|
2023-03-08 09:56:39 +00:00
|
|
|
out.add(llink)
|
2023-03-07 07:58:28 +00:00
|
|
|
return out
|
|
|
|
|
2023-03-10 12:01:11 +00:00
|
|
|
|
2023-03-08 09:56:39 +00:00
|
|
|
def get_link_doc(link,status="frontlink"):
|
|
|
|
r = courlan.check_url(link)
|
|
|
|
assert r is not None
|
2023-03-09 12:29:34 +00:00
|
|
|
link,host = r
|
2023-03-10 05:23:30 +00:00
|
|
|
domain = courlan.extract_domain(link)
|
2023-03-12 05:16:47 +00:00
|
|
|
return {"url":link,"host":host,"domain":domain,"status":status,"created_at":datetime.utcnow()}
|
2023-03-05 17:53:14 +00:00
|
|
|
|
2023-03-05 14:44:49 +00:00
|
|
|
def generic_visit(domain):
|
|
|
|
known_links = set(get_visited_links(domain))
|
|
|
|
visit_links = []
|
|
|
|
visit_links = trafilatura.find_feed_urls(domain)
|
|
|
|
if visit_links is None:
|
|
|
|
visit_links = trafilatura.sitemap_search(domain)
|
|
|
|
if visit_links is None:
|
|
|
|
visit_links = trafilatura.focused_crawler(dommain,known_links=known_links)
|
|
|
|
|
2023-03-07 15:18:32 +00:00
|
|
|
|
2023-03-08 09:56:39 +00:00
|
|
|
def fetch_pages(link_batch):
|
2023-03-07 07:58:28 +00:00
|
|
|
htmls = []
|
2023-03-11 13:14:39 +00:00
|
|
|
#print(link_batch)
|
|
|
|
#print("zzzzzzzzzz")
|
2023-03-07 07:58:28 +00:00
|
|
|
for link in link_batch:
|
2023-03-08 09:56:39 +00:00
|
|
|
print("fetching:::::")
|
2023-03-07 15:18:32 +00:00
|
|
|
print(link)
|
2023-03-11 13:14:39 +00:00
|
|
|
final_link = link
|
2023-03-11 10:30:30 +00:00
|
|
|
response = trafilatura.fetch_url(link,decode=False)
|
2023-03-11 13:14:39 +00:00
|
|
|
html = None
|
|
|
|
if response is not None :
|
|
|
|
good = True
|
|
|
|
if response.status != 200:
|
|
|
|
good = False
|
|
|
|
#LOGGER.error('not a 200 response: %s for URL %s', response.status, url)
|
|
|
|
elif response.data is None or len(response.data) < MINFILESIZE:
|
|
|
|
#LOGGER.error('too small/incorrect for URL %s', url)
|
|
|
|
good = False
|
|
|
|
# raise error instead?
|
|
|
|
elif len(response.data) > MAXFILESIZE:
|
|
|
|
good = False
|
|
|
|
#LOGGER.error('too large: length %s for URL %s', len(response.data), url)
|
|
|
|
if good:
|
|
|
|
html = trafilatura.utils.decode_response(response)
|
|
|
|
final_link = response.url
|
|
|
|
if html is not None:
|
|
|
|
html, final_link = trafilatura.spider.refresh_detection(html, final_link)
|
|
|
|
# is there a meta-refresh on the page?
|
|
|
|
if final_link is None: # malformed or malicious content
|
|
|
|
html = None
|
|
|
|
htmls.append((final_link,html))
|
2023-03-07 15:18:32 +00:00
|
|
|
return htmls
|
2023-03-07 09:57:47 +00:00
|
|
|
|
2023-03-11 13:14:39 +00:00
|
|
|
def fetch_robot(base_url):
|
2023-03-10 12:01:11 +00:00
|
|
|
rules = urllib.robotparser.RobotFileParser()
|
|
|
|
rules.set_url(base_url + '/robots.txt')
|
|
|
|
# exceptions happening here
|
|
|
|
try:
|
|
|
|
rules.read()
|
|
|
|
except Exception as exc:
|
2023-03-11 13:14:39 +00:00
|
|
|
#LOGGER.error('cannot read robots.txt: %s', exc)
|
2023-03-10 12:01:11 +00:00
|
|
|
rules = None
|
2023-03-10 15:19:24 +00:00
|
|
|
return rules
|
|
|
|
|
2023-03-07 09:57:47 +00:00
|
|
|
|
2023-03-10 05:23:30 +00:00
|
|
|
def extract_pages(link_batch,responses):
|
2023-03-07 09:57:47 +00:00
|
|
|
out = []
|
2023-03-11 13:14:39 +00:00
|
|
|
for original_link,(final_link,html) in zip(link_batch,responses):
|
2023-03-07 09:57:47 +00:00
|
|
|
doc = None
|
2023-03-11 13:14:39 +00:00
|
|
|
assert original_link is not None
|
2023-03-07 09:57:47 +00:00
|
|
|
if html is not None:
|
2023-03-11 13:14:39 +00:00
|
|
|
doc = trafilatura.bare_extraction(html,url=final_link,with_metadata=True,include_formatting=True,target_language=LANGUAGE)
|
2023-03-12 05:16:47 +00:00
|
|
|
if doc is not None:
|
|
|
|
if not "text" in doc or len(doc["text"]) < MINTEXTSIZE:
|
|
|
|
# text too small
|
|
|
|
doc = None
|
2023-03-11 13:14:39 +00:00
|
|
|
out.append((original_link,final_link,html,doc))
|
2023-03-07 09:57:47 +00:00
|
|
|
return out
|
|
|
|
|
2023-03-08 09:56:39 +00:00
|
|
|
|
2023-03-11 10:30:30 +00:00
|
|
|
def index_pages(db,domain,extracted_pages):
|
2023-03-07 09:57:47 +00:00
|
|
|
extracted_links = set()
|
|
|
|
linkcol = db["links"]
|
|
|
|
htmlcol = db["html"]
|
2023-03-07 15:18:32 +00:00
|
|
|
contentcol = db["content"]
|
2023-03-11 13:14:39 +00:00
|
|
|
links = []
|
2023-03-10 12:01:11 +00:00
|
|
|
for original_link,final_link,html,doc in extracted_pages:
|
2023-03-07 09:57:47 +00:00
|
|
|
state = "good"
|
|
|
|
if html is None:
|
|
|
|
state = "html_error"
|
|
|
|
elif doc is None:
|
|
|
|
state = "content_error"
|
2023-03-11 13:14:39 +00:00
|
|
|
if original_link != final_link:
|
|
|
|
linkcol.insert_one(get_link_doc(final_link,state))
|
|
|
|
state = "redirect"
|
|
|
|
linkcol.update_one({"url":original_link},{"$set":{"status":state}})
|
2023-03-07 09:57:47 +00:00
|
|
|
if doc is not None:
|
2023-03-12 05:16:47 +00:00
|
|
|
if html is not None:
|
|
|
|
htmlcol.insert_one({"url":final_link,"html":html,"html_size":len(html),"created_at":datetime.utcnow()})
|
2023-03-07 15:18:32 +00:00
|
|
|
checksums,sizes = calculate_checksums(doc["text"])
|
2023-03-12 05:16:47 +00:00
|
|
|
doc["created_at"] = datetime.utcnow()
|
|
|
|
doc["text_size"] = len(doc["text"])
|
2023-03-07 09:57:47 +00:00
|
|
|
doc["paragraph_checksums"] = checksums
|
|
|
|
doc["paragraph_sizes"] = sizes
|
2023-03-10 12:01:11 +00:00
|
|
|
# todo extract links
|
|
|
|
print(doc)
|
2023-03-07 15:18:32 +00:00
|
|
|
contentcol.insert_one(doc)
|
2023-03-10 15:19:24 +00:00
|
|
|
|
2023-03-11 10:30:30 +00:00
|
|
|
|
|
|
|
def extract_links(link_batch,responses,domain,rules,default_status="frontlink"):
|
|
|
|
links = {}
|
2023-03-11 13:14:39 +00:00
|
|
|
for original_link,(final_link,html) in zip(link_batch,responses):
|
2023-03-11 10:30:30 +00:00
|
|
|
status = default_status
|
2023-03-11 13:14:39 +00:00
|
|
|
extracted_links = courlan.extract_links(html,final_link,False,language=LANGUAGE)
|
|
|
|
#print(extracted_links)
|
|
|
|
for link in extracted_links:
|
2023-03-11 10:30:30 +00:00
|
|
|
if courlan.is_external(link,domain):
|
|
|
|
status = "frontlink"
|
|
|
|
elif courlan.is_navigation(link):
|
|
|
|
status = "navigation"
|
2023-03-11 13:14:39 +00:00
|
|
|
#print(link,status)
|
2023-03-11 10:30:30 +00:00
|
|
|
links[link] = status
|
|
|
|
outlinks = []
|
|
|
|
for link,status in links.items():
|
2023-03-11 13:14:39 +00:00
|
|
|
if not is_robot_good(link,rules):
|
2023-03-11 10:30:30 +00:00
|
|
|
continue
|
|
|
|
link = is_link_good(link)
|
|
|
|
if link is None:
|
|
|
|
continue
|
|
|
|
outlinks.append((link,status))
|
|
|
|
return outlinks
|
|
|
|
|
|
|
|
def index_links(db,extracted_links):
|
|
|
|
linkcol=db["links"]
|
|
|
|
for link,status in extracted_links:
|
|
|
|
doc = get_link_doc(link,status)
|
2023-03-10 15:19:24 +00:00
|
|
|
linkcol.insert_one(doc)
|
2023-03-07 09:57:47 +00:00
|
|
|
|
2023-03-11 10:30:30 +00:00
|
|
|
|
2023-03-10 12:01:11 +00:00
|
|
|
def get_links(db,domain,status,batch_size=BATCHSIZE):
|
2023-03-08 09:56:39 +00:00
|
|
|
linkcol = db["links"]
|
2023-03-11 13:14:39 +00:00
|
|
|
res = linkcol.find({"status":status,"host":domain},{"url":1},limit=batch_size)
|
|
|
|
links = []
|
2023-03-08 09:56:39 +00:00
|
|
|
for doc in res:
|
2023-03-11 13:14:39 +00:00
|
|
|
print(">>>>>" + status)
|
2023-03-10 12:01:11 +00:00
|
|
|
print(doc)
|
2023-03-11 13:14:39 +00:00
|
|
|
links.append(doc["url"])
|
|
|
|
return links
|
2023-03-07 07:58:28 +00:00
|
|
|
|
|
|
|
|
2023-03-05 14:44:49 +00:00
|
|
|
|
2023-03-11 13:43:20 +00:00
|
|
|
def process_links(db,domain,status,links=[],rules=None,batch_size=BATCHSIZE):
|
2023-03-11 13:14:39 +00:00
|
|
|
#print(links)
|
2023-03-11 10:30:30 +00:00
|
|
|
responses = fetch_pages(links)
|
2023-03-11 13:14:39 +00:00
|
|
|
#print(responses)
|
2023-03-11 10:30:30 +00:00
|
|
|
extracted_pages = extract_pages(links,responses)
|
2023-03-11 13:14:39 +00:00
|
|
|
#print(extracted_pages)
|
2023-03-11 10:30:30 +00:00
|
|
|
extracted_links = extract_links(links,responses,domain,rules,status)
|
2023-03-11 13:14:39 +00:00
|
|
|
print(extracted_links)
|
2023-03-11 10:30:30 +00:00
|
|
|
index_links(db,extracted_links)
|
|
|
|
index_pages(db,domain,extracted_pages)
|
|
|
|
|
2023-03-12 05:16:47 +00:00
|
|
|
|
|
|
|
def link_summary(db,domain):
|
|
|
|
linkcol = db["links"]
|
|
|
|
#res = linkcol.distinct("domain",{"hostname":domain})
|
|
|
|
|
|
|
|
res = linkcol.aggregate([
|
|
|
|
{"$match":{"host":domain}},
|
|
|
|
{"$group":{"_id":"$status","count":{"$sum":1}}},
|
|
|
|
])
|
|
|
|
for item in res:
|
|
|
|
print(item)
|
|
|
|
|
|
|
|
def create_indices(db):
|
|
|
|
linkcol = db["links"]
|
|
|
|
linkcol.create_index({"url":1},{"name":"url"})
|
|
|
|
linkcol.create_index({"host":1,"status":1},{"name":"hostname_status"})
|
|
|
|
contentcol = db["content"]
|
|
|
|
contentcol.create_index({"url":1})
|
|
|
|
contentcol.create_index({"paragraph_checksums":1})
|
|
|
|
contentcol.create_index({"domain":1})
|
|
|
|
htmlcol = db["html"]
|
|
|
|
htmlcol.create_index({"url":1})
|
|
|
|
|
2023-03-12 05:56:08 +00:00
|
|
|
@click.group()
|
|
|
|
def cli():
|
|
|
|
pass
|
|
|
|
|
2023-03-12 05:16:47 +00:00
|
|
|
@click.command()
|
2023-03-12 05:56:08 +00:00
|
|
|
@click.argument("start_link")
|
|
|
|
def simple_visit(start_link):
|
2023-03-08 09:56:39 +00:00
|
|
|
start_link,domain = courlan.check_url(start_link)
|
|
|
|
myclient = pymongo.MongoClient("mongodb://root:example@localhost:27017/")
|
|
|
|
db=myclient["crawler"]
|
2023-03-11 10:30:30 +00:00
|
|
|
rules = fetch_robot(domain)
|
2023-03-11 17:41:20 +00:00
|
|
|
batch_size = BATCHSIZE
|
|
|
|
navigation_links = get_links(db,domain,"navigation",batch_size)
|
|
|
|
if start_link is not None:
|
|
|
|
navigation_links.append(start_link)
|
2023-03-10 12:01:11 +00:00
|
|
|
print(navigation_links)
|
2023-03-11 17:41:20 +00:00
|
|
|
process_links(db,domain,"frontlink",navigation_links,rules)
|
|
|
|
links = get_links(db,domain,"frontlink",batch_size)
|
|
|
|
bl = len(links) - batch_size
|
|
|
|
if bl > 0:
|
|
|
|
print("Getting backlinks")
|
|
|
|
front_links = get_links(db,domain,"backlink",bl)
|
|
|
|
process_links(db,domain,"backlink",links,rules=rules)
|
|
|
|
link_summary(db,domain)
|
2023-03-12 05:56:08 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
cli()
|