def handle_start(message): info_hash = message.get('info_hash', []) try: client.get().torrent_start(info_hash) except ClientNotAvailable as exc: api.error_handler(exc, api.EVENT_TORRENT_START_RESPONSE) else: api.emit(api.EVENT_TORRENT_START_RESPONSE, dict(info_hash=info_hash)) api.flash("Started successfully")
def handle_remove(message): info_hash = message.get('info_hash', None) remove_data = message.get('remove_data', False) torrent = client.get().torrent_status(info_hash) if not torrent: status = api.STATUS_INVALID_INFO_HASH else: if client.get().torrent_remove(info_hash, remove_data=remove_data): status = api.STATUS_OK else: status = api.STATUS_FAIL api.emit(api.EVENT_TORRENT_REMOVE_RESPONSE, data=dict(info_hash=info_hash), status=status)
def system(): """ Show system / OS info # TODO Better Win/OSX/BSD? support. :return: System info :rtype: dict """ about_info = OrderedDict() about_info["Hostname"] = platform.node() about_info["Platform"] = "{0} ({1})".format(platform.platform(), platform.architecture()[0]) about_info["Python"] = "{0} {1}".format(platform.python_implementation(), platform.python_version()) about_info["Uptime-Sys"] = time.strftime("%H:%M:%S", time.gmtime(util.uptime_sys())) about_info["Uptime-App"] = time.strftime("%H:%M:%S", time.gmtime(util.uptime_app())) try: if hasattr(sys, "real_prefix"): about_info["Distribution"] = "VirtualEnv" else: distro = platform.linux_distribution() if distro[0]: about_info["Distribution"] = "{0} {1} {2}".format(distro[0], distro[1], distro[2]) except IndexError: pass # Get disk info and sort it by path disk_info = util.disk_free() sorted_disk_info = OrderedDict() for key in sorted(disk_info.keys()): sorted_disk_info[key] = disk_info[key] # Get arbitary client information client_name = config.get("general", "client") client_info = client.get().client_information() return dict(info=about_info, disk_info=sorted_disk_info, client_name=client_name, client_info=client_info)
def handle_speed_overall(message=None): try: up, dn = client.get().current_speeds() except ClientNotAvailable as exc: api.error_handler(exc, api.EVENT_SPEED_OVERALL_RESPONSE) else: api.emit(api.EVENT_SPEED_OVERALL_RESPONSE, dict(up=up, dn=dn))
def handle_speed(message): info_hash = message.get('info_hash', None) if info_hash: tor_speed = client.get().torrent_speed(info_hash) api.emit(api.EVENT_TORRENT_SPEED_RESPONSE, data=tor_speed) else: api.emit(api.EVENT_TORRENT_SPEED_RESPONSE, status=api.STATUS_FAIL)
def add(self, torrent, service, dl_path=None): """ Handles adding a new torrent to the system. This should be considered the main entry point of doing this to make sure things are consistent, this cannot be guaranteed otherwise :param torrent: Torrent data named tuple containing relevant values :type torrent: TorrentData :param service: The TorrentProvider instanced used to get the torrent :type service: TorrentProvider :param dl_path: Optional download path to use for the torrent """ try: if not dl_path: dl_path = app.config.get_download_path(torrent.section, torrent.release_name) res = client.get().add(torrent, download_dir=dl_path) if res: app.logger.info("Added release: {0}".format(torrent.release_name)) release_key = datastore.generate_release_key(torrent.release_name) section = datastore.get_section(torrent.section) source = datastore.get_source(service.name) download = models.DownloadEntity(release_key, torrent.release_name, section.section_id, source.source_id) db.session.add(download) db.session.commit() except DBAPIError as err: app.logger.exception(err) db.session.rollback() except Exception as err: app.logger.exception(err)
def handle_list_all(): """ Return a list of all torrents currently being tracked """ try: torrent_list = client.get().torrent_list() except Exception as exc: api.error_handler(exc, api.EVENT_TORRENT_LIST_RESPONSE) else: api.emit(api.EVENT_TORRENT_LIST_RESPONSE, data=torrent_list)
def handle_announce(message): """ (re)announce the torrent(s) to the tracker :param message: :type message: dict :return: :rtype: """ info_hash = message.get('data', []) status = api.STATUS_OK if client.get().torrent_reannounce(info_hash) else api.STATUS_FAIL api.emit(api.EVENT_TORRENT_ANNOUNCE_RESPONSE, status=status)
def handle_details(message): try: info_hash = message.get('info_hash', None) data = dict() if info_hash: data = client.get().torrent_status(info_hash) status = api.STATUS_OK else: status = api.STATUS_INCOMPLETE_REQUEST except ClientNotAvailable as exc: api.error_handler(exc, api.EVENT_TORRENT_DETAILS_RESPONSE) else: api.emit(api.EVENT_TORRENT_DETAILS_RESPONSE, data=data, status=status)
def system(): """ Show system / OS info # TODO Better Win/OSX/BSD? support. :return: System info :rtype: dict """ about_info = OrderedDict() about_info['Hostname'] = platform.node() about_info['Platform'] = "{0} ({1})".format(platform.platform(), platform.architecture()[0]) about_info['Python'] = "{0} {1}".format(platform.python_implementation(), platform.python_version()) about_info['Uptime-Sys'] = time.strftime("%H:%M:%S", time.gmtime(util.uptime_sys())) about_info['Uptime-App'] = time.strftime("%H:%M:%S", time.gmtime(util.uptime_app())) try: if hasattr(sys, "real_prefix"): about_info['Distribution'] = "VirtualEnv" else: distro = platform.linux_distribution() if distro[0]: about_info['Distribution'] = "{0} {1} {2}".format( distro[0], distro[1], distro[2]) except IndexError: pass # Get disk info and sort it by path disk_info = util.disk_free() sorted_disk_info = OrderedDict() for key in sorted(disk_info.keys()): sorted_disk_info[key] = disk_info[key] # Get arbitary client information client_name = config.get("general", "client") client_info = client.get().client_information() return dict(info=about_info, disk_info=sorted_disk_info, client_name=client_name, client_info=client_info)
def add(session, service, torrent, release_info, dl_path=None): """ Handles adding a new torrent to the system. This should be considered the main entry point of doing this to make sure things are consistent, this cannot be guaranteed otherwise :param release_info: Release info instance :type release_info: ReleaseInfo :param session: DB Session :type session: sqlalchemy.orm.session.Session :param torrent: Torrent data named tuple containing relevant values :type torrent: TorrentData :param service: The TorrentProvider instanced used to get the torrent :type service: TorrentProvider :param dl_path: Optional download path to use for the torrent """ status = False try: if not dl_path: dl_path = app.config.get_download_path(torrent.section, release_info) res = client.get().add(torrent, download_dir=dl_path) if not res: raise ClientError log.info("Added release: {0}".format(torrent.release_name)) release_key = release_info.release_key section = datastore.get_section(session, torrent.section) source = datastore.get_source(session, service.name) download = Download(release_key.as_unicode(), torrent.release_name, section.section_id, source.source_id) session.add(download) session.commit() except DBAPIError as err: log.exception(err) session.rollback() except ClientError: log.warning("Could not add torrent to client backend") except Exception as err: log.exception(err) else: metadata.update_media_info(release_key) status = True return status
def handle_recheck(message): info_hash = message.get('info_hash', []) resp = client.get().torrent_recheck(info_hash) api.emit(api.EVENT_TORRENT_RECHECK_RESPONSE, resp) api.flash("Rechecking..!")
def handle_event_update(message): events = client.get().get_events() api.emit(api.EVENT_UPDATE_RESPONSE)
def handle_peers(message): info_hash = message.get('info_hash', None) data = client.get().torrent_peers(info_hash) api.emit(api.EVENT_TORRENT_PEERS_RESPONSE, data=data)
def handle_files(message): info_hash = message.get('info_hash', None) tor_files = client.get().torrent_files(info_hash) api.emit(api.EVENT_TORRENT_FILES_RESPONSE, tor_files)