@isereb/tiktok-api-dl
Version:
Scraper for downloading media in the form of videos, images and audio from Tiktok. Also for stalking Tiktok Users, getting user posts, likes, comments, followers and following.
116 lines (115 loc) • 4.87 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getDefaultDownloadPath = getDefaultDownloadPath;
exports.downloadMedia = downloadMedia;
exports.handleMediaDownload = handleMediaDownload;
const path = __importStar(require("path"));
const os = __importStar(require("os"));
const axios_1 = __importDefault(require("axios"));
const fs = __importStar(require("fs"));
const logger_1 = require("../lib/logger");
function getDefaultDownloadPath() {
const homeDir = os.homedir();
return path.join(homeDir, "Downloads");
}
async function downloadMedia(url, outputPath, filename) {
try {
const response = await (0, axios_1.default)({
method: "GET",
url: url,
responseType: "stream"
});
if (!fs.existsSync(outputPath)) {
fs.mkdirSync(outputPath, { recursive: true });
}
const writer = fs.createWriteStream(path.join(outputPath, filename));
response.data.pipe(writer);
return new Promise((resolve, reject) => {
writer.on("finish", resolve);
writer.on("error", reject);
});
}
catch (error) {
throw new Error(`Failed to download media: ${error.message}`);
}
}
async function handleMediaDownload(data, outputPath, version) {
if (data.status !== "success") {
throw new Error(data.message);
}
const { result } = data;
const author = result.author;
const username = version === "v1" ? author.username : author?.nickname || "";
logger_1.Logger.success(`${result.type.charAt(0).toUpperCase() + result.type.slice(1)} Successfully Fetched!`);
logger_1.Logger.info(`Media Type: ${result.type}`);
switch (result.type) {
case "video": {
const videoUrl = version === "v1"
? result.video.playAddr[0]
: version === "v2"
? result.video.playAddr[0]
: result.videoHD;
const videoName = `ttdl_${username}_${Date.now()}.mp4`;
logger_1.Logger.info("Downloading video...");
await downloadMedia(videoUrl, outputPath, videoName);
logger_1.Logger.success(`Video downloaded successfully to: ${path.join(outputPath, videoName)}`);
break;
}
case "image": {
const userOutputPath = path.join(outputPath, `${username}_${Date.now()}`);
const images = result.images;
for (let i = 0; i < images.length; i++) {
const imageName = `ttdl_${username}_${Date.now()}_${i + 1}.png`;
logger_1.Logger.info(`Downloading image ${i + 1}/${images.length}...`);
await downloadMedia(images[i], userOutputPath, imageName);
logger_1.Logger.success(`Image downloaded successfully to: ${path.join(userOutputPath, imageName)}`);
}
break;
}
case "music": {
const musicName = `ttdl_${username}_${Date.now()}.mp3`;
logger_1.Logger.info("Downloading music...");
await downloadMedia(result.music, outputPath, musicName);
logger_1.Logger.success(`Music downloaded successfully to: ${path.join(outputPath, musicName)}`);
break;
}
default:
throw new Error(`Unsupported media type: ${result.type}`);
}
}