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

#bookmarks(bookmark_type = :all) ⇒ Object

Get the user’s bookmarked data.

Parameters:

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

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

    • :all - returns a hash with all bookmarked data



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/pandata/scraper.rb', line 67

def bookmarks(bookmark_type = :all)
  case bookmark_type
  when :tracks
    scrape_for(:bookmarked_tracks, :get_bookmarked_tracks)
  when :artists
    scrape_for(:bookmarked_artists, :get_bookmarked_artists)
  when :all
    { artists: bookmarks(:artists),
      tracks: bookmarks(:tracks) }
  end
end

#followersArray

Get the user’s public followers.

Returns:

  • (Array)

    identical to #following



115
116
117
# File 'lib/pandata/scraper.rb', line 115

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



109
110
111
# File 'lib/pandata/scraper.rb', line 109

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



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/pandata/scraper.rb', line 86

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

#playing_stationString

Get the user’s playing station.

Returns:

  • (String)


52
53
54
# File 'lib/pandata/scraper.rb', line 52

def playing_station
  scrape_for(:playing_station, :get_playing_station).first
end

#recent_activityArray

Get the user’s recent activity.

Returns:

  • (Array)

    array of activity names



46
47
48
# File 'lib/pandata/scraper.rb', line 46

def recent_activity
  scrape_for(:recent_activity, :get_recent_activity)
end

#stationsArray

Get the user’s stations.

Returns:

  • (Array)

    array of station names



58
59
60
# File 'lib/pandata/scraper.rb', line 58

def stations
  scrape_for(:stations, :get_stations)
end