Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function writeJsonConsole(text) {
- console.log(JSON.stringify({
- data: text
- }));
- }
- function readPostsText() {
- return [...document.getElementsByClassName(['wall_post_text'])]
- .map(v => v.innerHTML)
- .filter(v => {
- const openGameIndex = v.indexOf('Открытая игра');
- const companyIndex = v.indexOf('Набор кампании');
- return (openGameIndex >= 0 && openGameIndex < 100)
- || (companyIndex >= 0 && companyIndex < 100);
- })
- .map(text => text.replaceAll('<br>', '|NEW_LINE|')
- .replaceAll(/<img[^>]*>/g, '|IMG|')
- .replaceAll(/<a[^>]*>/g, '|START_LINK|')
- .replaceAll(/<\/a[^>]*>/g, '|END_LINK|')
- .replaceAll(/<button[^а-яА-я]*>Показать ещё[^а-яА-я]*">/g, '')
- .replaceAll('</span>', ''))
- }
- function parsePostsParts(postsText) {
- return postsText
- .map(value => {
- const dates = value.match(/[А-ЯA-Z]{2} \d+(\/|\.)\d+ \d+:\d+/) ?? [];
- const masters = (value.match(/Мастер игры: \|START_LINK\|[^|]+/) ?? [])
- .map(v => v.replace(/[^|]+\|START_LINK\|/, '')) ?? [];
- const prices = (value.match(/стоимость участия - \d+/) ?? [])
- .flatMap(v => v.match(/\d+/) ?? []);
- const gamers = (value.match(/участников - \d+ человек/) ?? [])
- .flatMap(v => v.match(/\d+/) ?? []);
- const duration = (value.match(/Длительность [^-]+- \d+/) ?? [])
- .flatMap(v => v.match(/\d+/) ?? []);
- return {
- date: dates[0],
- master: masters[0],
- price: +prices[0],
- gamers: +gamers[0],
- duration: +duration[0]
- }
- });
- }
- function parsePostDates(posts) {
- return posts.map(post => {
- const dateValues = post.date.split(' ');
- const timeValues = dateValues[2]?.split(':')
- return {
- day: dateValues[0],
- time: {
- hour: +timeValues[0],
- minute: +timeValues[1],
- }
- };
- });
- }
- function calculateDates(dates) {
- const notSalesDays = ['ПТ', 'СБ', 'ВС',]
- const notSalesTime = 18;
- return dates.reduce((acc, cur) => {
- if (notSalesDays.includes(cur.day) || cur.time.hour >= notSalesTime) {
- acc.full++;
- } else {
- acc.sales++;
- }
- return acc;
- }, { sales: 0, full: 0 });
- }
- const input = readPostsText()
- const partsOfPosts = parsePostsParts(input);
- const gameTimes = parsePostDates(partsOfPosts);
- const result = calculateDates(gameTimes);
- console.log(result);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement