def papers_get_keywords(self): """ Get all the keywords from papers_server """ try: keywords = self.papers_server.get_keywords() return sorted([e for e in keywords]) except Exception as e: logger.error(f'WORKER papers_get_keywords failed: {e}') return None
def papers_get_descriptions(self): """ Get all the description names from papers_server """ try: names = self.papers_server.get_descriptions() return sorted([e for e in names]) except Exception as e: logger.error(f'WORKER papers_get_descriptions failed: {e}') return None
def buffer_list(self): """ Get a list of filenames in buffer_server """ names = [] try: names = self.buffer_server.get_names() except Exception as e: logger.error(f'WORKER buffer_list failed: {e}') finally: return names
def buffer_parse(self, name): """ Parse file by [name] in buffer_server """ try: fpath, info, bits = self.buffer_server.get_by_name(name) contents = dict(title='', doi='') if '/Title' in info: contents['title'] = info['/Title'][1:-1] if '/doi' in info: doi = info['/doi'][1:-1].split('/')[1] contents['doi'] = doi return contents except Exception as e: logger.error(f'WORKER buffer_parse failed: {e}') return None
def buffer_get(self, name, method='open'): """ Get file by [name] in buffer_server using [method='open' or 'start'] method: 'open' means return bits stream 'start' means start the file using default app """ try: assert (method in ['start', 'open']) fpath, info, bits = self.buffer_server.get_by_name(name) if method == 'open': return bits if method == 'start': logger.info(f'WORKER buffer_get starts {name}') os.system(fpath) return None except Exception as e: logger.error(f'WORKER buffer_get failed: {e}') return None
def papers_get_by_title(self, title, fields=['keywords', 'descriptions']): """ Get paper and its contents by [title]. outputs: A dict contains bits, keywords, descriptions in the format of json. None if failed. """ try: paper_contents = self.papers_server.get_by_title(title, fields=fields) return paper_contents except AssertionError as e: logger.warning( f'WORKER papers_get_by_title cannot get not existing title: {title}.' ) return None except Exception as e: logger.error(f'WORKER papers_get_by_title failed: {e}') return None
def serve_connection(self, connection, address, idx=None): """ Method to serve [connection] of [idx] from [address]. """ try: # Fetch request request = connection.recv(65536).decode() length = len(request) logger.info(f'WEBSERVER-{idx} receives {length} bits') # Respond content = self.respond(request) if not isinstance(content, bytes): content = content.encode() length = len(content) logger.info(f'WEBSERVER-{idx} responses {length} bits') connection.sendall(content) idx += 1 except Exception as e: logger.error( f'WEBSERVER runtime error. connection={connection}, client_address={address}, error={e}' ) finally: connection.close() logger.info(f'WEBSERVER-{idx} connection closed')
def buffer_commit(self, name, content): """ Handle new commit based on [name] and [content]. Return 0 if success, return others if failed. """ # Parse [content] try: new_content = dict( timestamp=float(content['date']) / 1000, # Commit timestamp title=content['title'], # Title of the paper keywords=[ e.strip().title() for e in content['keywords'].split(',') if e.strip() ], # Keywords of the paper, list descriptions=self._description2dict_(content['descriptions']) ) # Descriptions of the paper, dict logger.info(f'WORKER buffer_commit parsed content') print(new_content) except Exception as e: logger.error( f'WORKER buffer_commit failed on parsing content: {content}, error: {e}' ) return 1 # Get pdfbits pdfbits = self.buffer_get(name) if pdfbits is None: logger.error( f'WORKER buffer_commit failed on getting pdf file {name}') return 1 try: # Commit to papers_server self.papers_server.new_commit(new_content, pdfbits) # Ignore new name in buffer_server self.buffer_server.new_ignore(name) logger.info(f'WORKER buffer_commit committed {new_content}.') return 0 except Exception as e: logger.error( f'Worker buffer_commit failed on committing content: {new_content}, error: {e}' ) return 1