From 10aeebb0603c59b39cec63f9bf608b14255d04e2 Mon Sep 17 00:00:00 2001 From: Alexei Bezborodov Date: Sat, 21 Oct 2023 09:43:44 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B2=D0=B0=D1=8F=20=D0=B2?= =?UTF-8?q?=D0=B5=D1=80=D1=81=D0=B8=D1=8F=20=D0=BF=D0=B0=D1=80=D1=81=D0=B5?= =?UTF-8?q?=D1=80=D0=B0=20=D0=B4=D0=BB=D1=8F=20=D1=80=D0=B8=D0=B0=20=D0=B8?= =?UTF-8?q?=20=D1=82=D0=B0=D1=81=D1=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LICENSE | 3 + README.md | 8 +++ news_parser.js | 200 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 211 insertions(+) create mode 100644 LICENSE create mode 100644 README.md create mode 100644 news_parser.js diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..237c39d --- /dev/null +++ b/LICENSE @@ -0,0 +1,3 @@ +Этот продукт является ОБЩЕСТВЕННЫМ ДОСТОЯНИЕМ и может быть использован КАК ЕСТЬ, со всеми достоинствами и недостатками, полностью или частично, кем угодно и в каких угодно целях БЕЗ КАКИХ-ЛИБО ОГРАНИЧЕНИЙ. + +This product is PUBLIC DOMAIN and may be used AS IS, with all advantages and faults, in whole or in part, by anyone for any purpose, WITHOUT ANY CONDITIONS. diff --git a/README.md b/README.md new file mode 100644 index 0000000..d08dbe3 --- /dev/null +++ b/README.md @@ -0,0 +1,8 @@ +# Парсер новостей + +* Убирает рекламу +* Выделяет заголовок, картинку, текст статьи +* Выставляет картинку размером 600 пикселей +* Текст выравнивает по ширине +* Указывает источник +* Для ЗВ готовит новость для КроссПостинга diff --git a/news_parser.js b/news_parser.js new file mode 100644 index 0000000..7290bc1 --- /dev/null +++ b/news_parser.js @@ -0,0 +1,200 @@ +// ==UserScript== +// @name News parser +// @namespace http://zakonvremeni.ru +// @version 0.1 +// @description Parse news +// @author AlexeiBv+mirocod@narod.ru +// @match https://tass.ru/* +// @match https://ria.ru/* +// @match https://zakonvremeni.ru/* +// @icon https://icons.duckduckgo.com/ip2/zakonvremeni.ru.ico +// @grant none +// ==/UserScript== + +// Общественное достояние, 2023, Алексей Безбородов (Alexei Bezborodov) + +(function() { + 'use strict'; + + function getByClass (className, parent) { + parent || (parent=document); + var descendants=parent.getElementsByTagName('*'), i=-1, e, result=[]; + var re = new RegExp("(?:^|\\s)" + className + "(?!\\S)"); + while (e=descendants[++i]) { + if (re.test(e.className)){ + result.push(e); + } + } + return result; + } + + function GetImageInContainers(baseClass, parent, textAlign) { + var elems = getByClass(baseClass, parent); + if (!elems) { + return ''; + } + + var i; + var img_src = ''; + var re = new RegExp("(https?:\/\/.*\.(?:png|jpg))"); + for (i in elems) { + var e = elems[i]; + var children = e.querySelectorAll("*"); + for(let i = 0; i < children.length; i++){ + var c = children[i]; + if (c.nodeName == 'IMG' && re.test(c.src)) { + img_src = c.src; + } + } + } + if (img_src.length > 0) { + return '

