Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Youtube Search RSS generator (deployable on Google Apps Script)
function getSearchRSS(query){
let results = YouTube.Search.list('id, snippet', {
q: query,
maxResults: 50,
order: 'date'
});
let encoded_query = encodeURIComponent(query)
let rss_url = ScriptApp.getService().getUrl() + "?" + encoded_query;
let channel = XmlService.createElement("channel")
.addContent(XmlService.createElement("title").addContent(XmlService.createText("Youtube Search RSS Feed: " + query)))
.addContent(XmlService.createElement("link").addContent(XmlService.createText("https://www.youtube.com/results?search_query=" + encoded_query)))
.addContent(XmlService.createElement("description").addContent(XmlService.createText("Youtube Search RSS feed for search query '" + query + "' updated on " + (new Date()))))
.addContent(XmlService.createElement("link", XmlService.getNamespace("atom", "http://www.w3.org/2005/Atom")).setAttribute("rel", "self").setAttribute("href", rss_url));
for(let i = 0; i < results.items.length; ++i){
let video = results.items[i];
let yt_url = "https://www.youtube.com/watch?v=" + video.id.videoId;
channel.addContent(XmlService.createElement("item")
.addContent(
XmlService.createElement("title").addContent(XmlService.createText(video.snippet.title))
).addContent(
XmlService.createElement("link").addContent(XmlService.createText(yt_url))
).addContent(
XmlService.createElement("description").addContent(XmlService.createText(video.snippet.description))
).addContent(
XmlService.createElement("pubDate").addContent(XmlService.createText(Utilities.formatDate(new Date(video.snippet.publishedAt), "EDT", "EEE, dd MMM yyyy HH:mm:ss Z")))
).addContent(
XmlService.createElement("guid").addContent(XmlService.createText(yt_url))
))
}
return XmlService.getPrettyFormat().format(XmlService.createDocument(
XmlService.createElement("rss").setAttribute("version", "2.0").addContent(channel)
));
}
function doGet(req) {
let query = decodeURIComponent(req.queryString);
return ContentService.createTextOutput(getSearchRSS(query))
.setMimeType(ContentService.MimeType.RSS)
}
@JuniorJPDJ

This comment has been minimized.

Copy link
Owner Author

@JuniorJPDJ JuniorJPDJ commented May 8, 2020

To deploy

  1. Create script on https://script.google.com/
  2. Paste code to new script
  3. Resources -> Advanced Google Services -> enable YouTube Data API
  4. Publish -> As an web app
  5. Grab generated URL, add search query on the end of URL, prepending it with "?" mark.
    DONE! :D

PS. Every change in code needs you to add new project version in deploy window.

Example:
URL from deploy tab: https://script.google.com/macros/s/BLABLABLA/exec
Search query I want this to generate: #hot16challenge2
URL-encoded version of this query: %23hot16challenge2
Final URL responsing with RSS based on this query: https://script.google.com/macros/s/BLABLABLA/exec?%23hot16challenge2

@KiloJKilo

This comment has been minimized.

Copy link

@KiloJKilo KiloJKilo commented Sep 13, 2020

Is it possible to get the video thumbnail added to this query?

@JuniorJPDJ

This comment has been minimized.

Copy link
Owner Author

@JuniorJPDJ JuniorJPDJ commented Sep 13, 2020

@KiloJKilo

This comment has been minimized.

Copy link

@KiloJKilo KiloJKilo commented Sep 13, 2020

Thanks, I have not been able to figure it out. Closest I can get is printing the image url into the feed.
Thanks anyway.

@svpn

This comment has been minimized.

Copy link

@svpn svpn commented Oct 19, 2020

Is there a quick way to convert this to a GCP cloud function so it can be accessed publicly without Google authentication?

@JuniorJPDJ

This comment has been minimized.

Copy link
Owner Author

@JuniorJPDJ JuniorJPDJ commented Oct 19, 2020

@svpn I never used GCP but this didn't need any auth from me, just that to use my youtube account, bcs this api needs login.
Then it worked without any auth on every RSS reader/Telegram/Matrix bots

@svpn

This comment has been minimized.

Copy link

@svpn svpn commented Oct 19, 2020

Ok, I figured it out.

At step 4 in your instructions:

Execute the app as must be set to Me

and

Who has access to the app must be set to Anyone, even anonymous.

@wisdomtooth

This comment has been minimized.

Copy link

@wisdomtooth wisdomtooth commented Jan 27, 2021

I get ReferenceError: YouTube is not defined (line 2, file "Code")

@JuniorJPDJ

This comment has been minimized.

Copy link
Owner Author

@JuniorJPDJ JuniorJPDJ commented Jan 28, 2021

@wisdomtooth have you enabled YouTube Data API like I mentioned in first comment?

@adcousin

This comment has been minimized.

Copy link

@adcousin adcousin commented May 26, 2021

Used it in my Fraidycat. Had to execute once in Google Apps Scripts to enable Youtube authorization. Thanks a lot. Any idea of similar tools for LinkedIn thread ?

@JuniorJPDJ

This comment has been minimized.

Copy link
Owner Author

@JuniorJPDJ JuniorJPDJ commented May 27, 2021

I'm sorry but I have no idea about any @adcousin ;/
Anyway - It's cool that it was helpful ;D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment