* Initial automatic cleanup with rubocop * Fix syntax error introduced by rubocop * Cleanup seeds file * Cleanup lib folder * Exclude bin folder because it contains auto generated files * Make Rubocop a little bit more chatty * Block length should not include comments in the count * Cleanup config folder * Cleanup specs * Updated Rubocop version and generated a todo file * Fix broken ArticlesApi spec * Fix tests * Restored rubocop pre-commit hook
91 lines
2.7 KiB
Ruby
91 lines
2.7 KiB
Ruby
require "rss"
|
|
require "rss/itunes"
|
|
require "open-uri"
|
|
|
|
class PodcastFeed
|
|
def get_all_episodes
|
|
Podcast.find_each do |podcast|
|
|
get_episodes(podcast)
|
|
end
|
|
end
|
|
|
|
def get_episodes(podcast, num = 1000)
|
|
rss = open(podcast.feed_url).read
|
|
feed = RSS::Parser.parse(rss, false)
|
|
feed.items.first(num).each do |item|
|
|
if !existing_episode(item, podcast)
|
|
create_new_episode(item, podcast)
|
|
elsif ep = existing_episode(item, podcast).first
|
|
update_existing_episode(ep, item, podcast)
|
|
end
|
|
end
|
|
feed.items.size
|
|
rescue StandardError => e
|
|
puts e.message
|
|
end
|
|
|
|
def create_new_episode(item, podcast)
|
|
ep = PodcastEpisode.new
|
|
ep.title = item.title
|
|
ep.podcast_id = podcast.id
|
|
ep.slug = item.title.downcase.gsub(/[^0-9a-z ]/i, "").gsub(" ", "-")
|
|
ep.subtitle = item.itunes_subtitle
|
|
ep.summary = item.itunes_summary
|
|
ep.website_url = item.link
|
|
ep.guid = item.guid
|
|
get_media_url(ep, item, podcast)
|
|
begin
|
|
ep.published_at = item.pubDate.to_date
|
|
rescue StandardError
|
|
puts "not valid date"
|
|
end
|
|
ep.body = item.content_encoded || item.itunes_summary || item.description
|
|
ep.save!
|
|
end
|
|
|
|
def update_existing_episode(ep, item, _podcast)
|
|
if ep.published_at == nil
|
|
begin
|
|
ep.published_at = item.pubDate.to_date
|
|
ep.save
|
|
rescue StandardError
|
|
puts "not valid date"
|
|
end
|
|
end
|
|
update_media_url(ep, item)
|
|
end
|
|
|
|
def existing_episode(item, podcast)
|
|
# presence returns nil if the query is an empty array, otherwise returns the array
|
|
PodcastEpisode.where(media_url: item.enclosure.url).presence ||
|
|
PodcastEpisode.where(title: item.title).presence ||
|
|
PodcastEpisode.where(guid: item.guid.to_s).presence ||
|
|
(podcast.unique_website_url? && PodcastEpisode.where(website_url: item.link).presence)
|
|
end
|
|
|
|
def get_media_url(ep, item, podcast)
|
|
ep.media_url = if Rails.env.test? ||
|
|
open(item.enclosure.url.gsub(/http:/, "https:")).status[0] == "200"
|
|
item.enclosure.url.gsub(/http:/, "https:")
|
|
else
|
|
item.enclosure.url
|
|
end
|
|
rescue StandardError
|
|
# podcast episode must have a media_url
|
|
ep.media_url = item.enclosure.url
|
|
if podcast.status_notice.empty?
|
|
podcast.update(status_notice: "This podcast may not be playable in the browser")
|
|
end
|
|
end
|
|
|
|
def update_media_url(ep, item)
|
|
if ep.media_url.include?("https")
|
|
nil
|
|
elsif !ep.media_url.include?("https") &&
|
|
item.enclosure.url.include?("https")
|
|
ep.update!(media_url: item.enclosure.url)
|
|
end
|
|
rescue StandardError
|
|
logger.info "something went wrong with #{podcast.title}, #{ep.title} -- #{ep.media_url}"
|
|
end
|
|
end
|