Class: Pandata::Scraper

Inherits:
Object
  • Object
show all
Defined in:
lib/pandata/scraper.rb

Overview

Downloads a user’s Pandora.com data. A user’s profile must be public for Pandata to download its data.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(webname) ⇒ Scraper

Returns a new instance of Scraper.



39
40
41
42
# File 'lib/pandata/scraper.rb', line 39

def initialize(webname)
  @parser = Parser.new
  @webname = webname
end

Instance Attribute Details

#download_cbObject

A Proc that gets called after some data has been downloaded.



16
17
18
# File 'lib/pandata/scraper.rb', line 16

def download_cb
  @download_cb
end

#webnameObject (readonly)

What Pandora uses to identify a user and it remains constant even if the user ties a new email address to their Pandora account.



13
14
15
# File 'lib/pandata/scraper.rb', line 13

def webname
  @webname
end

Class Method Details

.get(user_id) ⇒ Scraper, Array

If possible, get a Scraper instance for the user_id otherwise return an array of similar webnames.

Parameters:

  • user_id (String)

    email or webname

Returns:

  • (Scraper)

    a scraper object for the supplied user ID

  • (Array)

    array of similar webnames



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/pandata/scraper.rb', line 23

def self.get(user_id)
  search_url = DATA_FEED_URLS[:user_search] % { searchString: user_id }
  html = Downloader.read_page(search_url)
  webnames = Parser.new.get_webnames_from_search(html)

  if webnames.include?(user_id)
    new(user_id)
  # If user_id looks like an email and still gets a result.
  elsif webnames.size == 1 && /.*@.*\..*/ =~ user_id
    new(webnames.first)
  else
    webnames
  end
end

Instance Method Details

#followersArray

Get the user’s public followers.

Returns:

  • (Array)

    identical to #following



80
81
82
# File 'lib/pandata/scraper.rb', line 80

def followers
  scrape_for(:followers, :get_followers)
end

#followingArray

Get the public users being followed by the user.

Returns:

  • (Array)

    array of hashes with keys:

    • :name - profile name

    • :webname - unique Pandora ID

    • :href - URL to online Pandora profile



74
75
76
# File 'lib/pandata/scraper.rb', line 74

def following
  scrape_for(:following, :get_following)
end

#likes(like_type = :all) ⇒ Object

Get the user’s liked data. (The results from giving a ‘thumbs up.’)

Parameters:

  • like_type (Symbol) (defaults to: :all)
    • :artists - returns an array of artist names

    • :albums - returns an array of hashes with :artist and :album keys

    • :stations - returns an array of station names

    • :tracks - returns an array of hashes with :artist and :track keys

    • :all - returns a hash with all liked data



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/pandata/scraper.rb', line 51

def likes(like_type = :all)
  case like_type
  when :tracks
    scrape_for(:liked_tracks, :get_liked_tracks)
  when :artists
    scrape_for(:liked_artists, :get_liked_artists)
  when :stations
    scrape_for(:liked_stations, :get_liked_stations)
  when :albums
    scrape_for(:liked_albums, :get_liked_albums)
  when :all
    { artists: likes(:artists),
      albums: likes(:albums),
      stations: likes(:stations),
      tracks: likes(:tracks) }
  end
end