Module: Meuh

Defined in:
lib/meuh.rb,
lib/meuh/formatting.rb

Overview

This module provide some methods to scrape current tracks from radiomeuh.com

Defined Under Namespace

Modules: Formatting

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.format_results(tracks, color = true) ⇒ Object

Format tracks for output



93
94
95
# File 'lib/meuh.rb', line 93

def format_results(tracks, color=true)
  self::Formatting.text(tracks, color)
end

.text(el) ⇒ Object

Get the text of an element. This is an helper for internal usage.



26
27
28
29
30
# File 'lib/meuh.rb', line 26

def text(el)
  el.text.strip.gsub(/\r/, "\n")
rescue
  ''
end

.tracksObject

Query the website, parse the list of tracks and return a hash: { :previous => [ <track> ],

:current => <track>,
:next => [ <track>, ... ]

}

A track is also a hash: { :artist => “…”,

:album => "...",
:title => "...",
:remaining => "...", <-- remaining time
:time => "..." <-- time for previous tracks

}



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/meuh.rb', line 45

def tracks
  tracks = { :previous => [], :current => nil, :next => [] }

  html = open(url, 'User-Agent' => user_agent).read.encode!(Encoding::UTF_8)
  doc = Nokogiri::HTML(html)

  def parse_track t
    lines = t.inner_html.split(/<br\/?\s*>/).map do |l|
      l.gsub(/&amp;/, '&')
    end

    artist, title = lines[1].split(/\s+-\s+/, 2)

    unless title
      title = artist
      artist = nil
    end

    time = lines[0].strip

    {
      :artist => artist,
      :album => lines[2].strip,
      :title => title,
      :remaining => t.css('font').text.gsub(/^:\s+/, ''),
      :time => time == '...' ? '' : time
    }
  end

  got_current = false
  doc.css('tr > td + td').each do |td|
    if !got_current
      b = td.css('b').first
      if b
        got_current = true
        tracks[:current] = parse_track(b)
      else
        tracks[:next] << parse_track(td)
      end
    else
      tracks[:previous] << parse_track(td) unless td.text =~ /\(jingle\)/i
    end
  end

  tracks
end

.urlObject



17
18
19
# File 'lib/meuh.rb', line 17

def url
  'http://www.radiomeuh.com/meuh/playlist/index.php'
end

.user_agentObject



21
22
23
# File 'lib/meuh.rb', line 21

def user_agent
  "Meuh/#{version} +github.com/bfontaine/meuh"
end

.versionObject



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

def version
  '0.1.2'
end

Instance Method Details

#parse_track(t) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/meuh.rb', line 51

def parse_track t
  lines = t.inner_html.split(/<br\/?\s*>/).map do |l|
    l.gsub(/&amp;/, '&')
  end

  artist, title = lines[1].split(/\s+-\s+/, 2)

  unless title
    title = artist
    artist = nil
  end

  time = lines[0].strip

  {
    :artist => artist,
    :album => lines[2].strip,
    :title => title,
    :remaining => t.css('font').text.gsub(/^:\s+/, ''),
    :time => time == '...' ? '' : time
  }
end