Last active
January 22, 2023 22:37
-
-
Save dubsnipe/01f74ff272a96db3e4fc2b0cf750d1cb to your computer and use it in GitHub Desktop.
R script to download mp3 files from my fav podcasts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require(dplyr) | |
require(stringr) | |
require(xml2) | |
require(lubridate) | |
rss_feed = "" | |
## Script created with help from ChatGPT | |
find_all_mp3 <- function(rss_feed){ | |
# read in the RSS feed as an XML document | |
rss_xml <- read_xml(rss_feed) | |
# extract all the item elements | |
rss_items <<- xml_find_all(rss_xml, "//item") | |
# extract mp3 links, titles and dates from the "enclosure" elements in the "item" nodes | |
title <- xml_find_all(rss_items, "//item/title") %>% xml_text() | |
date <- xml_find_all(rss_items, "//item/pubDate") %>% xml_text() | |
description <- xml_find_all(rss_items, "//item/description") %>% xml_text() | |
link <- xml_find_all(rss_items[i], "//item/enclosure") %>% | |
xml_attr("url") | |
results <- as_tibble(cbind(title, link, date, description)) | |
colnames(results) <- c("title", "link", "date", "description") | |
return(results) | |
} | |
urls <- find_all_mp3(rss_feed) | |
# Cleaning up | |
str_replace_all(urls$title, "\\:", " - ") | |
urls$date <- strptime(urls$date, "%a, %d %b %Y %H:%M:%S %z") | |
urls <- urls %>% mutate(place = nrow(urls)-row(urls)+1) | |
urls$description <- gsub("<.*?>", "", urls$description) | |
# Download all mp3s | |
for(i in 1:nrow(urls)){ | |
tryCatch({ | |
download.file(URLencode(urls[i,]$link), | |
destfile = paste0( sprintf("%03d", nrow(urls)-i+1), " - ", str_trunc(urls[i,]$title, 50 ), ".mp3" ), | |
method="curl") | |
}) | |
} | |
final_text <- vector() | |
for (i in 1:nrow(urls)){ | |
final_text <- c(final_text, paste0(urls$place[i], "\nTitle: ", urls$title[i], " \nLink: ", urls$link[i], "\nDate: ", urls$date[i], "\n\n")) | |
} | |
cat( | |
paste(final_text), | |
file="podcast_download_report.txt", | |
sep="" | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment