Class: Sh::Database

Inherits:
Object show all
Defined in:
lib/sh_database.rb

Class Method Summary collapse

Class Method Details

.add_song(path) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/sh_database.rb', line 83

def self.add_song path
  tags = TagReader.read path

  artist = get_or_insert_artist(:name => tags[:artist])
  album = get_or_insert_album(:title => tags[:album], :artist_id => artist.id)

  unless album.special_case?
    album.year = tags[:year]
    album.save
  end

  song = Song.create do |s|
    s.path = path
    s.artist = artist
    s.album = album
    s.title = tags[:title]
    s.track_num = tags[:track_num]

    image = tags[:cover_art]
    if image
      image_path = File.join(Global::PATHS[:cover_dir], image.to_md5)
      open(image_path, 'w') do |file|
        file.write image
      end
      s.image_path = image_path
    end
  end

  return song
end

.get_or_insert_album(query) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/sh_database.rb', line 67

def self.get_or_insert_album query
  title = query[:title]
  album = Album[Album::UNKNOWN]
  unless title.nil?
    album = case title.downcase
    when "", "unknown", "unknown album"
      Album[Artist::UNKNOWN]
    when "[non-album tracks]", "non-album", "non-album tracks"
      Album[Album::NON_ALBUM]
    else
      Album.first(query) || Album.create(query)
    end
  end
  return album
end

.get_or_insert_artist(query) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/sh_database.rb', line 51

def self.get_or_insert_artist query
  name = query[:name]
  artist = Artist[Artist::UNKNOWN]
  unless name.nil?
    artist = case name.downcase
    when "", "unknown", "unknown artist"
      Artist[Artist::UNKNOWN]
    when "various", "various artists"
      Artist[Artist::VARIOUS]
    else
      Artist.first(query) || Artist.create(query)
    end
  end
  return artist
end

.loadObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/sh_database.rb', line 8

def self.load
  @@db = Sequel.sqlite(Global::PATHS[:db_file])

  unless @@db.table_exists? :artists
    @@db.create_table :artists do
      primary_key :id
      String :mbid
      String :name
      String :image_path
      String :info
    end
    @@db[:artists].insert(:id => 1, :name => "Unknown Artist")
    @@db[:artists].insert(:id => 2, :name => "Various Artists")
  end

  unless @@db.table_exists? :albums
    @@db.create_table :albums do
      primary_key :id
      String :mbid
      String :title
      String :image_path
      String :info
      Integer :year
      foreign_key :artist_id, :artists
    end
    @@db[:albums].insert(:id => 1, :title => "Unknown Album")
    @@db[:albums].insert(:id => 2, :title => "Non-Album Tracks")
  end

  @@db.create_table :songs do
    primary_key :id
    String :mbid
    String :path
    String :title
    String :lyrics
    String :image_path
    String :info
    Integer :track_num
    foreign_key :album_id, :albums
    foreign_key :artist_id, :artists
  end unless @@db.table_exists? :songs
end