How to autopost or autopublish to blogger using Google Apps Script

Like WordPress, you can also auto post your Blogger blogs using Google Apps Scripts. To achieve this you have to enable Post using email of your blogger account, also known as Mail2Blogger. Use this address to post text and images (up to 10MB in size) directly to your blog.
Sign in to your blogger account then navigate to Settings  ›  Email

Notice that the code snippet below uses youremail.secretword@blogger.com to auto publish using Gmail Service, which allows scripts to send email and access user's Gmail account. You can host an app script in any Google account and need not necessarily the same blogger account to which it is connected to.

The content for the blog can be scrapped from a URL.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
function doGet() {
  var url="http://tech.firstpost.com/news-analysis/amazon-now-offers-big-discounts-on-motorola-smartphones-and-wearables-302046.html";
  
  // The XPATH for the data to extract
  var xpath = '//*[@id="single"]/section[1]/div/div[2]';
  
  // Contruct a YQL URL
  var query = "select * from html where url = '" + url + "' and xpath = '" + xpath + "'";
  
  // Notice that we request the data in HTML (XML) format
  var yql   = "https://query.yahooapis.com/v1/public/yql?format=html&q=" + encodeURIComponent(query);
  var response = UrlFetchApp.fetch(yql);
  var document = XmlService.parse(response);
  var root = document.getRootElement();
  var result = XmlService.getPrettyFormat().format(root.getChild('results').getChild('div')).toString();
  
  //Extract image source and attach to mail
  var attach=UrlFetchApp.fetch(result.match(/src="([^"]+)"/gim)[0].replace('src="','').replace('\"','')).getBlob();
  
  //send email
  GmailApp.sendEmail("youremail.secretword@blogger.com", "Digital Thoughts",  result, {htmlBody: result, attachments: [attach]});
}

Note:  Do not ignore copyright and legal issues, while scrapping digital contents before you publish.

Comments

  1. Great content, am used this code for posting on my website www.teaserspot.com

    ReplyDelete

Post a Comment

Do not spam.