Skip to content

Instantly share code, notes, and snippets.

@Kenya-West
Last active October 23, 2023 17:11
Show Gist options
  • Save Kenya-West/f2eecbac9f7d7db539acbaa352dbe92e to your computer and use it in GitHub Desktop.
Save Kenya-West/f2eecbac9f7d7db539acbaa352dbe92e to your computer and use it in GitHub Desktop.
VK get posts in feed
// This is a script to get posts from feed, mark them with emoji (similar to reaction you set on post)
// In the end, it returns a text you can copy and post directly to VK.
// The text will reference all the posts you've reacted to.
const config = {
timeToLoad: 1000,
timeToUnload: 350,
zeroResultLimit: 10
};
// set ids of people, public pages or groups here. You can add as many as you want
// vk.com/[id_of_page], like vk.com/internetpasta
const sources = [ "internetpasta", "internet_2_0", "pastachn", "pastch", "lurkopub", "aveshit", "lurking", "bugurt_thread", "copyipasta", "imbetarded" ];
let postList = new Set();
let zeroResultCounter = 0;
// HERE WE GO
const timer = setInterval( async ()=> {
await work();
if (zeroResultCounter >= config.zeroResultLimit) {
clearInterval(timer)
console.dir(Array.from(postList));
};
}, config.timeToLoad + (zeroResultCounter * (config.timeToUnload)));
// THESE ARE THE FUNCTIONS
async function work() {
let delta = postList.size;
const posts = await getPosts();
extractDataFromPosts(posts).forEach((post) => postList.add(post));
delta = postList.size - delta;
if (delta === 0) { zeroResultCounter++ } else { zeroResultCounter = 0 };
window.scrollTo(0, document.body.scrollHeight);
sleep(config.timeToLoad);
}
async function getPosts() {
const posts = document.querySelectorAll(
`#feed_rows .post:has(.PostHeader__avatar:is(${sources.map((id) => `[href="/${id}"]`).join(",")}}))`
);
const postsArr = Array.from(posts);
console.log(`Found ${postsArr.length} posts, proceed to process...`);
return postsArr;
}
/**
*
* @param {HTMLElement[]} posts
* @returns {string[]}
*/
function extractDataFromPosts(posts) {
return posts.map((post) => {
const data = {
text: getPostText(post),
reaction: getReaction(post),
link: getLinkToPost(post),
shortText: getShortString(getPostText(post)).replace(/^[ ]+/g, ``).replace(/\n/g, ` `).replace(/\s+/g, ` `).replace(/^\#[A-Za-zА-Яа-я_]/g, ``)
}
// unLikePost(posts); // enabling this line causes VK's CAPTCHA to appear
return `@${data.link} (${data.reaction + ` ` ?? ``}"${data.shortText}")`;
});
}
/**
*
* @param {HTMLElement} post
* @returns {void}
*/
function unLikePost(post) {
post.querySelector(".post_content .like_wrap .PostButtonReactions[data-reaction-user-reaction-id]").click();
}
/**
*
* @param {HTMLElement} post
* @returns {string}
*/
function getPostText(post) {
return post.querySelector(`.post_content .wall_text .wall_post_text`)?.innerText ?? 'Картинка';
}
/**
*
* @param {HTMLElement} post
* @returns {string}
*/
function getReaction(post) {
const reactionIdMap = {
0: "💩",
1: "😂",
2: "🤔",
3: "😂",
4: "😱",
5: "😢"
}
// map this reactionIdMap with given string
return reactionIdMap[post.querySelector(`.post_content .like_wrap .PostButtonReactions[data-reaction-user-reaction-id]`).getAttribute(`data-reaction-user-reaction-id`)] ?? "";
}
/**
*
* @param {HTMLElement} post
* @returns {string}
*/
function getLinkToPost(post) {
return `https://vk.com${post.querySelector(`.post_link[href]`).getAttribute(`href`)}`;
}
/**
*
* @param {string} text
* @param {string} delimiter
* @param {number} length
* @returns
*/
function getShortString(text, delimiter = ".", length = 100) {
if (text.length <= length) {
return text;
} else {
let shortText = text.substring(0, 100);
let indexOfDot = shortText.lastIndexOf(delimiter);
if (indexOfDot === -1) {
return shortText + "...";
} else {
return shortText.substring(0, indexOfDot);
}
}
}
/**
*
* @param {number} ms
* @returns Promise
*/
function sleep(ms) {
return new Promise((r) => setTimeout(r, ms));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment