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_bookmarked_artists(xml) ⇒ Object

Returns an array of artist names.

Parameters:

  • xml (String)


95
96
97
98
99
100
101
102
103
# File 'lib/pandata/parser.rb', line 95

def get_bookmarked_artists(xml)
  artists = []

  xml_each_item(xml) do |title|
    artists << title
  end

  artists
end

#get_bookmarked_tracks(xml) ⇒ Object

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

Parameters:

  • xml (String)


82
83
84
85
86
87
88
89
90
91
# File 'lib/pandata/parser.rb', line 82

def get_bookmarked_tracks(xml)
  tracks = []

  xml_each_item(xml) do |title|
    track, artist = title.split(' by ')
    tracks << { artist: artist, track: track }
  end

  tracks
end

#get_followers(html) ⇒ Object

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

Parameters:

  • html (String)


150
151
152
# File 'lib/pandata/parser.rb', line 150

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)


144
145
146
# File 'lib/pandata/parser.rb', line 144

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)


132
133
134
135
136
137
138
139
140
# File 'lib/pandata/parser.rb', line 132

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)


120
121
122
# File 'lib/pandata/parser.rb', line 120

def get_liked_artists(html)
  get_infobox_titles(html)
end

#get_liked_stations(html) ⇒ Object

Returns an array of station names.

Parameters:

  • html (String)


126
127
128
# File 'lib/pandata/parser.rb', line 126

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)


107
108
109
110
111
112
113
114
115
116
# File 'lib/pandata/parser.rb', line 107

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_playing_station(xml) ⇒ String

Parameters:

  • xml (String)

Returns:

  • (String)


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

def get_playing_station(xml)
  station = ''

  xml_each_item(xml) do |title|
    station = title  # First title is the station name.
    break
  end

  station
end

#get_recent_activity(xml) ⇒ Object

Returns an array of recent activity names.

Parameters:

  • xml (String)


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

def get_recent_activity(xml)
  activity_names = []

  xml_each_item(xml) do |title|
    activity_names << title
  end

  activity_names
end

#get_stations(xml) ⇒ Object

Returns an array of station names.

Parameters:

  • xml (String)


57
58
59
60
61
62
63
64
65
# File 'lib/pandata/parser.rb', line 57

def get_stations(xml)
  stations = []

  xml_each_item(xml) do |title|
    stations << title
  end

  stations
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