tiktok-dl/scraper.js

183 lines
6.4 KiB
JavaScript
Raw Permalink Normal View History

2020-01-19 13:16:46 +00:00
optStrings = {
selectors: {
2020-01-19 19:15:42 +00:00
feedLoading: 'div.tiktok-loading.feed-loading',
modalArrowRight: 'div.video-card-modal > div > img.arrow-right',
2020-01-19 13:16:46 +00:00
modalClose: '.video-card-modal > div > div.close',
modalPlayer: 'div > div > main > div.video-card-modal > div > div.video-card-big > div.video-card-container > div > div > video',
modalShareInput: '.copy-link-container > input',
2020-01-20 17:42:34 +00:00
modalCaption: 'div.video-card-big > div.content-container > div.video-meta-info > h1',
modalSoundLink: 'div.content-container > div.video-meta-info > h2.music-info > a',
modalUploader: '.user-username',
videoPlayer: 'div.video-card-container > div > div > video',
videoShareInput: 'div.content-container.border > div.copy-link-container > input',
2020-01-20 17:42:34 +00:00
videoCaption: 'div.content-container.border > div.video-meta-info > h1',
videoSoundLink: 'div.content-container.border > div.video-meta-info > h2.music-info > a',
videoUploader: '.user-username',
2020-01-19 13:16:46 +00:00
},
classes: {
2020-01-19 19:15:42 +00:00
feedVideoItem: 'video-feed-item-wrapper',
2020-01-19 13:16:46 +00:00
modalCloseDisabled: 'disabled',
2020-02-25 18:12:01 +00:00
titleMessage: 'title',
2020-01-19 13:16:46 +00:00
},
tags: {
resultTag: 'video_urls',
resultParentTag: 'body',
},
attributes: {
src: "src",
},
2020-02-25 18:12:01 +00:00
tiktokMessages: [
"Couldn't find this account",
"No videos yet",
"Video currently unavailable",
],
2020-01-19 13:16:46 +00:00
};
2020-01-19 02:11:53 +00:00
currentState = {
preloadCount: 0,
finished: false,
limit: 0
};
2020-02-25 18:12:01 +00:00
checkForErrors = function() {
var titles = document.getElementsByClassName(optStrings.classes.titleMessage);
debugger;
if (titles && titles.length) {
var error = Array.from(titles).find(x => optStrings.tiktokMessages.includes(x.textContent)).textContent;
if (error) {
createVidUrlElement("ERR: " + error);
return true;
}
}
return false;
};
createVidUrlElement = function(outputObj) {
2020-01-19 13:16:46 +00:00
var urlSetElement = document.createElement(optStrings.tags.resultTag);
urlSetElement.innerText = JSON.stringify(outputObj);
2020-01-19 13:16:46 +00:00
document.getElementsByTagName(optStrings.tags.resultParentTag)[0].appendChild(urlSetElement);
currentState.finished = true;
};
2020-01-19 02:11:53 +00:00
2020-01-19 13:16:46 +00:00
buldVidUrlArray = function(finishCallback) {
2020-01-19 19:15:42 +00:00
var feedItem = document.getElementsByClassName(optStrings.classes.feedVideoItem)[0];
2020-01-19 02:11:53 +00:00
feedItem.click();
2020-01-19 13:16:46 +00:00
var videoArray = [];
2020-01-19 02:11:53 +00:00
var intervalID = window.setInterval(x => {
2020-01-19 13:16:46 +00:00
videoArray.push(getCurrentModalVideo());
if(currentState.limit > 0) {
if (videoArray.length >= currentState.limit) {
window.clearInterval(intervalID);
document.querySelector(optStrings.selectors.modalClose).click();
finishCallback(videoArray);
}
}
var arrowRight = document.querySelectorAll(optStrings.selectors.modalArrowRight)[0];
2020-01-19 13:16:46 +00:00
if (arrowRight.classList.contains(optStrings.classes.modalCloseDisabled)) {
2020-01-19 02:11:53 +00:00
window.clearInterval(intervalID);
2020-01-19 13:16:46 +00:00
document.querySelector(optStrings.selectors.modalClose).click();
finishCallback(videoArray);
2020-01-19 02:11:53 +00:00
} else {
arrowRight.click();
}
2020-01-19 19:15:42 +00:00
}, 20);
2020-01-19 02:11:53 +00:00
};
2020-01-19 13:16:46 +00:00
getCurrentModalVideo = function() {
var modalPlayer = document.querySelector(optStrings.selectors.modalPlayer);
var vidUrl = modalPlayer.getAttribute(optStrings.attributes.src);
var shareLink = document.querySelector(optStrings.selectors.modalShareInput).value;
2020-01-20 17:42:34 +00:00
var caption = document.querySelector(optStrings.selectors.modalCaption).textContent;
var soundLink = document.querySelector(optStrings.selectors.modalSoundLink);
var uploader = document.querySelector(optStrings.selectors.modalUploader).textContent;
2020-01-20 17:42:34 +00:00
var soundHref = soundLink.getAttribute("href");
var soundText = soundLink.text;
2020-01-19 13:16:46 +00:00
return {
url: vidUrl,
2020-01-20 17:42:34 +00:00
shareLink: shareLink,
caption: caption,
uploader: uploader,
2020-01-20 17:42:34 +00:00
sound: {
title: soundText,
link: soundHref,
},
2020-01-19 13:16:46 +00:00
};
};
2020-01-19 13:16:46 +00:00
getCurrentVideo = function() {
2020-02-25 18:12:01 +00:00
if(checkForErrors()) return;
var player = document.querySelector(optStrings.selectors.videoPlayer);
var vidUrl = player.getAttribute(optStrings.attributes.src);
var shareLink = document.querySelector(optStrings.selectors.videoShareInput).value;
2020-01-20 17:42:34 +00:00
var caption = document.querySelector(optStrings.selectors.videoCaption).textContent;
var soundLink = document.querySelector(optStrings.selectors.videoSoundLink);
var uploader = document.querySelector(optStrings.selectors.videoUploader).textContent;
2020-01-20 17:42:34 +00:00
var soundHref = soundLink.getAttribute("href");
var soundText = soundLink.text;
return {
url: vidUrl,
2020-01-20 17:42:34 +00:00
shareLink: shareLink,
caption: caption,
uploader: uploader,
2020-01-20 17:42:34 +00:00
sound: {
title: soundText,
link: soundHref,
},
};
};
scrollBottom = () => window.scrollTo(0, document.body.scrollHeight);
2020-01-19 02:11:53 +00:00
scrollWhileNew = function(finishCallback) {
var state = { count: 0 };
var intervalID = window.setInterval(x => {
scrollBottom();
2020-01-19 02:11:53 +00:00
var oldCount = state.count;
2020-01-19 19:15:42 +00:00
state.count = document.getElementsByClassName(optStrings.classes.feedVideoItem).length;
if(currentState.limit > 0) {
if (currentState.preloadCount >= currentState.limit || state.count >= currentState.limit) {
finishCallback(createVidUrlElement);
window.clearInterval(intervalID);
}
}
2020-02-25 18:12:01 +00:00
if(checkForErrors()) {
window.clearInterval(intervalID);
return;
} else if (state.count == 0) {
return;
2020-02-25 18:12:01 +00:00
}
2020-01-19 02:11:53 +00:00
if (oldCount !== state.count) {
currentState.preloadCount = state.count;
2020-01-19 02:11:53 +00:00
} else {
2020-01-19 19:15:42 +00:00
if (document.querySelector(optStrings.selectors.feedLoading)) {
return;
}
2020-01-19 02:11:53 +00:00
window.clearInterval(intervalID);
2020-01-19 19:15:42 +00:00
finishCallback(createVidUrlElement);
2020-01-19 02:11:53 +00:00
}
}, 1000);
};
bootstrapIteratingVideos = function(limit) {
currentState.limit = limit;
2020-01-19 19:15:42 +00:00
scrollWhileNew(buldVidUrlArray);
return 'bootstrapIteratingVideos';
2020-01-19 13:16:46 +00:00
};
bootstrapGetCurrentVideo = function() {
var video = getCurrentVideo();
createVidUrlElement(video);
return 'bootstrapGetCurrentVideo';
};
2020-01-19 02:11:53 +00:00
init = () => {
const newProto = navigator.__proto__;
delete newProto.webdriver;
navigator.__proto__ = newProto;
2020-01-19 19:15:42 +00:00
return 'script initialized';
2020-01-19 02:11:53 +00:00
};
2020-01-19 19:15:42 +00:00
init();