/**
* ==========================================================
* MOONINGNEWS — RSS to Blogger Auto-Publisher
* ទាញអត្ថបទសង្ខេបពី RSS Feed ហើយផុសទៅ Blogger ស្វ័យប្រវត្តិ
* ==========================================================
*
* របៀបដំឡើង៖
* 1) ចូល https://script.google.com → New Project → ចម្លងកូដនេះទាំងអស់ដាក់ជំនួស Code.gs
* 2) បើក Advanced Google Service៖ ខាងឆ្វេង ចុច Services (រូប +) → រក "Blogger API" → Add
* 3) កែតម្រូវផ្នែក CONFIG ខាងក្រោម (BLOG_ID និង RSS_FEEDS) ឲ្យត្រូវនឹងអ្នក
* 4) រត់ function setup() ម្តង ដើម្បីផ្តល់សិទ្ធិ (Authorize) — Google នឹងសួរអនុញ្ញាត
* 5) រត់ function createTrigger() ម្តង ដើម្បីឲ្យវាដំណើរការស្វ័យប្រវត្តិជារៀងរាល់ម៉ោង
*
* ការរក BLOG_ID៖ Blogger Dashboard → Settings → Basic → មើលក្នុង URL របស់ទំព័រ Settings
* (ជាលេខវែងៗ ដូចជា 1234567890123456789)
*/
/* ================= CONFIG ================= */
const BLOG_ID = '7727681337826807196';
const RSS_FEEDS = [
'https://example-news-site.com/rss',
// 'https://another-site.com/feed',
];
const MAX_POSTS_PER_RUN = 5; // ចំនួនអត្ថបទអតិបរមាដែលផុសក្នុងមួយដងរត់
const DEFAULT_LABEL = 'ព័ត៌មាន'; // Label ដែលដាក់ស្វ័យប្រវត្តិលើគ្រប់ post
const PUBLISH_IMMEDIATELY = false; // false = រក្សាទុកជា Draft (ត្រូវត្រួតពិនិត្យមុនផុស) | true = ផុសភ្លាម
const EXCERPT_MAX_CHARS = 400; // កំណត់ប្រវែងសេចក្តីសង្ខេបអតិបរមា
/* ================= MAIN ================= */
function main() {
const postedLinks = getPostedLinks_();
let count = 0;
RSS_FEEDS.forEach(function (feedUrl) {
if (count >= MAX_POSTS_PER_RUN) return;
let items;
try {
items = fetchFeedItems_(feedUrl);
} catch (err) {
Logger.log('កំហុសពេលទាញ feed: ' + feedUrl + ' — ' + err);
return;
}
items.forEach(function (item) {
if (count >= MAX_POSTS_PER_RUN) return;
if (postedLinks.indexOf(item.link) !== -1) return; // ធ្លាប់ផុសហើយ
try {
publishToBlogger_(item);
markAsPosted_(item.link);
count++;
Logger.log('ផុសជោគជ័យ: ' + item.title);
} catch (err) {
Logger.log('កំហុសពេលផុស "' + item.title + '": ' + err);
}
});
});
Logger.log('បញ្ចប់ — ផុសចំនួន ' + count + ' អត្ថបទ');
}
/* ================= FETCH & PARSE RSS ================= */
function fetchFeedItems_(feedUrl) {
const response = UrlFetchApp.fetch(feedUrl, { muteHttpExceptions: true });
const xml = XmlService.parse(response.getContentText());
const root = xml.getRootElement();
// គាំទ្រទាំង RSS 2.0 (channel > item) និង Atom (entry)
const channel = root.getChild('channel');
const rawItems = channel ? channel.getChildren('item') : root.getChildren('entry');
return rawItems.map(function (item) {
const title = stripHtml_(getChildText_(item, 'title'));
const link = getLink_(item);
const rawDescription = getChildText_(item, 'description') || getChildText_(item, 'summary');
const description = truncate_(stripHtml_(rawDescription), EXCERPT_MAX_CHARS);
const image = extractImage_(item, rawDescription);
return {
title: title,
link: link,
description: description,
image: image,
sourceDomain: extractDomain_(feedUrl)
};
}).filter(function (i) { return i.title && i.link; });
}
function getChildText_(element, name) {
const child = element.getChild(name);
return child ? child.getText() : '';
}
function getLink_(item) {
// RSS: <link>https://...</link> ជាអត្ថបទធម្មតា
const linkChild = item.getChild('link');
if (linkChild) {
const text = linkChild.getText();
if (text) return text.trim();
const hrefAttr = linkChild.getAttribute('href'); // Atom: <link href="..."/>
if (hrefAttr) return hrefAttr.getValue();
}
const guid = item.getChild('guid');
return guid ? guid.getText() : '';
}
function extractImage_(item, descriptionHtml) {
// ១) សាកល្បង <enclosure url="..." type="image/...">
const enclosure = item.getChild('enclosure');
if (enclosure) {
const type = enclosure.getAttribute('type');
const url = enclosure.getAttribute('url');
if (url && (!type || type.getValue().indexOf('image') !== -1)) {
return url.getValue();
}
}
// ២) សាកល្បង media:thumbnail / media:content (namespace mrss)
try {
const mediaNs = XmlService.getNamespace('media', 'http://search.yahoo.com/mrss/');
const thumb = item.getChild('thumbnail', mediaNs) || item.getChild('content', mediaNs);
if (thumb) {
const url = thumb.getAttribute('url');
if (url) return url.getValue();
}
} catch (e) { /* namespace មិនមាន — រំលង */ }
// ៣) ចុងក្រោយ សាកល្បងទាញរូបទីមួយចេញពី HTML នៃ description
if (descriptionHtml) {
const match = descriptionHtml.match(/<img[^>]+src=["']([^"']+)["']/i);
if (match) return match[1];
}
return '';
}
function stripHtml_(html) {
if (!html) return '';
return html.replace(/<[^>]*>/g, '').replace(/ /g, ' ').trim();
}
function truncate_(text, maxChars) {
if (!text || text.length <= maxChars) return text;
return text.substring(0, maxChars).trim() + '…';
}
function extractDomain_(url) {
const match = url.match(/^https?:\/\/([^\/]+)/i);
return match ? match[1] : url;
}
/* ================= PUBLISH TO BLOGGER ================= */
function publishToBlogger_(item) {
const post = {
kind: 'blogger#post',
blog: { id: BLOG_ID },
title: item.title,
content: buildPostContent_(item),
labels: [DEFAULT_LABEL]
};
Blogger.Posts.insert(post, BLOG_ID, { isDraft: !PUBLISH_IMMEDIATELY });
}
function buildPostContent_(item) {
let html = '';
if (item.image) {
html += '<img src="' + item.image + '" alt="" style="max-width:100%;border-radius:6px;margin-bottom:14px;" />';
}
html += '<p>' + item.description + '</p>';
html += '<p><a href="' + item.link + '" target="_blank" rel="noopener">អានអត្ថបទពេញលេញនៅប្រភព »</a></p>';
html += '<p style="font-size:12px;color:#888;">ប្រភព៖ ' + item.sourceDomain + '</p>';
return html;
}
/* ================= DEDUPE STORAGE ================= */
// រក្សាទុកបញ្ជីតំណដែលធ្លាប់ផុសរួច ក្នុង PropertiesService (រក្សាតែ ៣០០ ចុងក្រោយ ដើម្បីជៀសវាងលើសទំហំ)
function getPostedLinks_() {
const stored = PropertiesService.getScriptProperties().getProperty('POSTED_LINKS');
return stored ? JSON.parse(stored) : [];
}
function markAsPosted_(link) {
const links = getPostedLinks_();
links.push(link);
const trimmed = links.slice(-300); // រក្សាតែ ៣០០ ចុងក្រោយ
PropertiesService.getScriptProperties().setProperty('POSTED_LINKS', JSON.stringify(trimmed));
}
/* ================= SETUP & TRIGGER ================= */
function setup() {
// រត់លើកដំបូងដើម្បីផ្តល់សិទ្ធិ Google ស្នើសុំ (UrlFetch, Blogger, Properties)
Logger.log('Setup ជោគជ័យ — ឥឡូវអាចរត់ createTrigger() បាន');
}
function createTrigger() {
removeTriggers_();
ScriptApp.newTrigger('main')
.timeBased()
.everyHours(1)
.create();
Logger.log('បានបង្កើត Trigger — main() នឹងរត់រៀងរាល់ ១ម៉ោង');
}
function removeTriggers_() {
ScriptApp.getProjectTriggers().forEach(function (t) {
ScriptApp.deleteTrigger(t);
});
}
No comments:
Post a Comment