function fetchAndPostFromFeeds_Final() {
const blogId = "1835468259577533883"; // ← Apna Blogger ID yahan daalo
const postedUrls = PropertiesService.getScriptProperties().getProperty("postedUrls") || "[]";
const alreadyPosted = JSON.parse(postedUrls);
const feeds = [
"https://news.google.com/rss?hl=en-US&gl=US&ceid=US:en",
"https://news.google.com/rss/search?q=world+news&hl=en-US&gl=US&ceid=US:en",
"https://news.google.com/rss/search?q=international+breaking+news&hl=en-US&gl=US&ceid=US:en",
"https://news.google.com/rss?hl=en&gl=US&ceid=US:en"
];
const maxPostsPerFeed = 10;
let newPosted = [];
for (let feed of feeds) {
try {
const xml = UrlFetchApp.fetch(feed, { muteHttpExceptions: true }).getContentText();
const doc = XmlService.parse(xml);
const channel = doc.getRootElement().getChild("channel");
const items = channel.getChildren("item");
for (let i = 0; i < Math.min(items.length, maxPostsPerFeed); i++) {
const item = items[i];
const link = item.getChild("link").getText();
if (alreadyPosted.includes(link)) {
Logger.log("⏩ Already posted: " + link);
continue;
}
const pageHtml = UrlFetchApp.fetch(link, { muteHttpExceptions: true }).getContentText();
const title = item.getChildText("title") || "No Title";
// ✅ MULTI-PARAGRAPH EXTRACTOR
const paraMatches = [...pageHtml.matchAll(/<p[^>]*>(.*?)<\/p>/g)];
let contentParagraphs = [];
for (let para of paraMatches) {
const text = para[1].replace(/<[^>]+>/g, '').trim();
if (text.length > 50 && !text.includes("{") && !text.includes("function")) {
contentParagraphs.push(`<p>${text}</p>`);
if (contentParagraphs.length >= 3) break;
}
}
if (contentParagraphs.length < 1) {
Logger.log("⚠️ Skipped (not enough clean content): " + link);
continue;
}
// ✅ IMAGE FINDER — first valid image (jpg/png/webp), or fallback
const imageMatches = [...pageHtml.matchAll(/<img[^>]+src="([^"]+)"[^>]*>/gi)];
let imageSrc = "";
for (let match of imageMatches) {
const src = match[1];
if (/\.(jpg|jpeg|png|webp)/i.test(src)) {
imageSrc = src;
break;
}
}
// ✅ Fallback image agar kuch na mile
if (!imageSrc) {
imageSrc = "https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj8ifDv9cfMt5K6RJamiUnxqzbhRhWxch7V37jRfFS-PYPaXUO8yE4cvlLjCaBl8E-I-4oITYnSJU82Du0nenQa2ACwROlNbe_kNY1qqfBb5y2AbpaPp7Raa6zpeP_ehSxLab2nxy8YNglG1nJMKA9OZ6VSygEiCiMKKPAyElDOn_Bgc9WrCCmjdqv3yc2D/s1600/breaking-news-design-template-4bedd10442d6ab8625e47a8412be777e_screen.jpg"; // ← Yahan apni image lagao
}
// Add image as clickable link (so user still sees something)
const imageTag = imageSrc
? `<a href="${imageSrc}" target="_blank"><img src="${imageSrc}" style="max-width:100%;height:auto;" onerror="this.style.display='none';"></a><br><br>`
: "";
const body = `${imageTag}${contentParagraphs.join("")}`;
const postPayload = JSON.stringify({
title: title,
content: body,
labels: ["News"],
status: "LIVE"
});
const bloggerUrl = `https://www.googleapis.com/blogger/v3/blogs/${blogId}/posts/`;
const options = {
method: 'post',
contentType: 'application/json',
payload: postPayload,
headers: {
Authorization: `Bearer ${ScriptApp.getOAuthToken()}`
},
muteHttpExceptions: true
};
const response = UrlFetchApp.fetch(bloggerUrl, options);
Logger.log("✅ Posted: " + title);
Logger.log("📢 Code: " + response.getResponseCode());
newPosted.push(link);
Utilities.sleep(3000); // ⏱ delay
}
} catch (err) {
Logger.log("❌ Feed error: " + feed + " → " + err);
}
}
const updatedPosted = [...alreadyPosted, ...newPosted].slice(-100);
PropertiesService.getScriptProperties().setProperty("postedUrls", JSON.stringify(updatedPosted));
}
function fetchAndPostFromFeeds_Final() {
const blogId = "1835468259577533883"; // ← Apna Blogger ID yahan daalo
const postedUrls = PropertiesService.getScriptProperties().getProperty("postedUrls") || "[]";
const alreadyPosted = JSON.parse(postedUrls);
const feeds = [
"https://news.google.com/rss?hl=en-US&gl=US&ceid=US:en",
"https://news.google.com/rss/search?q=world+news&hl=en-US&gl=US&ceid=US:en",
"https://news.google.com/rss/search?q=international+breaking+news&hl=en-US&gl=US&ceid=US:en",
"https://news.google.com/rss?hl=en&gl=US&ceid=US:en",
"https://www.reutersagency.com/feed/?best-topics=top-news",
"http://feeds.bbci.co.uk/news/world/rss.xml",
"http://rss.cnn.com/rss/edition.rss",
"https://www.aljazeera.com/xml/rss/all.xml",
"https://www.theguardian.com/world/rss",
"https://feeds.nbcnews.com/nbcnews/public/news",
"https://www.cnbc.com/id/100727362/device/rss/rss.html",
"https://feeds.washingtonpost.com/rss/world",
"https://feeds.feedburner.com/time/world",
"https://www.yahoo.com/news/rss/world/",
"https://feeds.skynews.com/feeds/rss/world.xml",
"https://www.rt.com/rss/news/",
"https://feeds.feedburner.com/daily-express-world-news",
"https://www.scmp.com/rss/91/feed/",
"https://bbcstoriesnews.blogspot.com/feeds/posts/default?alt=rss"
];
const maxPostsPerFeed = 10;
let newPosted = [];
for (let feed of feeds) {
try {
const xml = UrlFetchApp.fetch(feed, { muteHttpExceptions: true }).getContentText();
const doc = XmlService.parse(xml);
const channel = doc.getRootElement().getChild("channel");
const items = channel.getChildren("item");
for (let i = 0; i < Math.min(items.length, maxPostsPerFeed); i++) {
const item = items[i];
const link = item.getChild("link").getText();
if (alreadyPosted.includes(link)) {
Logger.log("⏩ Already posted: " + link);
continue;
}
const pageHtml = UrlFetchApp.fetch(link, { muteHttpExceptions: true }).getContentText();
const title = item.getChildText("title") || "No Title";
// ✅ MULTI-PARAGRAPH EXTRACTOR
const paraMatches = [...pageHtml.matchAll(/<p[^>]*>(.*?)<\/p>/g)];
let contentParagraphs = [];
for (let para of paraMatches) {
const text = para[1].replace(/<[^>]+>/g, '').trim();
if (text.length > 50 && !text.includes("{") && !text.includes("function")) {
contentParagraphs.push(`<p>${text}</p>`);
if (contentParagraphs.length >= 3) break;
}
}
if (contentParagraphs.length < 1) {
Logger.log("⚠️ Skipped (not enough clean content): " + link);
continue;
}
// ✅ IMAGE FINDER — first valid image (jpg/png/webp), or fallback
const imageMatches = [...pageHtml.matchAll(/<img[^>]+src="([^"]+)"[^>]*>/gi)];
let imageSrc = "";
for (let match of imageMatches) {
const src = match[1];
if (/\.(jpg|jpeg|png|webp)/i.test(src)) {
imageSrc = src;
break;
}
}
// ✅ Fallback image agar kuch na mile
if (!imageSrc) {
imageSrc = "https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj8ifDv9cfMt5K6RJamiUnxqzbhRhWxch7V37jRfFS-PYPaXUO8yE4cvlLjCaBl8E-I-4oITYnSJU82Du0nenQa2ACwROlNbe_kNY1qqfBb5y2AbpaPp7Raa6zpeP_ehSxLab2nxy8YNglG1nJMKA9OZ6VSygEiCiMKKPAyElDOn_Bgc9WrCCmjdqv3yc2D/s1600/breaking-news-design-template-4bedd10442d6ab8625e47a8412be777e_screen.jpg"; // ← Yahan apni image lagao
}
const imageTag = `<img src="${imageSrc}" style="max-width:100%;height:auto;"><br><br>`;
const body = `${imageTag}${contentParagraphs.join("")}`;
const postPayload = JSON.stringify({
title: title,
content: body,
labels: ["News"],
status: "LIVE"
});
const bloggerUrl = `https://www.googleapis.com/blogger/v3/blogs/${blogId}/posts/`;
const options = {
method: 'post',
contentType: 'application/json',
payload: postPayload,
headers: {
Authorization: `Bearer ${ScriptApp.getOAuthToken()}`
},
muteHttpExceptions: true
};
const response = UrlFetchApp.fetch(bloggerUrl, options);
Logger.log("✅ Posted: " + title);
Logger.log("📢 Code: " + response.getResponseCode());
newPosted.push(link);
Utilities.sleep(1000); // ⏱ delay
}
} catch (err) {
Logger.log("❌ Feed error: " + feed + " → " + err);
}
}
const updatedPosted = [...alreadyPosted, ...newPosted].slice(-100);
PropertiesService.getScriptProperties().setProperty("postedUrls", JSON.stringify(updatedPosted));
}
No comments:
Post a Comment