zz
This commit is contained in:
		
							parent
							
								
									39f66bc98a
								
							
						
					
					
						commit
						e728134bf6
					
				@ -7,12 +7,15 @@ import trafilatura.utils
 | 
				
			|||||||
import sys
 | 
					import sys
 | 
				
			||||||
import courlan
 | 
					import courlan
 | 
				
			||||||
import urllib
 | 
					import urllib
 | 
				
			||||||
 | 
					from datetime import datetime
 | 
				
			||||||
 | 
					import click
 | 
				
			||||||
 | 
					
 | 
				
			||||||
LANGUAGE="sk"
 | 
					LANGUAGE="sk"
 | 
				
			||||||
DOMAIN = "sk"
 | 
					DOMAIN = "sk"
 | 
				
			||||||
BATCHSIZE=10
 | 
					BATCHSIZE=10
 | 
				
			||||||
MINFILESIZE=300
 | 
					MINFILESIZE=300
 | 
				
			||||||
MAXFILESIZE=1000000
 | 
					MAXFILESIZE=10000000
 | 
				
			||||||
 | 
					MINTEXTSIZE=200
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def calculate_checksums(text):
 | 
					def calculate_checksums(text):
 | 
				
			||||||
    """
 | 
					    """
 | 
				
			||||||
@ -83,7 +86,7 @@ def get_link_doc(link,status="frontlink"):
 | 
				
			|||||||
    assert r is not None
 | 
					    assert r is not None
 | 
				
			||||||
    link,host = r
 | 
					    link,host = r
 | 
				
			||||||
    domain = courlan.extract_domain(link)
 | 
					    domain = courlan.extract_domain(link)
 | 
				
			||||||
    return {"url":link,"host":host,"domain":domain,"status":status}
 | 
					    return {"url":link,"host":host,"domain":domain,"status":status,"created_at":datetime.utcnow()}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def generic_visit(domain):
 | 
					def generic_visit(domain):
 | 
				
			||||||
    known_links = set(get_visited_links(domain))
 | 
					    known_links = set(get_visited_links(domain))
 | 
				
			||||||
@ -147,6 +150,10 @@ def extract_pages(link_batch,responses):
 | 
				
			|||||||
        assert original_link is not None
 | 
					        assert original_link is not None
 | 
				
			||||||
        if html is not None:
 | 
					        if html is not None:
 | 
				
			||||||
            doc = trafilatura.bare_extraction(html,url=final_link,with_metadata=True,include_formatting=True,target_language=LANGUAGE)
 | 
					            doc = trafilatura.bare_extraction(html,url=final_link,with_metadata=True,include_formatting=True,target_language=LANGUAGE)
 | 
				
			||||||
 | 
					            if doc is not None:
 | 
				
			||||||
 | 
					                if not "text" in doc or len(doc["text"]) < MINTEXTSIZE:
 | 
				
			||||||
 | 
					                    # text too small
 | 
				
			||||||
 | 
					                    doc = None
 | 
				
			||||||
        out.append((original_link,final_link,html,doc))
 | 
					        out.append((original_link,final_link,html,doc))
 | 
				
			||||||
    return out
 | 
					    return out
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -167,10 +174,12 @@ def index_pages(db,domain,extracted_pages):
 | 
				
			|||||||
            linkcol.insert_one(get_link_doc(final_link,state))
 | 
					            linkcol.insert_one(get_link_doc(final_link,state))
 | 
				
			||||||
            state = "redirect"
 | 
					            state = "redirect"
 | 
				
			||||||
        linkcol.update_one({"url":original_link},{"$set":{"status":state}})
 | 
					        linkcol.update_one({"url":original_link},{"$set":{"status":state}})
 | 
				
			||||||
        if html is not None:
 | 
					 | 
				
			||||||
            htmlcol.insert_one({"url":final_link,"html":html})
 | 
					 | 
				
			||||||
        if doc is not None:
 | 
					        if doc is not None:
 | 
				
			||||||
 | 
					            if html is not None:
 | 
				
			||||||
 | 
					                htmlcol.insert_one({"url":final_link,"html":html,"html_size":len(html),"created_at":datetime.utcnow()})
 | 
				
			||||||
            checksums,sizes = calculate_checksums(doc["text"])
 | 
					            checksums,sizes = calculate_checksums(doc["text"])
 | 
				
			||||||
 | 
					            doc["created_at"] = datetime.utcnow()
 | 
				
			||||||
 | 
					            doc["text_size"] = len(doc["text"])
 | 
				
			||||||
            doc["paragraph_checksums"] = checksums
 | 
					            doc["paragraph_checksums"] = checksums
 | 
				
			||||||
            doc["paragraph_sizes"] = sizes
 | 
					            doc["paragraph_sizes"] = sizes
 | 
				
			||||||
            # todo extract links
 | 
					            # todo extract links
 | 
				
			||||||
@ -231,6 +240,30 @@ def process_links(db,domain,status,links=[],rules=None,batch_size=BATCHSIZE):
 | 
				
			|||||||
    index_links(db,extracted_links)
 | 
					    index_links(db,extracted_links)
 | 
				
			||||||
    index_pages(db,domain,extracted_pages)
 | 
					    index_pages(db,domain,extracted_pages)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					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})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@click.command()
 | 
				
			||||||
def simple_visit(start_link=None):
 | 
					def simple_visit(start_link=None):
 | 
				
			||||||
    start_link,domain = courlan.check_url(start_link)
 | 
					    start_link,domain = courlan.check_url(start_link)
 | 
				
			||||||
    myclient = pymongo.MongoClient("mongodb://root:example@localhost:27017/")
 | 
					    myclient = pymongo.MongoClient("mongodb://root:example@localhost:27017/")
 | 
				
			||||||
@ -249,25 +282,3 @@ def simple_visit(start_link=None):
 | 
				
			|||||||
        front_links = get_links(db,domain,"backlink",bl)
 | 
					        front_links = get_links(db,domain,"backlink",bl)
 | 
				
			||||||
    process_links(db,domain,"backlink",links,rules=rules)
 | 
					    process_links(db,domain,"backlink",links,rules=rules)
 | 
				
			||||||
    link_summary(db,domain)
 | 
					    link_summary(db,domain)
 | 
				
			||||||
 | 
					 | 
				
			||||||
def link_summary(db,domain):
 | 
					 | 
				
			||||||
    linkcol = db["links"]
 | 
					 | 
				
			||||||
    res = linkcol.aggregate([
 | 
					 | 
				
			||||||
        {"$match":{"hostname":domain}},
 | 
					 | 
				
			||||||
        {"$group":{"_id":{"status":domain},"count":{"$count":1}}},
 | 
					 | 
				
			||||||
    ])
 | 
					 | 
				
			||||||
    print(res)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
def create_indices(db):
 | 
					 | 
				
			||||||
    linkcol = db["links"]
 | 
					 | 
				
			||||||
    linkcol.create_index({"url":1},{"name":"url"})
 | 
					 | 
				
			||||||
    linkcol.create_index({"hostname":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})
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
simple_visit(sys.argv[1])
 | 
					 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user