Class: Myshazam::Track

Inherits:
Struct
  • Object
show all
Defined in:
lib/myshazam.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#authorObject

Returns the value of attribute author

Returns:

  • (Object)

    the current value of author



5
6
7
# File 'lib/myshazam.rb', line 5

def author
  @author
end

#shazam_idObject

Returns the value of attribute shazam_id

Returns:

  • (Object)

    the current value of shazam_id



5
6
7
# File 'lib/myshazam.rb', line 5

def shazam_id
  @shazam_id
end

#titleObject

Returns the value of attribute title

Returns:

  • (Object)

    the current value of title



5
6
7
# File 'lib/myshazam.rb', line 5

def title
  @title
end

Class Method Details

.list_from_file(download_file) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/myshazam.rb', line 8

def list_from_file download_file
  ids = []

  doc = Nokogiri::HTML(File.open(download_file, "r:utf-8"))
  doc.xpath("//table/tr").map.with_index do |tr, i|
    next if i == 0  # Skip header

    # Find shazam id, don't parse return duplicates
    link = tr.xpath("./td/a").first
    shazam_id = Regexp.last_match[1].to_i if link.attributes["href"].to_s.strip =~ /t(\d+)$/
    if ids.include? shazam_id; next; else ids.push(shazam_id); end

    Track.new(
      tr.children.first.content.to_s.strip, # Title
      tr.xpath("./td[position()=2]").first.content.to_s.strip, # Author
      shazam_id
    )
  end.compact
end

Instance Method Details

#magnetObject



43
44
45
# File 'lib/myshazam.rb', line 43

def magnet
  @magnet ||= magnet_from_piratebay
end

#magnet_from_piratebayObject



33
34
35
36
37
38
39
40
41
# File 'lib/myshazam.rb', line 33

def magnet_from_piratebay
  begin
    search_url = "http://thepiratebay.se/search/%s" % URI::encode(to_s)
    doc = Nokogiri::HTML open(search_url)
    doc.xpath("//a[starts-with(@href,'magnet')]").first.attributes["href"].to_s
  rescue
    nil
  end
end

#to_sObject



29
30
31
# File 'lib/myshazam.rb', line 29

def to_s
  [self.author, self.title].compact.join(" ").strip
end