'; + } + return ''; + } + + function Trim(s) { + return ( s || '' ).replace( /^\s+|\s+$/g, '' ); + } + + function RemoveBeforeSplitter(a_String, a_Splitter) { + var index = a_String.indexOf(a_Splitter) + if (index != -1) { + return a_String.substring(index + a_Splitter.length); + } + return a_String; + } + + function RemoveAfterSplitter(a_String, a_Splitter, a_SaveSplitter) { + var index = a_String.indexOf(a_Splitter) + if (index != -1) { + var spl_len = a_Splitter.length + if (!a_SaveSplitter) { + spl_len = 0; + } + return a_String.substring(0, index + spl_len); + } + return a_String; + } + + function GetContentInContainers(a_OutTag, baseClass, parent, textAlign, a_ElementFilterFunc, a_ClearTextFunc) { + var elems = getByClass(baseClass, parent); + if (!elems) { + return 'Не удалось найти ' + baseClass; + } + + var result = ''; + for (var i in elems) { + var e = elems[i]; + if (a_ElementFilterFunc && !a_ElementFilterFunc(e)) { + continue; + } + + var content = ''; + if (e.querySelectorAll) { + var children = e.querySelectorAll("*"); + if (children.length == 0 || e.innerText) { + if (e.innerText) { + content += Trim(e.textContent); + } + } + else { + for (let i = 0; i < children.length; i++) { + var c = children[i]; + if (c.innerText) { + content += Trim(c.textContent); + } + } + } + } + if (a_ClearTextFunc) { + content = a_ClearTextFunc(content); + } + if (a_OutTag == '') { + result += content; + } + else { + result += '<' + a_OutTag + ' style = "text-align:' + textAlign + ';">' + content + ''; + } + } + return result; + } + + function ClearUrl(a_Url) { + var separator = '?'; + return RemoveAfterSplitter(a_Url, separator, false); + } + + function FIlterTrue(element) { + return true; + } + + function FIlterRia(element) { + if (element.dataset.type == 'text' || element.dataset.type == 'quote' || element.dataset.type == 'list') { + return true; + } + return false; + } + + function FIlterZV(element) { + if (element.itemprop == 'articleBody') { + return true; + } + return false; + } + + function ClearTextFuncTemplate(a_RemoveBeforeList) { + function ClearTextFunc(a_Content) { + var content = a_Content; + for (let i = 0; i < a_RemoveBeforeList.length; i++) { + var r = a_RemoveBeforeList[i]; + content = RemoveBeforeSplitter(content, r); + } + return content; + } + return ClearTextFunc + } + + function MakeContent() { + var content = ''; + var title_tag = 'h2'; + var p_tag = 'p'; + var zero_tag = ''; + var source_add = true; + if (location.hostname == 'tass.ru') { + content += GetContentInContainers(title_tag, 'tass_pkg_title--variant_h1_default.*', document.getElementById('content_box'), 'center'); + content += GetImageInContainers('Image_wrapper_.*', document.getElementById('content_box'), 'center'); + content += GetContentInContainers(p_tag, 'Paragraph_paragraph.*', document.getElementById('content_box'), 'justify', FIlterTrue, ClearTextFuncTemplate(['/ТАСС/. '])); + } + else if (location.hostname == 'ria.ru') { + content += GetContentInContainers(title_tag, 'article__title', document.getElementsByClassName('article__header')[0], 'center'); + content += GetImageInContainers('photoview__open', document.getElementsByClassName('article__header')[0], 'center'); + content += GetContentInContainers(p_tag, 'article__block', document.getElementsByClassName('article__body')[0], 'justify', FIlterRia, ClearTextFuncTemplate(['– РИА Новости. ', '— РИА Новости. '])); + } + else if (location.hostname == 'zakonvremeni.ru') { + var title = GetContentInContainers(zero_tag, 'page-header', document.getElementsByClassName('item-page')[0]); + var parent_category = GetContentInContainers(zero_tag, 'parent-category-name', document.getElementsByClassName('item-page')[0]); + var category = GetContentInContainers(zero_tag, 'category-name', document.getElementsByClassName('item-page')[0]); + var page = RemoveAfterSplitter(Trim(document.getElementsByClassName('item-page')[0].querySelector('[itemprop=articleBody]').textContent), '.', true); + content = title + '\n' + parent_category + ' ' + category + '\n\n' + page + '\n' + document.URL; + source_add = false; + } + + var result = ''; + if (content.length > 0) { + result = ''; + } + return result; + } + + var content = MakeContent(); + var logo = document.createElement("div"); + logo.innerHTML = '
' + content + '
'; + + document.body.insertBefore(logo, document.body.firstChild); + +})();