Skip to content

Instantly share code, notes, and snippets.

@JuniorJPDJ
Last active July 15, 2023 20:38
Show Gist options
  • Save JuniorJPDJ/aa26d4c61bc1e78af039e9a17bc17907 to your computer and use it in GitHub Desktop.
Save JuniorJPDJ/aa26d4c61bc1e78af039e9a17bc17907 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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
Copy link

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

@JuniorJPDJ
Copy link
Author

JuniorJPDJ commented Sep 13, 2020

@KiloJKilo
Copy link

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
Copy link

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
Copy link
Author

@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
Copy link

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
Copy link

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

@JuniorJPDJ
Copy link
Author

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

@adcousin
Copy link

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
Copy link
Author

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

@MementoMorti
Copy link

MementoMorti commented Sep 24, 2022

Awesome! Thank you so much. What an useful script!
Can you tell me how to incorporate a YouTube operator into the query? I would like to retrieve only videos longer than 20 minutes. I can´t get it running by adding ", long" to the query though.

EDIT:
I also tried adding the parameter videoDuration: 'long' to the results string (based on https://developers.google.com/youtube/v3/docs/search/list). However, it seems to have zero effect.

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