Skip to content

Instantly share code, notes, and snippets.

@camillebaldock
Last active January 2, 2019 20:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save camillebaldock/40db50ad9bef72c7c304 to your computer and use it in GitHub Desktop.
Save camillebaldock/40db50ad9bef72c7c304 to your computer and use it in GitHub Desktop.
Dashing PocketCasts widget

Description

A Dashing widget for displaying the number of unplayed PocketCasts podcasts in an account.

See a live demo here.

Usage

Due to the fact that PocketCasts does not, at current time of writing, have a public API, I wrote some code to scrape the data off their webpage. Due to extensive use of JavaScript on the PocketCasts webplayer, I chose an approach using Watir that uses a browser's webdriver. This proved tricky to test on Heroku at the time so I decided to run the script on a personal machine at home and push the data to my Dashing application.

Setup the following environment variables:

  • POCKET_CASTS_EMAIL
  • POCKET_CASTS_PASSWORD
  • AUTH_TOKEN: the auth token of your Dashing application

Dependencies

mechanize watir-webdriver

Add it to your Dashing application's Gemfile:

gem 'mechanize'
gem 'watir-webdriver'

and run bundle install.

require 'watir-webdriver'
require 'json'
require 'uri'
require 'net/http'
class PocketCastsClient
def initialize(email_address, password)
@email_address = email_address
@password = password
@browser = Watir::Browser.new :firefox
@unplayed_podcasts = {}
end
def podcasts
@browser.goto "https://play.pocketcasts.com/users/sign_in"
@browser.text_field(:name => 'user[email]').set(@email_address)
@browser.text_field(:name => 'user[password]').set(@password)
@browser.form(:id, 'new_user').submit
#load home page
sleep(10)
podcast_elements = @browser.elements(:class => 'podcast_content').select{ |element| element.visible? }
podcast_elements.each do |podcast_element|
process_podcast(podcast_element)
end
@browser.close
@unplayed_podcasts
end
private
def process_podcast(podcast_element)
podcast_element.click
#load podcast page
sleep(10)
name = @browser.elements(:id => 'podcast_header_text').first.h1.text
show_more = get_visible_show_more_elements
if show_more.size > 0
#load more entries only once
sleep(10)
if show_more.first.visible?
show_more.first.click
end
end
unplayed = @browser.elements(:class => 'played_status_0').size + @browser.elements(:class => 'played_status_1').size
p "#{name}: #{unplayed} unplayed"
@unplayed_podcasts[name] = unplayed
end
def get_visible_show_more_elements
@browser.elements(:class => 'show_more').select{ |element| element.visible? }
end
end
client = PocketCastsClient.new(ENV["POCKET_CASTS_EMAIL"], ENV["POCKET_CASTS_PASSWORD"])
auth_token=ENV["AUTH_TOKEN"]
json_headers = {"Content-Type" => "application/json",
"Accept" => "application/json"}
unplayed_podcasts = client.podcasts.values.inject(:+)
p "#{unplayed_podcasts} podcasts"
params = {'auth_token' => auth_token, 'current' => unplayed_podcasts}
uri = URI.parse('http://your-dashboard-url.com/widgets/podcasts')
http = Net::HTTP.new(uri.host, uri.port)
response = http.post(uri.path, params.to_json, json_headers)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment