Class: Pandata::Parser

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

Overview

Parses HTML/XML pages from Pandora for relevant data.

Instance Method Summary collapse

Instance Method Details

#get_followers(html) ⇒ Object

Returns an array of hashes with :name, :webname and :href keys.

Parameters:

  • html (String)


88
89
90
# File 'lib/pandata/parser.rb', line 88

def get_followers(html)
  get_followx_users(html)
end

#get_following(html) ⇒ Object

Returns an array of hashes with :name, :webname and :href keys.

Parameters:

  • html (String)


82
83
84
# File 'lib/pandata/parser.rb', line 82

def get_following(html)
  get_followx_users(html)
end

#get_liked_albums(html) ⇒ Object

Returns an array of hashes with :artist and :album keys.

Parameters:

  • html (String)


70
71
72
73
74
75
76
77
78
# File 'lib/pandata/parser.rb', line 70

def get_liked_albums(html)
  albums = []

  infobox_each_link(html) do |title, subtitle|
    albums << { album: title, artist: subtitle }
  end

  albums
end

#get_liked_artists(html) ⇒ Object

Returns an array of artist names.

Parameters:

  • html (String)


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

def get_liked_artists(html)
  get_infobox_titles(html)
end

#get_liked_stations(html) ⇒ Object

Returns an array of station names.

Parameters:

  • html (String)


64
65
66
# File 'lib/pandata/parser.rb', line 64

def get_liked_stations(html)
  get_infobox_titles(html)
end

#get_liked_tracks(html) ⇒ Object

Returns an array of hashes with :artist and :track keys.

Parameters:

  • html (String)


45
46
47
48
49
50
51
52
53
54
# File 'lib/pandata/parser.rb', line 45

def get_liked_tracks(html)
  tracks = []

  doublelink_each_link(html) do |title, subtitle|
    artist = subtitle.sub(/^by\s/i, '')
    tracks << { track: title, artist: artist }
  end

  tracks
end

#get_next_data_indices(html) ⇒ Hash, False

Get the query parameters necessary to get the next page of data from Pandora.

Parameters:

  • html (String)

Returns:

  • (Hash, False)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/pandata/parser.rb', line 25

def get_next_data_indices(html)
  # .js-more-link is found on mobile pages.
  show_more = Nokogiri::HTML(html).css('.show_more, .js-more-link')[0]

  if show_more
    next_indices = {}
    data_attributes = ['nextStartIndex', 'nextLikeStartIndex', 'nextThumbStartIndex']
    data_attributes.each do |attr_name|
      attr = show_more.attributes['data-' + attr_name.downcase]
      next_indices[attr_name.to_sym] = attr.value.to_i if attr
    end

    next_indices
  else
    false
  end
end

#get_webnames_from_search(html) ⇒ Array

Get the webnames from a user ID search.

Parameters:

  • html (String)

Returns:

  • (Array)

    array of webnames



11
12
13
14
15
16
17
18
19
20
# File 'lib/pandata/parser.rb', line 11

def get_webnames_from_search(html)
  user_links = Nokogiri::HTML(html).css('.user_name a')
  webnames = []

  user_links.each do |link|
    webnames << link['webname']
  end

  webnames
end