class QBittorrentClient(TorrentClient): def __init__(self, endpoint, username, password): self._api_client = Client(endpoint) self._api_client.login(username, password) if not self._api_client: message = "Unable to connect to qBittorrent API. Please check your -e, -u and -p arguments." logger.error(message) raise Exception(message) def get_torrent_info(self, torrent_hash): torrents = self._api_client.torrents() for torrent in torrents: if torrent['hash'] == torrent_hash: return Torrent(torrent_hash, torrent['progress'] == 1, torrent['category'], torrent['save_path']) return None def remove_torrent(self, torrent_hash): self._api_client.delete_permanently(torrent_hash) def stop_torrent(self, torrent_hash): self._api_client.pause(torrent_hash) def get_torrent_files(self, torrent_hash): files = self._api_client.get_torrent_files(torrent_hash) parsed_files = [] for file in files: if ".unwanted" not in file['name']: parsed_files.append(TorrentFile(file['name'])) return parsed_files
delete_dir = False host = getHost(settings.qBittorrent['host'], settings.qBittorrent['port'], settings.qBittorrent['ssl']) qb = Client(host) qb.login(settings.qBittorrent['username'], settings.qBittorrent['password']) if settings.qBittorrent['actionbefore']: if settings.qBittorrent[ 'actionbefore'] == 'pause': # currently only support pausing log.debug("Sending action %s to qBittorrent" % settings.qBittorrent['actionbefore']) qb.pause(torrent_hash) if settings.qBittorrent['convert']: # Check for custom qBittorrent output_dir if settings.qBittorrent['output_dir']: settings.output_dir = settings.qBittorrent['output_dir'] log.debug("Overriding output_dir to %s." % settings.qBittorrent['output_dir']) # Perform conversion. log.info("Performing conversion") settings.delete = False if not settings.output_dir: # If the user hasn't set an output directory, go up one from the root path and create a directory there as [name]-convert suffix = "convert" settings.output_dir = os.path.abspath(
t = time.localtime() current_time = time.strftime("%H:%M:%S", t) temp_cTime = current_time.split(':') cTime_seconds = int(temp_cTime[0]) * (60 * 60) + int( temp_cTime[1]) * 60 + int(temp_cTime[2]) if (cTime_seconds < workingHours[1] and cTime_seconds > workingHours[0]): pass else: if int(temp_cTime[0]) > 6: print('Sleeping until midnight') time.sleep(10) #darn, the pause function isn't working too well. put this here for a cheap fix. todo: make this more stable. qb.pause(tor[0]['hash']) while (int(temp_cTime[0]) > 6): t = time.localtime() current_time = time.strftime("%H:%M:%S", t) temp_cTime = current_time.split(':') time.sleep(600) #this is all very inefficient :( print('waking up...') qb.resume(tor[0]['hash']) if (tor[0]['progress']) == 1: print('Finished downloading ' + tor[0]['name']) qb.delete(tor[0]['hash']) magnetLinks.pop(0) searchAr.pop(0) searchDoc.truncate(0)
if free_gb > MIN_SPACE_GB: log.info('Starting paused torrents...') torrents = qb.torrents(filter='paused') no_of_torrents = len(torrents) i = 0 for torrent in torrents: if DO_NOT_RESUME_TAG not in torrent['tags']: if DRY_RUN != 'yes': qb.resume(torrent['hash']) log.debug('Torrent name: %s started%s', torrent['name'], ' [SIMULATED]' if DRY_RUN == 'yes' else '') i = i + 1 else: log.debug('Torrent name: %s not resumed as tag %s avoids it%s', torrent['name'], DO_NOT_RESUME_TAG, ' [SIMULATED]' if DRY_RUN == 'yes' else '') log.info('Started %d of %d torrents.', i, no_of_torrents) else: log.info('Pausing active torrents...') torrents = qb.torrents(filter='downloading') no_of_torrents = len(torrents) i = 0 for torrent in torrents: if torrent['state'] == 'downloading' or torrent['state'] == 'queuedDL' or torrent['state'] == 'stalledDL': if DO_NOT_PAUSE_TAG not in torrent['tags']: if DRY_RUN != 'yes': qb.pause(torrent['hash']) log.debug('Torrent name: %s paused%s', torrent['name'], ' [SIMULATED]' if DRY_RUN == 'yes' else '') i = i + 1 else: log.debug('Torrent name: %s not paused as tag %s avoids it%s', torrent['name'], DO_NOT_PAUSE_TAG, ' [SIMULATED]' if DRY_RUN == 'yes' else '') log.info('Paused %d of %d torrents.', i, no_of_torrents) qb.logout()
def main(): done = False try: qb = Client('http://127.0.0.1:8080/') qb.login() except: print("Error with qb client") paths = dict([(f, None) for f in os.listdir(".")]) if "watch_path" in paths: watch_path_f = open('watch_path') watch_path = watch_path_f().read() else: watch_path_f = open("watch_path.txt", "w+") watch_path_f.write(".") watch_path = "." watch_path_f.close() if "download_save_path" in paths: download_save_path_f = open('download_save_path') download_save_path = download_save_path_f().read() else: download_save_path_f = open("download_save_path.txt", "w+") download_save_path_f.write(".") download_save_path = "." download_save_path_f.close() original_files_in_path = dict([(f, None) for f in os.listdir(watch_path)]) print("--TorrentChecker--") while not done: try: time.sleep(15) current_files_in_path = dict([(f, None) for f in os.listdir(watch_path)]) new_files_in_path = [ f for f in current_files_in_path if f not in original_files_in_path ] removed_files_in_path = [ f for f in original_files_in_path if f not in current_files_in_path ] if new_files_in_path: print("Added: ", ", ".join(new_files_in_path)) for f in new_files_in_path: try: file_with_magnet_link = open(f) qb.download_from_link(file_with_magnet_link.read() ) # savepath=download_save_path) file_with_magnet_link.close() torrents = qb.torrents(filter='downloading') for torrent in torrents: print("Downloading: " + torrent['name']) open(torrent['name'] + " - Downloading.txt", "w+") os.remove(f) except ValueError: done = True print("Error reading magnet link.") except: done = True print("Unexpected error") if removed_files_in_path: print("Removed: ", ", ".join(removed_files_in_path)) torrents = qb.torrents(filter='seeding') for torrent in torrents: print("Completed: " + torrent['name']) qb.pause(torrent['hash']) open(torrent['name'] + " - Completed.txt", "w+") original_files_in_path = dict([(f, None) for f in os.listdir(watch_path)]) except: done = True print("Unexpected error") input("Press any button to exit")