async def test_get_entry(enable_chant, enable_api, session): """ Test getting an entry with REST API GET request """ for md_type, kwargs in ( ( session.mds.TorrentMetadata, { "title": "bla", "infohash": random_infohash(), "tracker_info": "http://sometracker.local/announce" }, ), ( session.mds.ChannelDescription, { "text": dumps({ "description_text": "*{{}bla <\\> [)]// /ee2323㋛㋛㋛ ", "channel_thumbnail": "ffffff.jpg" }) }, ), ): with db_session: md = md_type(**kwargs) md.status = COMMITTED await do_request( session, 'metadata/%s/%i' % (hexlify(md.public_key), md.id_), expected_json=md.to_simple_dict(), )
def on_confirm_clicked(channel_id): TriblerNetworkRequest( f"collections/mychannel/{channel_id}/copy", lambda _: self.table_view.window().tray_show_message( "Channel update", "Torrent(s) added to your channel"), raw_data=dumps(entries), method='POST', )
def save_to_file(self, filename, data): base_dir = QFileDialog.getExistingDirectory(self, "Select an export directory", "", QFileDialog.ShowDirsOnly) if len(base_dir) > 0: dest_path = os.path.join(base_dir, filename) try: with open(dest_path, "w") as torrent_file: torrent_file.write(json.dumps(data)) except IOError as exc: ConfirmationDialog.show_error(self.window(), "Error exporting file", str(exc))
def on_confirm_clicked(channel_id): changes_list = [{ 'public_key': entry['public_key'], 'id': entry['id'], 'origin_id': channel_id } for entry in entries] TriblerNetworkRequest("metadata", self.model.remove_items, raw_data=dumps(changes_list), method='PATCH')
async def test_get_channel_description(enable_chant, enable_api, session): """ Test getting description of the channel from the database """ descr_txt = "foobar" with db_session: chan = session.mds.ChannelMetadata.create_channel(title="bla") channel_description = session.mds.ChannelDescription( origin_id=chan.id_, json_text=dumps({"description_text": descr_txt})) response_dict = await do_request( session, f'channels/{hexlify(chan.public_key)}/{chan.id_}/description', expected_code=200) assert response_dict == loads(channel_description.json_text)
async def put_channel_description(self, request): channel_pk, channel_id = self.get_channel_from_request(request) request_parsed = await request.json() updated_json_text = dumps({"description_text": request_parsed["description_text"]}) with db_session: channel_description = self.session.mds.ChannelDescription.select( lambda g: g.public_key == channel_pk and g.origin_id == channel_id ).first() if channel_description is not None: channel_description.update_properties({"json_text": updated_json_text}) else: channel_description = self.session.mds.ChannelDescription( public_key=channel_pk, origin_id=channel_id, json_text=updated_json_text, status=NEW ) return RESTResponse(loads(channel_description.json_text))
def on_emptying_tokens(self, data): if not data: return json_data = json.dumps(data) if has_qr: self.empty_tokens_barcode_dialog = QWidget() self.empty_tokens_barcode_dialog.setWindowTitle("Please scan the following QR code") self.empty_tokens_barcode_dialog.setGeometry(10, 10, 500, 500) qr = qrcode.QRCode(version=1, error_correction=qrcode.constants.ERROR_CORRECT_M, box_size=10, border=5) qr.add_data(json_data) qr.make(fit=True) img = qr.make_image() # PIL format qim = ImageQt(img) pixmap = QtGui.QPixmap.fromImage(qim).scaled(600, 600, QtCore.Qt.KeepAspectRatio) label = QLabel(self.empty_tokens_barcode_dialog) label.setPixmap(pixmap) self.empty_tokens_barcode_dialog.resize(pixmap.width(), pixmap.height()) self.empty_tokens_barcode_dialog.show() else: ConfirmationDialog.show_error(self.window(), DEPENDENCY_ERROR_TITLE, DEPENDENCY_ERROR_MESSAGE)
def update(self, version_id): if str(self.last_version) == str(version_id): return self.version_history["last_version"] = version_id self.version_history["history"][str(time.time())] = version_id self.file_path.write_text(json.dumps(self.version_history))
def on_event_clicked(self, item): event_dict = item.data(0, Qt.UserRole) self.window().event_text_box.setPlainText(json.dumps(event_dict))
def save_settings(self, checked): # Create a dictionary with all available settings settings_data = { 'general': {}, 'Tribler': {}, 'download_defaults': {}, 'libtorrent': {}, 'watch_folder': {}, 'tunnel_community': {}, 'trustchain': {}, 'resource_monitor': {}, 'ipv8': {}, 'chant': {}, } settings_data['download_defaults']['saveas'] = self.window( ).download_location_input.text() settings_data['general']['log_dir'] = self.window( ).log_location_input.text() settings_data['watch_folder']['enabled'] = self.window( ).watchfolder_enabled_checkbox.isChecked() if settings_data['watch_folder']['enabled']: settings_data['watch_folder']['directory'] = self.window( ).watchfolder_location_input.text() settings_data['libtorrent']['proxy_type'] = self.window( ).lt_proxy_type_combobox.currentIndex() if (self.window().lt_proxy_server_input.text() and len(self.window().lt_proxy_server_input.text()) > 0 and len(self.window().lt_proxy_port_input.text()) > 0): try: settings_data['libtorrent']['proxy_server'] = "{}:{}".format( self.window().lt_proxy_server_input.text(), int(self.window().lt_proxy_port_input.text()), ) except ValueError: ConfirmationDialog.show_error( self.window(), tr("Invalid proxy port number"), tr("You've entered an invalid format for the proxy port number. Please enter a whole number." ), ) return else: settings_data['libtorrent']['proxy_server'] = ":" if self.window().lt_proxy_username_input.text() and self.window( ).lt_proxy_password_input.text(): settings_data['libtorrent']['proxy_auth'] = "{}:{}".format( self.window().lt_proxy_username_input.text(), self.window().lt_proxy_password_input.text(), ) else: settings_data['libtorrent']['proxy_auth'] = ":" settings_data['libtorrent']['utp'] = self.window( ).lt_utp_checkbox.isChecked() try: max_conn_download = int( self.window().max_connections_download_input.text()) except ValueError: ConfirmationDialog.show_error( self.window(), tr("Invalid number of connections"), tr("You've entered an invalid format for the maximum number of connections. " "Please enter a whole number."), ) return if max_conn_download == 0: max_conn_download = -1 settings_data['libtorrent'][ 'max_connections_download'] = max_conn_download try: if self.window().upload_rate_limit_input.text(): user_upload_rate_limit = int( float(self.window().upload_rate_limit_input.text()) * 1024) if user_upload_rate_limit < MAX_LIBTORRENT_RATE_LIMIT: settings_data['libtorrent'][ 'max_upload_rate'] = user_upload_rate_limit else: raise ValueError if self.window().download_rate_limit_input.text(): user_download_rate_limit = int( float(self.window().download_rate_limit_input.text()) * 1024) if user_download_rate_limit < MAX_LIBTORRENT_RATE_LIMIT: settings_data['libtorrent'][ 'max_download_rate'] = user_download_rate_limit else: raise ValueError except ValueError: ConfirmationDialog.show_error( self.window(), tr("Invalid value for bandwidth limit"), tr("You've entered an invalid value for the maximum upload/download rate. \n" "The rate is specified in KB/s and the value permitted is between 0 and %d KB/s.\n" "Note that the decimal values are truncated.") % (MAX_LIBTORRENT_RATE_LIMIT / 1024), ) return try: if self.window().api_port_input.text(): api_port = int(self.window().api_port_input.text()) if api_port <= 0 or api_port >= 65536: raise ValueError() self.window().gui_settings.setValue("api_port", api_port) except ValueError: ConfirmationDialog.show_error( self.window(), tr("Invalid value for api port"), tr("Please enter a valid port for the api (between 0 and 65536)" ), ) return seeding_modes = ['forever', 'time', 'never', 'ratio'] selected_mode = 'forever' for seeding_mode in seeding_modes: if getattr(self.window(), "seeding_" + seeding_mode + "_radio").isChecked(): selected_mode = seeding_mode break settings_data['download_defaults']['seeding_mode'] = selected_mode settings_data['download_defaults']['seeding_ratio'] = float( self.window().seeding_ratio_combobox.currentText()) try: settings_data['download_defaults'][ 'seeding_time'] = string_to_seconds( self.window().seeding_time_input.text()) except ValueError: ConfirmationDialog.show_error( self.window(), tr("Invalid seeding time"), tr("You've entered an invalid format for the seeding time (expected HH:MM)" ), ) return settings_data['tunnel_community']['exitnode_enabled'] = self.window( ).allow_exit_node_checkbox.isChecked() settings_data['download_defaults']['number_hops'] = self.window( ).number_hops_slider.value() settings_data['download_defaults']['anonymity_enabled'] = self.window( ).download_settings_anon_checkbox.isChecked() settings_data['download_defaults'][ 'safeseeding_enabled'] = self.window( ).download_settings_anon_seeding_checkbox.isChecked() settings_data['download_defaults'][ 'add_download_to_channel'] = self.window( ).download_settings_add_to_channel_checkbox.isChecked() settings_data['resource_monitor']['enabled'] = self.window( ).checkbox_enable_resource_log.isChecked() settings_data['resource_monitor']['cpu_priority'] = int( self.window().slider_cpu_level.value()) # network statistics settings_data['ipv8']['statistics'] = self.window( ).checkbox_enable_network_statistics.isChecked() self.window().settings_save_button.setEnabled(False) # TODO: do it in RESTful style, on the REST return JSON instead # In case the default save dir has changed, add it to the top of the list of last download locations. # Otherwise, the user could absentmindedly click through the download dialog and start downloading into # the last used download dir, and not into the newly designated default download dir. if self.settings['download_defaults']['saveas'] != settings_data[ 'download_defaults']['saveas']: self.window().update_recent_download_locations( settings_data['download_defaults']['saveas']) self.settings = settings_data TriblerNetworkRequest("settings", self.on_settings_saved, method='POST', raw_data=json.dumps(settings_data))
def save(self): self.file_data["last_version"] = self.code_version.version_str self.file_data["history"][str(self.code_version.last_launched_at )] = self.code_version.version_str self.file_path.write_text(json.dumps(self.file_data))
def save_settings(self): # Create a dictionary with all available settings settings_data = { 'general': {}, 'Tribler': {}, 'download_defaults': {}, 'libtorrent': {}, 'watch_folder': {}, 'tunnel_community': {}, 'market_community': {}, 'trustchain': {}, 'resource_monitor': {}, 'ipv8': {}, 'chant': {}, } settings_data['download_defaults']['saveas'] = self.window().download_location_input.text() settings_data['general']['log_dir'] = self.window().log_location_input.text() settings_data['watch_folder']['enabled'] = self.window().watchfolder_enabled_checkbox.isChecked() if settings_data['watch_folder']['enabled']: settings_data['watch_folder']['directory'] = self.window().watchfolder_location_input.text() settings_data['market_community']['enabled'] = self.window().enable_market_checkbox.isChecked() settings_data['libtorrent']['proxy_type'] = self.window().lt_proxy_type_combobox.currentIndex() if ( self.window().lt_proxy_server_input.text() and len(self.window().lt_proxy_server_input.text()) > 0 and len(self.window().lt_proxy_port_input.text()) > 0 ): try: settings_data['libtorrent']['proxy_server'] = "%s:%s" % ( self.window().lt_proxy_server_input.text(), int(self.window().lt_proxy_port_input.text()), ) except ValueError: ConfirmationDialog.show_error( self.window(), "Invalid proxy port number", "You've entered an invalid format for the proxy port number. " "Please enter a whole number.", ) return else: settings_data['libtorrent']['proxy_server'] = ":" if self.window().lt_proxy_username_input.text() and self.window().lt_proxy_password_input.text(): settings_data['libtorrent']['proxy_auth'] = "%s:%s" % ( self.window().lt_proxy_username_input.text(), self.window().lt_proxy_password_input.text(), ) else: settings_data['libtorrent']['proxy_auth'] = ":" settings_data['libtorrent']['utp'] = self.window().lt_utp_checkbox.isChecked() try: max_conn_download = int(self.window().max_connections_download_input.text()) except ValueError: ConfirmationDialog.show_error( self.window(), "Invalid number of connections", "You've entered an invalid format for the maximum number of connections. " "Please enter a whole number.", ) return if max_conn_download == 0: max_conn_download = -1 settings_data['libtorrent']['max_connections_download'] = max_conn_download try: if self.window().upload_rate_limit_input.text(): user_upload_rate_limit = int(self.window().upload_rate_limit_input.text()) * 1024 if user_upload_rate_limit < sys.maxsize: settings_data['libtorrent']['max_upload_rate'] = user_upload_rate_limit else: raise ValueError if self.window().download_rate_limit_input.text(): user_download_rate_limit = int(self.window().download_rate_limit_input.text()) * 1024 if user_download_rate_limit < sys.maxsize: settings_data['libtorrent']['max_download_rate'] = user_download_rate_limit else: raise ValueError except ValueError: ConfirmationDialog.show_error( self.window(), "Invalid value for bandwidth limit", "You've entered an invalid value for the maximum upload/download rate. " "Please enter a whole number (max: %d)" % (sys.maxsize / 1000), ) return try: if self.window().api_port_input.text(): api_port = int(self.window().api_port_input.text()) if api_port <= 0 or api_port >= 65536: raise ValueError() self.window().gui_settings.setValue("api_port", api_port) except ValueError: ConfirmationDialog.show_error( self.window(), "Invalid value for api port", "Please enter a valid port for the api (between 0 and 65536)", ) return seeding_modes = ['forever', 'time', 'never', 'ratio'] selected_mode = 'forever' for seeding_mode in seeding_modes: if getattr(self.window(), "seeding_" + seeding_mode + "_radio").isChecked(): selected_mode = seeding_mode break settings_data['download_defaults']['seeding_mode'] = selected_mode settings_data['download_defaults']['seeding_ratio'] = float(self.window().seeding_ratio_combobox.currentText()) try: settings_data['download_defaults']['seeding_time'] = string_to_seconds( self.window().seeding_time_input.text() ) except ValueError: ConfirmationDialog.show_error( self.window(), "Invalid seeding time", "You've entered an invalid format for the seeding time (expected HH:MM)", ) return settings_data['tunnel_community']['exitnode_enabled'] = self.window().allow_exit_node_checkbox.isChecked() settings_data['download_defaults']['number_hops'] = self.window().number_hops_slider.value() settings_data['download_defaults'][ 'anonymity_enabled' ] = self.window().download_settings_anon_checkbox.isChecked() settings_data['download_defaults'][ 'safeseeding_enabled' ] = self.window().download_settings_anon_seeding_checkbox.isChecked() settings_data['download_defaults'][ 'add_download_to_channel' ] = self.window().download_settings_add_to_channel_checkbox.isChecked() settings_data['resource_monitor']['enabled'] = self.window().checkbox_enable_resource_log.isChecked() settings_data['resource_monitor']['cpu_priority'] = int(self.window().slider_cpu_level.value()) # network statistics settings_data['ipv8']['statistics'] = self.window().checkbox_enable_network_statistics.isChecked() self.window().settings_save_button.setEnabled(False) TriblerNetworkRequest("settings", self.on_settings_saved, method='POST', raw_data=json.dumps(settings_data))