diff --git a/README.md b/README.md index c02e5c19..30084115 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,13 @@ Within a single point you have to do the scripts from top to bottom. * Analysis: aktuell_spacy.py * Archiv: Done - not needed again +#### UDE Organization: +* Info: Needs much disc space and time +* Path: inse-internal-external-scripts\intern\ude_organization +* Crawl: +* Upload: upload_ude_pages.py + + Parameter: path to "warc_zips"-folder (should be in the same directory) + ### Extern: #### Common Crawl: diff --git a/intern/ude_organization/upload_ude_pages.py b/intern/ude_organization/upload_ude_pages.py new file mode 100644 index 00000000..93288a8c --- /dev/null +++ b/intern/ude_organization/upload_ude_pages.py @@ -0,0 +1,246 @@ +import warcio +import sys +import langdetect +import trafilatura +import os +import json +import re +import time +import html +from elasticsearch import Elasticsearch +import yake +#inse elastic instance +es = Elasticsearch('https://inse.nes.uni-due.de:443/elastic', basic_auth=('elastic', 'RLldUaJgbGeoqd3KCX3p'), request_timeout=60, max_retries=5, retry_on_timeout=True) +#es = Elasticsearch({'host':'localhost', 'port': 9200, 'scheme': 'http'}) +#es = Elasticsearch('http://localhost:9200',verify_certs=True, basic_auth=('elastic', '+JFa3xVIF0g_YAhpgTLl'), max_retries=5, retry_on_timeout=True) + + + +index_name = "ude_organization" + + +def check_elastic_status(): + if not es.ping(): + print("Elasticsearch connection not found") + return False + + #if es.indices.exists(index=index_name): + # es.indices.delete(index=index_name, ignore=[400, 404]) + + settings = { + "analysis": { + "analyzer": { + "customanalyzer": { + "type": "custom", + "tokenizer": "standard", + "filter": [ + "lowercase", + "asciifolding" + ] + } + } + } + } + mappings = { + + "properties": { + "uri": { + "type": "text", + "fields": { + "keyword": { + "type": "keyword" + } + } + }, + "domain": { + "type": "keyword" + }, + "subdomain": { + "type": "keyword" + }, + "path": { + "type": "text", + "fields": { + "keyword": { + "type": "keyword" + } + } + }, + "query": { + "type": "text", + "fields": { + "keyword": { + "type": "keyword" + } + } + }, + "host": { + "type": "keyword" + }, + "body": { + "type": "text", + "analyzer": "customanalyzer", + "search_analyzer": "customanalyzer" + }, + "lang": { + "type": "keyword" + }, + "tags": { + "type": "nested", + "properties": { + "value": { "type": "keyword" }, + "score": { "type": "double" } + } + }, + } + + + } + try: + if not es.indices.exists(index=index_name): + es.indices.create(index=index_name, mappings=mappings, settings=settings) + print(f"INDEX {index_name} created...") + else: + print(f"INDEX {index_name} already exists...") + + + except Exception as e: + print("failed to create index mapping") + print(e) + return False + + return True + +def getGroups(match): + uri={'protocol':"", + 'subdomain':"", + 'domain':"", + 'port':"", + 'path':"", + 'query':""} + if match: + uri['protocol'] = match.group(1) if match.group(1) else "" + uri['subdomain'] = match.group(2) if match.group(2) else "" + uri['domain'] = match.group(3) if match.group(3) else "" + uri['port'] = match.group(4) if match.group(4) else "" + uri['path'] = match.group(5) if match.group(5) else "" + uri['query'] = match.group(6) if match.group(6) else "" + + return uri + + +def process_file(filename,total_records): + print("Searching file", filename) + records = 0 + bodies = [] + regex_for_link_parts = r'^(\w+):\/\/(?:((?:[^\/:.]+\.)+))?([^\/:.]+(?:\.[^\/:.]+)+)(?::(\d+))?(\/[^?#]*)?(\?[^#]*)?(#.*)?$' + with open(filename, 'rb') as stream: + for record in warcio.ArchiveIterator(stream): + if record.rec_type == 'response': + content_type = record.http_headers.get_header('Content-Type') + if not bool(content_type): + continue + if 'text/html' in content_type.lower(): + tokens = content_type.split(";") + encoding = "iso-8859-1" + for token in tokens: + trimmed = token.strip() + if trimmed.startswith("charset="): + encoding = trimmed[8:].lower() + + try: + decoded = record.content_stream().read().decode(encoding) + except: + continue + + text = trafilatura.extract(decoded, output_format="txt") + try: + lang = langdetect.detect(text) + except: + lang = '' + try: + title = html.unescape(re.search(r"