Class: Organo::AnimeDatabase

Inherits:
Object
  • Object
show all
Defined in:
lib/organo/anime_database.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAnimeDatabase

Returns a new instance of AnimeDatabase.



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
# File 'lib/organo/anime_database.rb', line 17

def initialize
  @database = Sequel.sqlite(Config::DATABASE_FILE)

  unless @database.table_exists?(:shows)
    @database.create_table(:shows) do
      Integer :mal_id
      String :slug, unique: true, null: true
      String :title
      String :img_url
      String :search_query, null: true
      Boolean :is_sequel
      primary_key %i[mal_id]
    end
  end

  unless @database.table_exists?(:sequels)
    @database.create_table(:sequels) do
      Integer :parent_id
      Integer :child_id
      String :type, default: 'season'
      Integer :type_num, default: 0
      String :subtype, default: ''
      Integer :subtype_num, default: 0
      Integer :subtype_selector, default: -1
      primary_key %i[parent_id child_id]
    end
  end
end

Class Method Details

.instanceObject



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

def self.instance
  @instance.nil? ? new : @instance
end

Instance Method Details

#anime_add(mal_id) ⇒ Object

Adds show entry to the database using the specified MyAnimeList ID



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
# File 'lib/organo/anime_database.rb', line 54

def anime_add(mal_id)
  show = show_by_id(mal_id).first
  if show.nil?
    show_json = Query.get_anime_by_id(mal_id)
    @database.transaction do
      @database.after_commit do
        puts 'Added show to the database.'
      end
      @database.after_rollback do
        puts 'An error occurred. No change was made.'
      end
      @database.from(:shows).insert(
        mal_id: show_json['mal_id'].to_i,
        slug: generate_unique_slug(show_json['title']),
        title: show_json['title'],
        img_url: show_json['images']['jpg']['image_url'],
        search_query: show_json['title'].gsub(':', ' -'),
        is_sequel: false
      )
    end
  else
    puts 'This show already exists in the database.'
  end
  show_by_id(mal_id).first
end


132
133
134
135
136
137
138
139
140
141
142
# File 'lib/organo/anime_database.rb', line 132

def anime_find_broken_links
  shows = @database.from(:shows)
  broken_list = []
  shows.each do |show|
    response = HTTParty.get(show[:img_url])
    next unless response.code == 404

    broken_list.push(show[:mal_id])
  end
  broken_list
end

#anime_remove(mal_id) ⇒ Object

Removes show entry from the database using the specified MyAnimeList ID



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/organo/anime_database.rb', line 82

def anime_remove(mal_id)
  show = show_by_id(mal_id).first
  if show.nil?
    puts 'This show doesn\'t exist in the database.'
  else
    @database.transaction do
      show_by_id(mal_id).delete
      puts 'The show was removed from the database.'
    end
  end
  show
end

#anime_show(mal_id) ⇒ Object

Get database entry of show using the specified MyAnimeList ID



48
49
50
# File 'lib/organo/anime_database.rb', line 48

def anime_show(mal_id)
  show_by_id(mal_id).first
end

#anime_update(mal_id, params) ⇒ Object

Edits anime entry with specified params



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/organo/anime_database.rb', line 97

def anime_update(mal_id, params)
  anime = show_by_id(mal_id)

  if anime.first.nil?
    puts "Specified show doesn't exist."
  elsif params.empty?
    puts 'No change was made.'
  else
    @database.transaction do
      @database.after_commit do
        puts 'Show values changed.'
      end
      @database.after_rollback do
        puts 'An error occurred. No change was made.'
      end
      anime.update(params)
    end
  end
  anime
end

#anime_update_image(mal_id_list) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/organo/anime_database.rb', line 118

def anime_update_image(mal_id_list)
  mal_id_list.each do |mal_id|
    show = show_by_id(mal_id)
    api_image = Query.get_anime_image(mal_id)
    if show.count.positive?
      show.update(img_url: api_image)
      puts 'Updated image.'
      puts show.first
    else
      puts "MAL ID #{mal_id} doesn't exist in the database."
    end
  end
end

#sequel_remove(parent_id, child_id) ⇒ Object

Removes sequel entry from database



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/organo/anime_database.rb', line 199

def sequel_remove(parent_id, child_id)
  sequel = sequel_by_id(parent_id, child_id)
  if sequel.first.nil?
    puts "Specified sequel doesn't exist."
  else
    @database.transaction do
      @database.after_commit do
        puts 'Sequel entry was removed.'
      end
      @database.after_rollback do
        puts 'An error occurred. No change was made.'
      end
      child_show = show_by_id(sequel.first[:child_id])
      child_show.update(
        slug: generate_unique_slug(child_show.first[:title]),
        is_sequel: false
      )
      sequel.delete
    end
  end
  sequel
end

#sequel_set(parent_id, child_id, type, type_num, subtype, subtype_num, subtype_selector) ⇒ Object

Adds sequel entry for parent and child show



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/organo/anime_database.rb', line 146

def sequel_set(parent_id, child_id, type, type_num, subtype, subtype_num, subtype_selector)
  parent_show = show_by_id(parent_id)
  child_show = show_by_id(child_id)
  if !parent_show.first.nil? && !child_show.first.nil?
    if @database.from(:sequels).where(parent_id: child_id).count.zero?
      @database.from(:sequels).insert(
        parent_id: parent_id,
        child_id: child_id,
        type: type,
        type_num: type_num,
        subtype: subtype,
        subtype_num: subtype_num,
        subtype_selector: subtype_selector
      )
      child_show.update(slug: nil, is_sequel: true)
      puts 'Added sequel to the database.'
    else
      puts "Can't add sequel to database."
      puts "#{child_show.first[:title]}(#{child_show.first[:mal_id]}) is already a parent for sequels."
    end
  else
    puts "Can't add sequel to database."
    puts "Parent MAL_ID #{parent_id} doesn't exist in the database." if parent_show.first.nil?
    puts "Child MAL_ID #{child_id} doesn't exist in the database." if child_show.first.nil?
  end
  sequel_by_id(parent_id, child_id)
end

#sequel_update(parent_id, child_id, params) ⇒ Object

Edits sequel entry with specified params



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/organo/anime_database.rb', line 176

def sequel_update(parent_id, child_id, params)
  sequel = sequel_by_id(parent_id, child_id)

  if sequel.first.nil?
    puts "Specified sequel doesn't exist."
  elsif params.empty?
    puts 'No change was made.'
  else
    @database.transaction do
      @database.after_commit do
        puts 'Sequel values changed.'
      end
      @database.after_rollback do
        puts 'An error occurred. No change was made.'
      end
      sequel.update(params)
    end
  end
  sequel
end

#statisticsObject



222
223
224
225
226
# File 'lib/organo/anime_database.rb', line 222

def statistics
  puts "Total show count: #{@database.from(:shows).count}"
  puts "Non-sequel show count #{@database.from(:shows).where(is_sequel: false).count}"
  puts "Sequel show count: #{@database.from(:sequels).count}"
end