diff --git a/bin/index.mjs b/bin/index.mjs index 0834acf..7d24012 100755 --- a/bin/index.mjs +++ b/bin/index.mjs @@ -1,7 +1,7 @@ #!/usr/bin/env node import { Command } from 'commander'; -import { loadConfig, makeConfigIfNotExist } from '../build/utils/directory.js'; +import { loadConfig, makeConfigIfNotExist } from '../build/utils/configV2.js'; import { loginV2 } from '../build/qbittorrent/auth.js'; import { sendMessageV2 } from '../build/discord/api.js' import { buildTorrentAddedBody } from '../build/discord/messages.js' diff --git a/build/qbittorrent/api.js b/build/qbittorrent/api.js index b0ed52e..6b6fb40 100644 --- a/build/qbittorrent/api.js +++ b/build/qbittorrent/api.js @@ -102,6 +102,13 @@ export class QbittorrentApi { } }); } + async reannounce(infohash) { + await this.client.get(ApiEndpoints.reannounce, { + params: { + hashes: infohash, + } + }); + } } var ApiEndpoints; (function (ApiEndpoints) { @@ -114,6 +121,7 @@ var ApiEndpoints; ApiEndpoints["pauseTorrents"] = "/api/v2/torrents/pause"; ApiEndpoints["addTorrent"] = "/api/v2/torrents/add"; ApiEndpoints["deleteTorrents"] = "/api/v2/torrents/delete"; + ApiEndpoints["reannounce"] = "/api/v2/torrents/reannounce"; })(ApiEndpoints || (ApiEndpoints = {})); export const login = (qbittorrentSettings) => { return axios.get(`${qbittorrentSettings.url}${ApiEndpoints.login}`, { diff --git a/build/racing/add.js b/build/racing/add.js index f15723f..8c2a572 100644 --- a/build/racing/add.js +++ b/build/racing/add.js @@ -100,7 +100,7 @@ export const addTorrentToRace = async (api, settings, path, category) => { // Need to reannounce if (working === false) { logger.debug(`No working tracker. Will reannounce and sleep...`); - // TODO: API Reannounce + await api.reannounce(torrent.hash); await sleep(settings.REANNOUNCE_INTERVAL); attempts++; } diff --git a/build/utils/configV2.js b/build/utils/configV2.js new file mode 100644 index 0000000..74d664f --- /dev/null +++ b/build/utils/configV2.js @@ -0,0 +1,71 @@ +import path from 'node:path'; +import fs from 'node:fs'; +import os from 'node:os'; +import { defaultSettings } from './config.js'; +import { getLoggerV3 } from './logger.js'; +const getConfigDir = () => { + const homeDir = os.homedir(); + const configDir = path.join(homeDir, '.config/qbit-race/'); + return configDir; +}; +const getConfigPath = () => { + return path.join(getConfigDir(), 'config.json'); +}; +export const makeConfigIfNotExist = () => { + const logger = getLoggerV3(); + const configDir = getConfigDir(); + logger.debug(`config dir is ${configDir}`); + try { + const stats = fs.statSync(configDir); + if (stats.isDirectory() === true) { + logger.debug(`Dir exists, wont do anything`); + } + } + catch (e) { + // console.log(e.code); + // Probably didnt exist. Try to make + try { + fs.mkdirSync(configDir, { recursive: true }); + logger.debug(`Made dir`); + } + catch (e) { + logger.error(`Fail to make dir`); + process.exit(1); + } + } + // Now check config + const configFilePath = path.join(configDir, 'config.json'); + // Check if config exists + try { + const stats = fs.statSync(configFilePath); + if (stats.isFile() === true) { + logger.debug(`Config file exists. Will try reading it`); + const configFile = fs.readFileSync(configFilePath); + const config = JSON.parse(configFile.toString()); + return config; + } + } + catch (e) { + // Probably doesnt exist + logger.info(`Config does not exist. Writing it now...`); + const defaultConfigToWrite = JSON.stringify(defaultSettings, null, 2); + fs.writeFileSync(configFilePath, defaultConfigToWrite); + } +}; +/** + * loadConfig will attempt to read and then JSON.parse the file + * TODO: Validate its legit config + * + * If the directory / path to config does not exist it will throw! + * + * It's assumed makeConfigIfNotExist() has already been called. + */ +export const loadConfig = () => { + const configPath = getConfigPath(); + const configData = fs.readFileSync(configPath); + return JSON.parse(configData.toString()); +}; +// TODO: Future improvement: +// If an older config, missing keys, then modify it to add the new stuff +// +//# sourceMappingURL=configV2.js.map \ No newline at end of file diff --git a/src/utils/directory.ts b/src/utils/configV2.ts similarity index 100% rename from src/utils/directory.ts rename to src/utils/configV2.ts