-
-
Save JuniorJPDJ/aa26d4c61bc1e78af039e9a17bc17907 to your computer and use it in GitHub Desktop.
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) | |
} |
Is it possible to get the video thumbnail added to this query?
https://developers.google.com/youtube/v3/docs/search#snippet.thumbnails
https://www.rssboard.org/media-rss#media-thumbnails
@KiloJKilo It looks like it's possible.
You could do it yourself, shouldn't be hard.
Thanks, I have not been able to figure it out. Closest I can get is printing the image url into the feed.
Thanks anyway.
Is there a quick way to convert this to a GCP cloud function so it can be accessed publicly without Google authentication?
@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
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.
I get ReferenceError: YouTube is not defined (line 2, file "Code")
@wisdomtooth have you enabled YouTube Data API like I mentioned in first comment?
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 ?
I'm sorry but I have no idea about any @adcousin ;/
Anyway - It's cool that it was helpful ;D
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.
To deploy
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