from dataclasses import dataclass from typing import Optional from pyzotero import zotero from src import settings from src.logic.webrequest import BibTextTransformer, WebRequest @dataclass class Creator: firstName: str = None lastName: str = None creatorType: str = "author" def from_dict(self, data: dict) -> None: for key, value in data.items(): setattr(self, key, value) def from_string(self, data: str) -> "Creator": 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) -> dict: 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) -> dict: ret = {} for key, value in self.__dict__.items(): if value: ret[key] = value return ret def assign(self, book) -> None: 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) -> dict: ret = {} for key, value in self.__dict__.items(): if value: ret[key] = value return ret def assign(self, book: dict) -> None: for key, value in book.__dict__.items(): if key in self.__dict__.keys(): try: setattr(self, key, value) except AttributeError: pass class ZoteroController: zoterocfg = settings.zotero def __init__(self): if self.zoterocfg.library_id is None: return self.zot = zotero.Zotero( # type: ignore self.zoterocfg.library_id, self.zoterocfg.library_type, self.zoterocfg.api_key, ) def get_books(self) -> list: ret = [] items = self.zot.top() # type: ignore 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) -> dict: 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: 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) -> Optional[str]: resp = self.zot.create_items([item]) # type: ignore if "successful" in resp.keys(): # #print(resp["successful"]["0"]["key"]) return resp["successful"]["0"]["key"] else: return None def deleteItem(self, key) -> None: items = self.zot.items() for item in items: if item["key"] == key: self.zot.delete_item(item) # type: ignore # #print(item) break def createHGSection(self, book: Book, data: dict) -> Optional[str]: 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) -> Optional[str]: 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) -> Optional[str]: # #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) -> str: title = self.zot.item( # type: ignore item, content="bib", style="deutsche-gesellschaft-fur-psychologie", )[0] # title = title[0] title = ( title.replace("", "") .replace("", "") .replace('
', "") .replace("
", "") .replace("&", "&") ) return title if __name__ == "__main__": zot = ZoteroController() book = zot.createBook("DV 3000 D649 (4)") row = "Döbert, Hans & Hörner, Wolfgang & Kopp, Bortho von & Reuter, Lutz R." zot.createBookSection() # book = Book() # # # book. # ISBN = "9783801718718" # book = createBook(isbn=ISBN) # chapter = BookSection() # chapter.title = "Geistige Behinderung" # chapter.bookTitle = book.title # chapter.pages = "511 - 538" # chapter.publisher = book.publisher # authors = [ # Creator("Jennifer M.", "Phillips").__dict__, # Creator("Hower", "Kwon").__dict__, # Creator("Carl", "Feinstein").__dict__, # Creator("Inco", "Spintczok von Brisinski").__dict__, # ] # publishers = book.author # if isinstance(publishers, str): # publishers = [publishers] # for publisher in publishers: # # #print(publisher) # creator = Creator().from_string(publisher) # creator.creatorType = "editor" # authors.append(creator.__dict__) # chapter.creators = authors # chapter.publisher = book.publisher # # #print(chapter.to_dict()) # createBookSection(chapter.to_dict()) # get_citation("9ZXH8DDE") # # # #print() # # #print(get_books()) # # #print(zot.item_creator_types("bookSection"))