def get_libranon(server_prefix, lsb_server, n=0, libranon=False): if not libranon: libranon = u"{} {}".format(string.capwords(random.choice(first_names)), string.capwords(random.choice(last_names))) if n > 2: logger.info("LIBRANON + N: {} {}".format(libranon, n)) return libranon try: r = requests.get("{}://library.{}/get_active_librarians".format( server_prefix, lsb_server), timeout=2, verify=False) if r.ok: req = r.json() if 'librarians' in req: logger.info("LIBRANON & ACTIVE LIBRARIANS: {} {}; n={}".format( libranon, str(req['librarians']), n)) if libranon not in req['librarians']: return libranon except requests.exceptions.RequestException as e: logger.info("EXCEPTION_GET_LIBRANON: {}".format(e)) return get_libranon(server_prefix, lsb_server, n + 1)
def run(self): time.sleep(5) inc_time = self.increase_time(1,2) in_time = inc_time.next() count = 0 try: while self.gotcha: if count%in_time == 0: in_time = inc_time.next() for url in self.urls: if requests.get(url).ok: self.connection_ok.emit() print("connection_ok!") else: self.lost_connection.emit() self.gotcha = False print("lost_connection!") return time.sleep(0.2) count += 1 return except: self.lost_connection.emit() self.gotcha = False print("BAAAM!") return return
def get_libranon(server_prefix, lsb_server, n=0, libranon=False): if not libranon: libranon = u"{} {}".format(string.capwords(random.choice(first_names)), string.capwords(random.choice(last_names))) if n > 2: logger.info("LIBRANON + N: {} {}".format(libranon, n)) return libranon try: r = requests.get("{}://library.{}/get_active_librarians".format( server_prefix, lsb_server), timeout=2, verify=False) if r.ok: req = r.json() if 'librarians' in req: logger.info("LIBRANON & ACTIVE LIBRARIANS: {} {}; n={}" .format(libranon, str(req['librarians']), n)) if libranon not in req['librarians']: return libranon except requests.exceptions.RequestException as e: logger.info("EXCEPTION_GET_LIBRANON: {}".format(e)) return get_libranon(server_prefix, lsb_server, n+1)
def get_server_list(self, uuid): #r = requests.get("https://library.{}/get_catalog".format(prefs['lsb_server'], params={'uuid': uuid})) r = requests.get("http://library.{}/get_catalog".format(prefs['lsb_server']), params={'uuid': uuid}) catalog = r.json() if catalog is None: return [] else: return catalog['books']
def get_book_metadata(self, book_id): try: book_metadata = requests.get("{base_url}{book_metadata_url}{book_id}".format(base_url=self.base_url, book_metadata_url=self.book_metadata_url, book_id=book_id)) if book_metadata.ok: return book_metadata.content else: return False except requests.exceptions.RequestException as e: return False
def get_server_list(self, uuid): try: r = requests.get("{}://library.{}/get_catalog".format(prefs['server_prefix'], prefs['lsb_server']), params={'uuid': uuid}) catalog = r.json() except: catalog = None self.upload_error.emit() if catalog is None: return [] else: return catalog['books']
def get_books_ids(self, total_num=1000000): books_ids_url = 'ajax/search?query=&num={total_num}&sort=last_modified'.format(total_num=total_num) try: books_ids_request = requests.get("{base_url}{books_ids_url}".format(base_url=self.base_url, books_ids_url=books_ids_url)) if books_ids_request.ok: return books_ids_request.json()['book_ids'] else: return False except requests.exceptions.RequestException as e: return False
def __init__(self, gui, icon, do_user_config, qaction, us): QDialog.__init__(self, gui) self.main_gui = gui self.do_user_config = do_user_config self.qaction = qaction self.us = us self.initial = True self.lsb_url_text = 'Be a librarian. Share your library.' self.url_label_tooltip = '<<<< Be a librarian. Click on Start sharing button.<<<<' self.lsb_url = 'nourl' if prefs['librarian'] == 'l' or prefs['librarian'] == '': self.librarian = get_libranon() else: self.librarian = prefs['librarian'] self.metadata_thread = MetadataLibThread(self.librarian) self.check_connection = ConnectionCheck() self.clip = QApplication.clipboard() self.pxmp = QPixmap() self.pxmp.load('images/icon_connected.png') self.icon_connected = QIcon(self.pxmp) self.setStyleSheet(""" QDialog { background-color: white; } QPushButton { font-size: 16px; border-style: solid; border-color: red; font-family:'BitstreamVeraSansMono',Consolas,monospace; text-transform: uppercase; } QPushButton#arrow { border-width: 16px; border-right-color:white; padding: -10px; color:red; } QPushButton#url { background-color: red; min-width: 460px; color: white; text-align: left; } QPushButton#url:hover { background-color: white; color: red; } QPushButton#share { background-color: red; color: white; margin-right: 10px; } QPushButton#share:hover { background-color: white; color: red; } QPushButton#url2 { color: #222; text-align: left; } QPushButton#url2:hover { color: red; } QLineEdit#edit { background-color: white; color: black; font-size: 16px; border-style: solid; border-color: red; font-family:'BitstreamVeraSansMono',Consolas,monospace; text-transform: uppercase; } """) self.ll = QVBoxLayout() #self.ll.setSpacing(1) self.l = QHBoxLayout() self.l.setSpacing(0) self.l.setMargin(0) #self.l.setContentsMargins(0,0,0,0) self.w = QWidget() self.w.setLayout(self.l) self.setLayout(self.ll) self.setWindowIcon(icon) self.debug_label = QLabel() self.ll.addWidget(self.debug_label) self.debug_label.show() self.lets_share_button = QPushButton() self.lets_share_button.setSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.MinimumExpanding) self.lets_share_button.setObjectName("share") #self.lets_share_button.clicked.connect(self.lets_share) self.l.addWidget(self.lets_share_button) self.url_label = QPushButton() self.url_label.setSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.MinimumExpanding) self.url_label.setObjectName("url") #self.url_label.clicked.connect(self.open_url) self.l.addWidget(self.url_label) self.arrow_button = QPushButton("_____") self.arrow_button.setObjectName("arrow") self.l.addWidget(self.arrow_button) self.ll.addWidget(self.w) self.ll.addSpacing(5) self.libranon_layout = QHBoxLayout() self.libranon_layout.setSpacing(0) self.libranon_layout.setMargin(0) #self.l.setContentsMargins(0,0,0,0) self.libranon_container = QWidget() self.libranon_container.setLayout(self.libranon_layout) self.edit = QLineEdit() self.edit.setObjectName("edit") self.edit.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) self.edit.setToolTip("Change your librarian name") self.edit.setText(self.librarian) #self.edit.textChanged.connect(self.handle_text_changed) self.save_libranon = QPushButton("librarian:") self.save_libranon.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum) self.save_libranon.setObjectName("share") self.save_libranon.setToolTip("Save your librarian name") self.libranon_layout.addWidget(self.save_libranon) self.libranon_layout.addWidget(self.edit) self.save_libranon.clicked.connect(self.save_librarian) self.ll.addWidget(self.libranon_container) self.ll.addSpacing(10) self.chat_button = QPushButton("Chat room: https://chat.memoryoftheworld.org") #self.chat_button.hovered.connect(self.setCursorToHand) self.chat_button.setObjectName("url2") self.chat_button.setToolTip('Meetings every thursday at 23:59 (central eruopean time)') self.chat_button.clicked.connect(functools.partial(self.open_url, "https://chat.memoryoftheworld.org/?nick={}".format(self.librarian.lower().replace(" ", "_")))) self.ll.addWidget(self.chat_button) self.about_project_button = QPushButton('Public Library: http://www.memoryoftheworld.org') self.about_project_button.setObjectName("url2") self.about_project_button.setToolTip('When everyone is librarian, library is everywhere.') self.metadata_thread.uploaded.connect(lambda: self.render_library_button("{}://library.{}".format(prefs['server_prefix'], prefs['lsb_server']), "Building together real-time p2p library infrastructure.")) self.ll.addWidget(self.about_project_button) self.debug_log = QListWidget() self.ll.addWidget(self.debug_log) self.debug_log.addItem("Initiatied!") self.debug_log.hide() from PyQt4 import QtWebKit self.webview = QtWebKit.QWebView() self.webview.setMaximumWidth(680) self.webview.setMaximumHeight(400) self.webview.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) self.webview.setUrl(QtCore.QUrl( "https://chat.memoryoftheworld.org/?nick={}".format(self.librarian.lower().replace(" ", "_")))) self.ll.addWidget(self.webview) #self.webview.hide() #- check if there is a new version of plugin ---------------------------------------------- self.plugin_url = "https://github.com/marcellmars/letssharebooks/raw/master/calibreletssharebooks/letssharebooks_calibre.zip" self.running_version = ".".join(map(str, lsb.version)) try: r = requests.get('https://raw.github.com/marcellmars/letssharebooks/master/calibreletssharebooks/_version', timeout=3) self.latest_version = r.text[-1] except: self.latest_version = "0.0.0" self.upgrade_button = QPushButton('Please download and upgrade from {0} to {1} version of plugin.'.format(self.us.running_version, self.latest_version)) self.upgrade_button.setObjectName("url2") self.upgrade_button.setToolTip('Running latest version you make developers happy') self.upgrade_button.clicked.connect(functools.partial(self.open_url, self.plugin_url)) version_list = [self.us.running_version, self.us.latest_version] version_list.sort(key=lambda s: map(int, s.split('.'))) if self.us.running_version != self.us.latest_version: if self.us.running_version == version_list[0]: self.ll.addSpacing(20) self.ll.addWidget(self.upgrade_button) #------------------------------------------------------------------------------------------ self.resize(self.sizeHint()) #- parsing/tee log file ------------------------------------------------------------------- self.se = open("/tmp/lsb.log", "w+b") #self.se = tempfile.NamedTemporaryFile() self.so = self.se sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0) os.dup2(self.so.fileno(), sys.stdout.fileno()) os.dup2(self.se.fileno(), sys.stderr.fileno()) #- state machine -------------------------------------------------------------------------- self.machine = QtCore.QStateMachine() self.on = QtCore.QState() self.on.setObjectName("on") self.on.entered.connect(lambda: self.render_lsb_button("Start sharing", self.lsb_url_text)) self.calibre_web_server = QtCore.QState() self.calibre_web_server.setObjectName("calibre_web_server") self.calibre_web_server.entered.connect(self.start_calibre_server) self.calibre_web_server.entered.connect(lambda: self.render_lsb_button("Stop sharing", self.lsb_url_text)) self.calibre_web_server.assignProperty(self.debug_label, 'text', 'Starting Calibre web server...') self.ssh_server = QtCore.QState() self.ssh_server.setObjectName("ssh_server") self.ssh_server.entered.connect(lambda: self.render_lsb_button("Stop sharing", "Connecting...")) self.ssh_server.entered.connect(self.establish_ssh_server) self.ssh_server.assignProperty(self.debug_label, 'text', 'Establishing SSH tunnel...') self.ssh_server_established = QtCore.QState() self.ssh_server_established.setObjectName("ssh_server_established") self.ssh_server_established.entered.connect(lambda: self.render_lsb_button("Stop sharing", self.lsb_url_text)) self.ssh_server_established.entered.connect(self.check_connections) self.ssh_server_established.assignProperty(self.debug_label, 'text', 'Established SSH tunnel...') self.url_label_clicked = QtCore.QState() self.url_label_clicked.setObjectName("url_label_clicked") self.url_label_clicked.entered.connect(lambda: self.open_url(self.lsb_url)) self.url_label_clicked.assignProperty(self.debug_label, 'text', 'URL label clicked!') self.about_project_clicked = QtCore.QState() self.about_project_clicked.setObjectName("about_project_clicked") self.about_project_clicked.entered.connect(lambda: self.open_url("{}://library.{}".format(prefs['server_prefix'], prefs['lsb_server']))) self.about_project_clicked.assignProperty(self.debug_label, 'text', 'about_project_button clicked!') self.library_state_changed = QtCore.QState() self.library_state_changed.entered.connect(lambda: self.render_library_button("Uploading library metadata...", "Sharing with the others who share their libraries now...")) self.library_state_changed.setObjectName("library_state_changed") self.library_state_changed.entered.connect(self.sync_metadata) self.off = QtCore.QState() self.off.setObjectName("off") self.off.entered.connect(lambda: self.disconnect_all()) self.off.assignProperty(self.debug_label, 'text', 'Start again...') self.on.addTransition(self.lets_share_button.clicked, self.calibre_web_server) self.calibre_web_server.addTransition(self.lets_share_button.clicked, self.off) self.calibre_web_server.addTransition(self.calibre_didnt_start, self.off) self.calibre_web_server.addTransition(self.started_calibre_web_server, self.ssh_server) self.ssh_server.addTransition(self.lets_share_button.clicked, self.off) self.ssh_server.addTransition(self.check_connection.lost_connection, self.off) self.ssh_server.addTransition(self.established_ssh_tunnel, self.ssh_server_established) self.ssh_server_established.addTransition(self.lets_share_button.clicked, self.off) self.ssh_server_established.addTransition(self.url_label.clicked, self.url_label_clicked) self.ssh_server_established.addTransition(self.about_project_button.clicked, self.about_project_clicked) self.ssh_server_established.addTransition(self.check_connection.lost_connection, self.off) self.ssh_server_established.addTransition(self.us.library_changed, self.library_state_changed) self.url_label_clicked.addTransition(self.ssh_server_established) self.about_project_clicked.addTransition(self.ssh_server_established) self.library_state_changed.addTransition(self.metadata_thread.uploaded, self.ssh_server_established) self.library_state_changed.addTransition(self.metadata_thread.upload_error, self.off) self.library_state_changed.addTransition(self.lets_share_button.clicked, self.off) self.library_state_changed.addTransition(self.url_label.clicked, self.url_label_clicked) self.off.addTransition(self.on) self.machine.addState(self.on) self.machine.addState(self.calibre_web_server) self.machine.addState(self.ssh_server) self.machine.addState(self.ssh_server_established) self.machine.addState(self.url_label_clicked) self.machine.addState(self.about_project_clicked) self.machine.addState(self.library_state_changed) self.machine.addState(self.off) self.machine.setInitialState(self.on) self.machine.start()