Skip to content

Instantly share code, notes, and snippets.

@mohan-cao
Last active May 27, 2019 21:58
Show Gist options
  • Select an option

  • Save mohan-cao/b899a5e8a3f7912e03940c64b319941e to your computer and use it in GitHub Desktop.

Select an option

Save mohan-cao/b899a5e8a3f7912e03940c64b319941e to your computer and use it in GitHub Desktop.
Facebook RSS Feed photo post filter for IFTTT (to any service)

How do I connect a FB public page with IFTTT?

Thanks to the lovely folks over at https://github.com/RSS-Bridge/rss-bridge, it can be done as simply as finding a public RSS bridge server (with FB support) and making an RSS feed of your favourite page.

  1. Make your RSS bridge feed from the FB page (Mrss/Atom feeds confirmed working)
  2. Choose the RSS feed option in IFTTT triggers
  3. Choose the type of trigger (new feed item, new feed item with keyword)
  4. Now, just select your action as normal

At this point, you have the choice of doing whatever to your new feed item when you receive it.

Manipulating feed data

This can be done in different ways, some more complex than others.

Filtering code (RECOMMENDED)

Using the filter code in this gist, you can filter and change your output actions as you see fit. It's relatively difficult to test with a slow feed, so I recommend making small functions and testing those individually with raw RSS entries (e.g. copy and paste the contents string into the browser console and test that)

Action-trigger pipelining with Google Sheets and script editor

This way involves a lot more setup and propagation time (but is entirely free for the foreseeable future).

  1. Set up a trigger from RSS to GSheets, outputting just EntryContents
  2. Wait until you get a new row in the new sheet
  3. Create a new script in Tools->Script Editor
  4. Use the template in this gist to make an array function that makes a new column containing your filtered data
  5. Set up a trigger from GSheets (new row added type) to your custom action (Telegram, etc.)
  6. Repeat 2-5 if you want to chain together multiple pipeline steps (i.e. feed -> gsheets processing -> gsheets processing -> action)
/// This code is functionally equivalent to the filter code in IFTTT
/// Gets largest dimension image from FB RSS <content> in the RSS entry
function GetBiggestImage(content) {
if (!content) return "";
if (Array.isArray(content)) {
return content.map(function(e){return e.map(function(f){return GetBiggestImage(f)})})
}
var re = /src\s*=\s*"(.+?)"/g;
var biggestDim = [0, 0];
var newImageURL = "https://placehold.it/200x200/.png";
var m = null;
do {
m = re.exec(content); // match URL in img src tag
if (m) {
var dim = m[1].match(/[a-zA-Z](\d+)x(\d+)/); // match /[\alpha][w]x[h]/ in URL and get largest dimensions
if( dim === null ) continue;
var w = parseInt(dim[1]);
var h = parseInt(dim[2]);
if (w > biggestDim[0] && h > biggestDim[1]) {
newImageURL = m[1];
biggestDim[0] = w;
biggestDim[1] = h;
}
}
} while (m);
return newImageURL;
}
/// Gets single-line caption from FB RSS <content> in the RSS entry
function GetCaption(content) {
if (!content) return "";
if (Array.isArray(content)) {
return content.map(function(e){return e.map(function(f){return GetCaption(f)})});
}
var cap = content.match(/<p>(.+?)<\/p>/g);
if (!cap) {
return "No caption found";
} else {
return cap.join(' ').replace(/<\/?(.+?)>/g,''); // Condense all lines in post to a single line and strip HTML tags
}
}
/// This code is functionally equivalent to the GSheets Script Editor code (except for the send to telegram part at the bottom)
/// Gets largest dimension image from FB RSS <content> in the RSS entry
function getImage(content: string) {
let re = /src\s*=\s*"(.+?)"/g;
let biggestDim = [0, 0];
let newImageURL = "https://placehold.it/200x200/.png";
let m = null;
do {
m = re.exec(content); // match URL in img src tag
if (m) {
let dim = m[1].match(/[a-zA-Z](\d+)x(\d+)/); // match /[\alpha][w]x[h]/ in URL and get largest dimensions
if( dim === null ) continue;
let w = parseInt(dim[1]);
let h = parseInt(dim[2]);
if (w > biggestDim[0] && h > biggestDim[1]) {
newImageURL = m[1];
biggestDim[0] = w;
biggestDim[1] = h;
}
}
} while (m);
return newImageURL;
}
/// Gets single-line caption from FB RSS <content> in the RSS entry
function getCaption(content: string) {
let parsed = content.replace(/&gt;/g, '>').replace(/&lt;/g, '<');
let cap = parsed.match(/<p>(.+?)<\/p>/g);
if (!cap) {
return "No caption found";
} else {
return cap.join(' ').replace(/<\/?(.+?)>/g,''); // Condense all lines in post to a single line and strip HTML tags
}
}
let ampUnescaped: string = Feed.newFeedItem.EntryContent.replace(/&amp;/g, "&"); // Photo URLs sometimes fail with &= in query
let newImageURL: string = getImage(ampUnescaped);
let newCaption: string = getCaption(ampUnescaped);
// Send to service (Telegram, Facebook, whatever)
Telegram.sendPhoto.setPhotoUrl(newImageURL);
Telegram.sendPhoto.setCaption(newCaption + " " + Feed.newFeedItem.EntryUrl);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment