Module: GMusic

Defined in:
lib/gmusic.rb,
lib/gmusic/parser.rb

Defined Under Namespace

Modules: DownloadInfoParser, LyricsParser, SongListParser

Constant Summary collapse

VALID_CONDITIONS =
[ :title, :artist ]
SEARCH_URL_TEMPLATE =
%Q(http://www.google.cn/music/search?q=%s&aq=f)
DOWNLOAD_INFO_URL_TEMPLATE =
%Q(http://www.google.cn/music/top100/musicdownload?id=%s)
LYRICS_URL_TEMPLATE =
%Q(http://www.google.cn/music/top100/lyrics?id=%s)

Class Method Summary collapse

Class Method Details

.search(conditions) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/gmusic.rb', line 18

def search(conditions)
  validate_conditions(conditions)
  query = construct_query(conditions)
  songs = SongListParser.parse(SEARCH_URL_TEMPLATE % query)
  
  matched = []
  songs.each do |song|
    if matched?(song, conditions)
      song.merge!(DownloadInfoParser.parse(DOWNLOAD_INFO_URL_TEMPLATE % song[:id]))
      song.merge!(LyricsParser.parse(LYRICS_URL_TEMPLATE % song[:id])) 
      matched << song
    end
  end
  matched
end