from pyzotero import zotero from dataclasses import dataclass from src.logic.webrequest import WebRequest, BibTextTransformer from omegaconf import OmegaConf config = OmegaConf.load("config.yaml") config = config["zotero"] @dataclass class Creator: firstName: str = None lastName: str = None creatorType: str = "author" def from_dict(self, data: dict): for key, value in data.items(): setattr(self, key, value) def from_string(self, data: str): if "," in data: self.firstName = data.split(",")[1] self.lastName = data.split(",")[0] return self # set __dict__ object to be used in json @dataclass class Book: itemType: str = "book" creators: list[Creator] = None tags: list = None collections: list = None relations: dict = None title: str = None abstractNote: str = None series: str = None seriesNumber: str = None volume: str = None numberOfVolumes: str = None edition: str = None place: str = None publisher: str = None date: str = None numPages: str = None language: str = None ISBN: str = None shortTitle: str = None url: str = None accessDate: str = None archive: str = None archiveLocation: str = None libraryCatalog: str = None callNumber: str = None rights: str = None extra: str = None def to_dict(self): ret = {} for key, value in self.__dict__.items(): if value: ret[key] = value return ret @dataclass class BookSection: itemType: str = "bookSection" title: str = None creators: list[Creator] = None abstractNote: str = None bookTitle: str = None series: str = None seriesNumber: str = None volume: str = None numberOfVolumes: str = None edition: str = None place: str = None publisher: str = None date: str = None pages: str = None language: str = None ISBN: str = None shortTitle: str = None url: str = None accessDate: str = None archive: str = None archiveLocation: str = None libraryCatalog: str = None callNumber: str = None rights: str = None extra: str = None tags = list collections = list relations = dict def to_dict(self): ret = {} for key, value in self.__dict__.items(): if value: ret[key] = value return ret def assign(self, book): for key, value in book.__dict__.items(): if key in self.__dict__.keys(): try: setattr(self, key, value) except AttributeError: pass @dataclass class JournalArticle: itemType = "journalArticle" title: str = None creators: list[Creator] = None abstractNote: str = None publicationTitle: str = None volume: str = None issue: str = None pages: str = None date: str = None series: str = None seriesTitle: str = None seriesText: str = None journalAbbreviation: str = None language: str = None DOI: str = None ISSN: str = None shortTitle: str = None url: str = None accessDate: str = None archive: str = None archiveLocation: str = None libraryCatalog: str = None callNumber: str = None rights: str = None extra: str = None tags = list collections = list relations = dict def to_dict(self): ret = {} for key, value in self.__dict__.items(): if value: ret[key] = value return ret def assign(self, book: dict): for key, value in book.__dict__.items(): if key in self.__dict__.keys(): try: setattr(self, key, value) except AttributeError: pass class ZoteroController: def __init__(self): self.zot = zotero.Zotero( config["library_id"], config["library_type"], config["api_key"] ) def get_books(self): ret = [] items = self.zot.top() for item in items: if item["data"]["itemType"] == "book": ret.append(item) return ret # create item in zotero # item is a part of a book def __get_data(self, isbn): web = WebRequest() web.get_ppn(isbn) data = web.get_data_elsa() bib = BibTextTransformer("ARRAY") bib.get_data(data) book = bib.return_data() return book # # print(zot.item_template("bookSection")) def createBook(self, isbn): book = self.__get_data(isbn) bookdata = Book() bookdata.title = book.title.split(":")[0] bookdata.ISBN = book.isbn bookdata.language = book.language bookdata.date = book.year bookdata.publisher = book.publisher bookdata.url = book.link bookdata.edition = book.edition bookdata.place = book.place bookdata.numPages = book.pages authors = [ Creator().from_string(author).__dict__ for author in book.author.split(";") ] bookdata.creators = authors return bookdata def createItem(self, item): resp = self.zot.create_items([item]) if "successful" in resp.keys(): # print(resp["successful"]["0"]["key"]) return resp["successful"]["0"]["key"] else: return None def deleteItem(self, key): items = self.zot.items() for item in items: if item["key"] == key: self.zot.delete_item(item) # print(item) break def createHGSection(self, book: Book, data: dict): chapter = BookSection() chapter.assign(book) chapter.pages = data["pages"] chapter.itemType = "bookSection" chapter.ISBN = "" chapter.url = "" chapter.title = data["chapter_title"] creators = chapter.creators for creator in creators: creator["creatorType"] = "editor" chapter.creators = creators authors = [ Creator().from_string(author).__dict__ for author in data["section_author"].split(";") ] chapter.creators += authors # print(chapter.to_dict()) return self.createItem(chapter.to_dict()) pass def createBookSection(self, book: Book, data: dict): chapter = BookSection() chapter.assign(book) chapter.pages = data["pages"] chapter.itemType = "bookSection" chapter.ISBN = "" chapter.url = "" chapter.title = "" return self.createItem(chapter.to_dict()) # chapter.creators def createJournalArticle(self, journal, article): # print(type(article)) journalarticle = JournalArticle() journalarticle.assign(journal) journalarticle.itemType = "journalArticle" journalarticle.creators = [ Creator().from_string(author).__dict__ for author in article["section_author"].split(";") ] journalarticle.date = article["year"] journalarticle.title = article["chapter_title"] journalarticle.publicationTitle = article["work_title"].split(":")[0].strip() journalarticle.pages = article["pages"] journalarticle.ISSN = article["isbn"] journalarticle.issue = article["issue"] journalarticle.url = article["isbn"] # print(journalarticle.to_dict()) return self.createItem(journalarticle.to_dict()) def get_citation(self, item): title = self.zot.item( item, content="bib", style="deutsche-gesellschaft-fur-psychologie", )[0] # title = title[0] title = ( title.replace("", "") .replace("", "") .replace('