367 lines
22 KiB
Python
367 lines
22 KiB
Python
from datetime import datetime
|
|
from pymongo import MongoClient
|
|
from tqdm import tqdm
|
|
import json
|
|
import os
|
|
import xml.etree.ElementTree as ET
|
|
from elasticsearch import Elasticsearch
|
|
import requests
|
|
import traceback
|
|
import logging
|
|
from bs4 import BeautifulSoup
|
|
from dotenv import load_dotenv
|
|
from os import listdir
|
|
from os.path import isfile, join
|
|
|
|
|
|
load_dotenv()
|
|
ELK_lOCAL = os.getenv("ELK_LOCAL")
|
|
es = Elasticsearch([ELK_lOCAL])
|
|
XML_DIR = os.getenv("XML_DIR")
|
|
SERVER = "mongodb://inse:Guide2020@inse.nes.uni-due.de:27017"
|
|
mongo_client = MongoClient(SERVER)
|
|
db = mongo_client.inse
|
|
collection = db.uni_due
|
|
|
|
def getXMLFiles():
|
|
for year in range(1952,2023):
|
|
url = f'https://bibliographie.ub.uni-due.de/servlets/solr/select?q=status:confirmed+AND+year:{year}'
|
|
page = requests.get(url)
|
|
soup = BeautifulSoup(page.content, 'html.parser')
|
|
soup_text = soup.find('title').text
|
|
number_publications = getPublicationsNumber(soup_text)
|
|
|
|
save_dir = f'{XML_DIR}\data_{year}.xml'
|
|
xml_url = f'https://bibliographie.ub.uni-due.de/servlets/solr/export?q=status%3Aconfirmed+AND+year%3A{year}&rows={number_publications}&XSL.Transformer=mods&lang=en'
|
|
response = requests.get(xml_url)
|
|
if int(number_publications) > 0:
|
|
with open(save_dir,'w', encoding="utf8") as file:
|
|
print(f'storing xml for year {year} number of pubs {number_publications}')
|
|
file.write(response.text)
|
|
|
|
|
|
def getPublicationsNumber(text):
|
|
if text.find('No') >-1 or text.find('Keine')>-1:
|
|
return 0
|
|
elif text.find('One') >-1 or text.find('Eine')>-1:
|
|
return 1
|
|
else:
|
|
return text[16: text.find('P')-1]
|
|
|
|
def parseXMLFile(xml_file):
|
|
try:
|
|
content = []
|
|
id = ''
|
|
with open(xml_file, 'r',encoding="utf8") as file :
|
|
filedata = file.read()
|
|
filedata = filedata.replace("&", "and")
|
|
with open('pyparse/data/test.xml', 'w', encoding="utf8") as file:
|
|
file.write(filedata)
|
|
|
|
with open(xml_file, 'r', encoding="utf8") as file:
|
|
|
|
namespace = {"mods" : "http://www.loc.gov/mods/v3"}
|
|
namespace_inner = {"xml": "{http://www.w3.org/XML/1998/namespace}"}
|
|
tree = ET.parse(file, parser = ET.XMLParser(encoding = 'iso-8859-5'))
|
|
root = tree.getroot()
|
|
contents = []
|
|
for publication in root:
|
|
id = publication.attrib['ID']
|
|
json_object = {}
|
|
json_object['id'] = publication.attrib['ID'] #id
|
|
json_object['genre_main'] = '' #genre_main
|
|
json_object['genre_local'] = '' #genre_local
|
|
json_object['genre_intern'] = '' #genre_intern
|
|
json_object['genre_de'] = '' #genre_de
|
|
json_object['genre_en'] = '' #genre_en
|
|
json_object['title'] = '' #title
|
|
json_object['author'] = '' #authors
|
|
json_object['authors'] = [] #authors_list
|
|
json_object['identifier_doi'] = '' #identifier_doi
|
|
json_object['identifier_urn'] = '' #identifier_urn
|
|
json_object['identifier_scopus'] = '' #identifier_scopus
|
|
json_object['abstract_en'] = '' #abstract_en
|
|
json_object['abstract_de'] = '' #abstract_de
|
|
json_object['abstract'] = '' #abstract
|
|
json_object['note'] = '' #note
|
|
json_object['host_title'] = '' #host_title
|
|
json_object['host_genre_main'] = '' #host_genre_main
|
|
json_object['host_genre_intern'] = '' #host_genre_intern
|
|
json_object['host_genre_de'] = '' #host_genre_de
|
|
json_object['host_genre_en'] = '' #host_genre_en
|
|
json_object['host_isbn'] = '' #host_isbn
|
|
json_object['host_location'] = '' #host_location
|
|
json_object['host_publisher'] = '' #host_publisher
|
|
json_object['host_page_start'] = '' #host_page_start
|
|
json_object['host_page_end'] = '' #host_page_end
|
|
json_object['host_title_short'] = '' #host_title_short
|
|
json_object['host_volume'] = '' #host_volume
|
|
json_object['series_genre_main'] = '' #series_genre_main
|
|
json_object['series_title'] = '' #series_title
|
|
json_object['series_volume'] = '' #series_volume
|
|
json_object['conference'] = '' #conference
|
|
json_object['year'] = '' #year
|
|
json_object['editor'] = '' #editor
|
|
json_object['editors'] = [] #list of editors
|
|
json_object['keyword'] = '' #keyword
|
|
json_object['keywords'] = [] #list of keywords
|
|
json_object['doc_language'] = '' #doc_language
|
|
json_object['research_area'] = '' #research_area
|
|
json_object['research_areas'] = [] #list of research_areas
|
|
json_object['institute'] = '' #institute
|
|
json_object['institutes'] = [] #list of institutes
|
|
json_object['faculty'] = '' #faculty
|
|
json_object['faculties'] = [] #list of faculties
|
|
json_object['group'] = '' #group
|
|
json_object['groups'] = [] #list of groups
|
|
|
|
|
|
|
|
title = publication.find("./mods:titleInfo/mods:title", namespace) #general
|
|
subtitle = publication.find("./mods:titleInfo/mods:subTitle",namespace)
|
|
publication_year = publication.find(".//mods:dateIssued[@keyDate='yes']", namespace)
|
|
genre_main = publication.find("./mods:genre[@authority='marcgt']",namespace)
|
|
genre_local = publication.find("./mods:genre[@authority='local']",namespace)
|
|
genre_intern = publication.find("./mods:genre[@type='intern']",namespace)
|
|
genre_de = publication.find(f"./mods:genre[@{namespace_inner['xml']}lang='de']",namespace)
|
|
genre_en = publication.find(f"./mods:genre[@{namespace_inner['xml']}lang='en']",namespace)
|
|
|
|
|
|
if subtitle is not None and title is not None:
|
|
json_object['title'] = f'{title.text}: {subtitle.text}'
|
|
if subtitle is None and title is not None:
|
|
json_object['title'] = title.text
|
|
if publication_year is not None:
|
|
json_object['year'] = publication_year.text
|
|
if genre_main is not None:
|
|
json_object['genre_main'] = genre_main.text
|
|
if genre_local is not None:
|
|
json_object['genre_local'] = genre_local.text
|
|
if genre_intern is not None:
|
|
json_object['genre_intern'] = genre_intern.text
|
|
if genre_de is not None:
|
|
json_object['genre_de'] = genre_de.text
|
|
if genre_en is not None:
|
|
json_object['genre_en'] = genre_en .text
|
|
|
|
names = publication.findall("./mods:name[@type='personal']", namespace) #author_info
|
|
for name in names:
|
|
author = {}
|
|
author['author_name'] = ''
|
|
author['scopus_id'] = ''
|
|
author['lsf_id'] = ''
|
|
author['role'] = ''
|
|
author['affiliation'] = ''
|
|
lastname = name.find("./mods:namePart[@type='family']", namespace)
|
|
firstname = name.find("./mods:namePart[@type='given']", namespace)
|
|
identifier_scopus = name.find("./mods:nameIdentifier[@type='scopus']", namespace)
|
|
identifier_lsf = name.find("./mods:nameIdentifier[@type='lsf']", namespace)
|
|
role = name.find(".//mods:roleTerm[@type='text']", namespace)
|
|
affiliation = name.find("./mods:affiliation", namespace)
|
|
if lastname is not None and firstname is not None:
|
|
author['author_name'] = f'{lastname.text}, {firstname.text}'
|
|
if identifier_scopus is not None:
|
|
author['scopus_id'] = identifier_scopus.text
|
|
if identifier_lsf is not None:
|
|
author['lsf_id'] = identifier_lsf.text
|
|
if role is not None:
|
|
author['role'] = role.text
|
|
if affiliation is not None:
|
|
author['affiliation'] = affiliation.text
|
|
json_object['authors'].append(author)
|
|
editors = publication.findall("./mods:relatedItem[@type='host']//mods:name[@type='personal']", namespace) #editor_info
|
|
for edt in editors:
|
|
editor = {}
|
|
editor['editor_name'] = ''
|
|
lastname = edt.find("./mods:namePart[@type='family']", namespace)
|
|
firstname = edt.find("./mods:namePart[@type='given']", namespace)
|
|
if lastname is not None and firstname is not None:
|
|
editor['editor_name'] = f'{lastname.text}, {firstname.text}'
|
|
json_object['editors'].append(editor)
|
|
host_genre_main = publication.find("./mods:relatedItem[@type='host']/mods:genre[@authority='marcgt']", namespace) #related info
|
|
host_genre_intern = publication.find("./mods:relatedItem[@type='host']/mods:genre[@type='intern']", namespace)
|
|
host_genre_de = publication.find(f"./mods:relatedItem[@type='host']/mods:genre[@{namespace_inner['xml']}lang='de']", namespace)
|
|
host_genre_en = publication.find(f"./mods:relatedItem[@type='host']/mods:genre[@{namespace_inner['xml']}lang='en']", namespace)
|
|
host_title = publication.find("./mods:relatedItem[@type='host']//mods:title", namespace)
|
|
host_page_start = publication.find("./mods:relatedItem[@type='host']//mods:start", namespace)
|
|
host_page_end = publication.find("./mods:relatedItem[@type='host']//mods:end", namespace)
|
|
host_isbn = publication.find("./mods:relatedItem[@type='host']/mods:identifier[@type='isbn']", namespace)
|
|
host_publisher = publication.find("./mods:relatedItem[@type='host']//mods:publisher", namespace)
|
|
host_location = publication.find("./mods:relatedItem[@type='host']//mods:placeTerm[@type='text']", namespace)
|
|
|
|
host_title_short = publication.find("./mods:relatedItem[@type='host']//mods:title[@type='abbreviated']", namespace)
|
|
host_volume = publication.find("./mods:relatedItem[@type='host']//mods:part/mods:detail[@type='volume']/mods:number", namespace)
|
|
|
|
conference_name = publication.find("./mods:relatedItem[@type='host']//mods:name[@type='conference']/mods:namePart", namespace)
|
|
series_genre_main = publication.find("./mods:relatedItem[@type='host']/mods:relatedItem[@type='series']/mods:genre[@authority='marcgt']", namespace)
|
|
series_title = publication.find("./mods:relatedItem[@type='host']/mods:relatedItem[@type='series']//mods:title", namespace)
|
|
series_volume = publication.find("./mods:relatedItem[@type='host']/mods:relatedItem[@type='series']//mods:detail[@type='volume']/mods:number", namespace)
|
|
if host_title is not None:
|
|
json_object['host_title'] = host_title.text
|
|
if series_title is not None:
|
|
json_object['series_title'] = series_title.text
|
|
if series_volume is not None:
|
|
json_object['series_volume'] = series_volume.text
|
|
if conference_name is not None:
|
|
json_object['conference'] = conference_name.text
|
|
if host_genre_main is not None:
|
|
json_object['host_genre_main'] = host_genre_main.text
|
|
if host_genre_intern is not None:
|
|
json_object['host_genre_intern'] = host_genre_intern.text
|
|
if host_genre_de is not None:
|
|
json_object['host_genre_de'] = host_genre_de.text
|
|
if host_genre_en is not None:
|
|
json_object['host_genre_en'] = host_genre_en.text
|
|
if series_genre_main is not None:
|
|
json_object['series_genre_main'] = series_genre_main.text
|
|
if host_page_start is not None:
|
|
json_object['host_page_start'] = host_page_start.text
|
|
if host_page_end is not None:
|
|
json_object['host_page_end'] = host_page_end.text
|
|
if host_isbn is not None:
|
|
json_object['host_isbn'] = host_isbn.text
|
|
if host_publisher is not None:
|
|
json_object['host_publisher'] = host_publisher.text
|
|
if host_location is not None:
|
|
json_object['host_location'] = host_location.text
|
|
if host_title_short is not None:
|
|
json_object['host_title_short'] = host_title_short.text
|
|
if host_volume is not None:
|
|
json_object['host_volume'] = host_volume.text
|
|
|
|
doc_identifier_doi = conference_name = publication.find("./mods:identifier[@type='doi']", namespace) #doc_identifiers
|
|
doc_identifier_scopus = conference_name = publication.find("./mods:identifier[@type='scopus']", namespace)
|
|
doc_identifier_urn = conference_name = publication.find("./mods:identifier[@type='urn']", namespace)
|
|
|
|
if doc_identifier_doi is not None:
|
|
json_object['identifier_doi'] = doc_identifier_doi.text
|
|
if doc_identifier_scopus is not None:
|
|
json_object['identifier_scopus'] = doc_identifier_scopus.text
|
|
if doc_identifier_urn is not None:
|
|
json_object['identifier_urn'] = doc_identifier_urn.text
|
|
|
|
topics = publication.findall("./mods:subject//mods:topic", namespace) #keywords
|
|
for topic in topics:
|
|
keyword = {}
|
|
keyword['keyword'] = ''
|
|
if topic is not None and topic.text.find(";")>0:
|
|
keywords = [x.strip() for x in topic.text.split(";")]
|
|
for word in keywords:
|
|
keyword['keyword'] = word.capitalize()
|
|
json_object['keywords'].append(keyword)
|
|
elif topic is not None:
|
|
keyword['keyword'] = topic.text.capitalize()
|
|
json_object['keywords'].append(keyword)
|
|
|
|
|
|
|
|
doc_language = publication.find("./mods:language//mods:languageTerm[@type='code']", namespace) #language
|
|
if doc_language is not None:
|
|
json_object['doc_language'] = doc_language.text
|
|
|
|
abstract_en = publication.find(f"./mods:abstract[@{namespace_inner['xml']}lang='en']", namespace) #abstracts
|
|
abstract_de = publication.find(f"./mods:abstract[@{namespace_inner['xml']}lang='de']", namespace)
|
|
abstract = publication.find("./mods:abstract",namespace)
|
|
if abstract_en is not None:
|
|
json_object['abstract_en'] = abstract_en.text
|
|
if abstract_de is not None:
|
|
json_object['abstract_de'] = abstract_de.text
|
|
if abstract is not None:
|
|
json_object['abstract'] = abstract.text
|
|
|
|
classifications_areas = publication.findall("./mods:classification[@authorityURI='https://bibliographie.ub.uni-due.de/classifications/fachreferate']", namespace) #classifications
|
|
if len(classifications_areas) == 0:
|
|
classifications_areas = publication.findall("./mods:classification[@authorityURI='http://duepublico.uni-duisburg-essen.de/classifications/fachreferate']", namespace)
|
|
for classification in classifications_areas:
|
|
area = {}
|
|
area['area'] = ''
|
|
if classification is not None:
|
|
if classification.text is not None and classification.text != "" :
|
|
area['area'] = classification.text
|
|
json_object['research_areas'].append(area)
|
|
|
|
classifications_origins = publication.findall("./mods:classification[@authorityURI='https://bibliographie.ub.uni-due.de/classifications/ORIGIN']", namespace) #classifications
|
|
if len(classifications_origins) == 0:
|
|
classifications_origins = publication.findall("./mods:classification[@authorityURI='http://duepublico.uni-duisburg-essen.de/classifications/ORIGIN']", namespace)
|
|
for classification in classifications_origins:
|
|
faculty = {}
|
|
institute = {}
|
|
group = {}
|
|
faculty['name'] = ''
|
|
institute['name'] = ''
|
|
group['name'] = ''
|
|
if classification is not None:
|
|
if classification.text.find("»") > 0:
|
|
result = [x.strip() for x in classification.text.split('»')]
|
|
if len(result) == 4:
|
|
faculty['name'] = result[0]
|
|
institute['name'] = result[1]
|
|
group['name'] = result[3]
|
|
json_object['faculties'].append(faculty)
|
|
json_object['institutes'].append(institute)
|
|
json_object['groups'].append(group)
|
|
if len(result) == 3:
|
|
faculty['name'] = result[0]
|
|
institute['name'] = result[1]
|
|
group['name'] = result[2]
|
|
json_object['faculties'].append(faculty)
|
|
json_object['institutes'].append(institute)
|
|
json_object['groups'].append(group)
|
|
if len(result) == 2:
|
|
if result[0].find("Faculty") or result[0].find("Fakultät"):
|
|
faculty['name'] = result[0]
|
|
institute['name'] = result[1]
|
|
json_object['faculties'].append(faculty)
|
|
json_object['institutes'].append(institute)
|
|
else:
|
|
institute['name'] = result[0]
|
|
group['name'] = result[1]
|
|
json_object['institutes'].append(institute)
|
|
json_object['groups'].append(group)
|
|
else:
|
|
faculty['name'] = classification.text
|
|
json_object['faculties'].append(faculty)
|
|
|
|
note = publication.find("./mods:note", namespace) #note
|
|
if note is not None:
|
|
json_object['note'] = note.text
|
|
|
|
json_object['keyword'] = '; '.join([element['keyword'] for element in json_object['keywords']])
|
|
json_object['research_area'] = '; '.join([element['area'] for element in json_object['research_areas']])
|
|
json_object['faculty'] = '; '.join([element['name'] for element in json_object['faculties']])
|
|
json_object['institute'] = '; '.join([element['name'] for element in json_object['institutes']])
|
|
json_object['group'] = '; '.join([element['name'] for element in json_object['groups']])
|
|
json_object['editor'] = '; '.join([element['editor_name'] for element in json_object['editors']])
|
|
json_object['author'] = '; '.join([element['author_name'] for element in json_object['authors']])
|
|
|
|
#print(json_object)
|
|
contents.append(json_object)
|
|
return contents
|
|
|
|
except:
|
|
print(f'parsing & persisting in {xml_file} and id {id}')
|
|
logging.error(traceback.format_exc())
|
|
|
|
def persistObject(contents):
|
|
body = []
|
|
for content in tqdm(contents):
|
|
#collection.insert_one(content)
|
|
body.append({'index': {'_index': 'due_index_1.0','_id':content['id']}})
|
|
body.append(content)
|
|
response = es.bulk(body=body)
|
|
print(f'####persisted {len(contents)} successfully')
|
|
|
|
return
|
|
|
|
def parseNPersist():
|
|
#list_jsons = parseXMLFile('pyparse/data/test.xml')
|
|
files = [file for file in listdir(XML_DIR) if isfile(join(XML_DIR, file))]
|
|
for file in files:
|
|
print(f'parsing & persisting {file}')
|
|
list_jsons = parseXMLFile(f'{XML_DIR}\{file}')
|
|
persistObject(list_jsons)
|
|
|
|
if __name__ == '__main__':
|
|
getXMLFiles()
|
|
parseNPersist()
|