Class: UncleKryon::Hacker

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/unclekryon/hacker.rb

Constant Summary collapse

HAX_DIRNAME =
'hax'
HAX_KRYON_FILENAME =
'kryon_<hax>_<release>.yaml'
TRAIN_DIRNAME =
'train'
TRAIN_KRYON_FILENAME =
'kryon.yaml'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#init_log, #log

Constructor Details

#initialize(hax_dirname: HAX_DIRNAME, hax_kryon_filename: HAX_KRYON_FILENAME, no_clobber: false, train_dirname: TRAIN_DIRNAME, train_kryon_filename: TRAIN_KRYON_FILENAME, **options) ⇒ Hacker

Returns a new instance of Hacker.



40
41
42
43
44
45
46
47
# File 'lib/unclekryon/hacker.rb', line 40

def initialize(hax_dirname: HAX_DIRNAME,hax_kryon_filename: HAX_KRYON_FILENAME,no_clobber: false,
      train_dirname: TRAIN_DIRNAME,train_kryon_filename: TRAIN_KRYON_FILENAME,**options)
  @hax_dirname = hax_dirname
  @hax_kryon_filename = hax_kryon_filename
  @no_clobber = no_clobber
  @train_dirname = train_dirname
  @train_kryon_filename = train_kryon_filename
end

Instance Attribute Details

#hax_dirnameObject

Returns the value of attribute hax_dirname.



32
33
34
# File 'lib/unclekryon/hacker.rb', line 32

def hax_dirname
  @hax_dirname
end

#hax_kryon_filenameObject

Returns the value of attribute hax_kryon_filename.



33
34
35
# File 'lib/unclekryon/hacker.rb', line 33

def hax_kryon_filename
  @hax_kryon_filename
end

#no_clobberObject Also known as: no_clobber?

Returns the value of attribute no_clobber.



34
35
36
# File 'lib/unclekryon/hacker.rb', line 34

def no_clobber
  @no_clobber
end

#train_dirnameObject

Returns the value of attribute train_dirname.



35
36
37
# File 'lib/unclekryon/hacker.rb', line 35

def train_dirname
  @train_dirname
end

#train_kryon_filenameObject

Returns the value of attribute train_kryon_filename.



36
37
38
# File 'lib/unclekryon/hacker.rb', line 36

def train_kryon_filename
  @train_kryon_filename
end

Instance Method Details

#build_hax_kryon_aums_filepath(release) ⇒ Object



287
288
289
# File 'lib/unclekryon/hacker.rb', line 287

def build_hax_kryon_aums_filepath(release)
  return build_hax_kryon_filepath('aums',release)
end

#build_hax_kryon_filepath(hax, release) ⇒ Object



291
292
293
294
295
296
297
298
299
# File 'lib/unclekryon/hacker.rb', line 291

def build_hax_kryon_filepath(hax,release)
  raise 'Release (year) arg is nil' if release.nil?

  fn = @hax_kryon_filename.clone
  fn = fn.gsub('<hax>',hax)
  fn = fn.gsub('<release>',release.to_s)

  return File.join(@hax_dirname,fn)
end

#build_train_kryon_filepathObject



301
302
303
# File 'lib/unclekryon/hacker.rb', line 301

def build_train_kryon_filepath
  return File.join(@train_dirname,@train_kryon_filename)
end

#create_kryon_aum_year_album_parser(date, year = nil, index = nil) ⇒ Object



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
# File 'lib/unclekryon/hacker.rb', line 49

def create_kryon_aum_year_album_parser(date,year=nil,index=nil)
  pd = parse_date(date,year,index)
  date = pd[:date]
  index = pd[:index]
  year = pd[:year]

  # Try the yaml file
  artist = ArtistDataData.load_file(build_hax_kryon_aums_filepath(year))
  release = artist.releases[year]

  if release.nil?
    # Try manually from the site
    year_parser = create_kryon_aum_year_parser(year)
    artist = year_parser.artist
    release = year_parser.parse_site
    raise "Release[#{year}] does not exist" if release.nil?
  end

  album = find_kryon_aum_year_album(artist,date,year,index)[0]
  album_parser = KryonAumYearAlbumParser.new(artist,album.url)

  album_parser.album = album
  album_parser.trainers.filepath = build_train_kryon_filepath

  return album_parser
end

#create_kryon_aum_year_parser(year) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/unclekryon/hacker.rb', line 76

def create_kryon_aum_year_parser(year)
  year_parser = KryonAumYearParser.new(year)

  year_parser.trainers.filepath = build_train_kryon_filepath

  return year_parser
end

#find_kryon_aum_year_album(artist, date, year = nil, index = nil) ⇒ Object



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
# File 'lib/unclekryon/hacker.rb', line 84

def find_kryon_aum_year_album(artist,date,year=nil,index=nil)
  album = nil
  albums = []

  artist.albums.values.each_with_index do |a,i|
    date_begin = Util.parse_date_s(a.date_begin)
    date_end = Util.parse_date_s(a.date_end)

    if (date_begin && ((date_end  && date >= date_begin && date <= date_end) ||
                       (!date_end && date == date_begin)))
      albums.push([a,i])
    end
  end

  if !albums.empty?
    if index >= 0 && index < albums.length
      album = albums[index]
    else
      raise "Invalid album ordinal number[#{index + 1}]"
    end
  end

  raise "Invalid album[#{date}]" if album.nil?

  return album
end

#parse_date(date, year = nil, index = nil) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/unclekryon/hacker.rb', line 111

def parse_date(date,year=nil,index=nil)
  if !date.is_a?(Date)
    ds = date.split(':')
    date = ds[0]

    if index.nil?
      index = ds
      index = (index.length <= 1) ? 0 : (index[1].to_i - 1)
    end

    begin
      new_date = Date.strptime(date,'%Y.%m.%d')
    rescue ArgumentError
      new_date = Date.strptime(date,'%m.%d')

      if !year.nil?
        new_date = Date.new(year.to_i,new_date.month,new_date.day)
      end
    end

    date = new_date
  elsif index.nil?
    index = 0
  end

  if year.nil?
    # year is actually the release's title, so only override it if have to
    year = date.year.to_s
  end

  return {date: date,index: index,year: year}
end

#parse_kryon_aum_year(year) ⇒ Object



144
145
146
147
148
149
150
151
152
153
# File 'lib/unclekryon/hacker.rb', line 144

def parse_kryon_aum_year(year)
  year_parser = create_kryon_aum_year_parser(year)
  release = year_parser.parse_site

  if @no_clobber
    puts release.to_s
  else
    year_parser.artist.save_to_file(build_hax_kryon_aums_filepath(year))
  end
end

#parse_kryon_aum_year_album(date, year = nil, index = nil) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/unclekryon/hacker.rb', line 155

def parse_kryon_aum_year_album(date,year=nil,index=nil)
  pd = parse_date(date,year,index)
  date = pd[:date]
  index = pd[:index]
  year = pd[:year]

  album_parser = create_kryon_aum_year_album_parser(date,year,index)
  album_parser.parse_site

  if @no_clobber
    puts album_parser.album.to_s
  else
    album_parser.artist.save_to_file(build_hax_kryon_aums_filepath(year))
  end
end

#parse_kryon_aum_year_albums(year, begin_album = nil) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/unclekryon/hacker.rb', line 171

def parse_kryon_aum_year_albums(year,begin_album=nil)
  if !begin_album.nil?
    pd = parse_date(begin_album,year)
    begin_album = pd[:date]
    index = pd[:index]
    year = pd[:year]
  end

  # Try the yaml file
  artist = ArtistDataData.load_file(build_hax_kryon_aums_filepath(year))
  release = artist.releases[year]
  updated_on = nil

  if release.nil?
    # Try manually from the site
    year_parser = create_kryon_aum_year_parser(year)
    artist = year_parser.artist
    release = year_parser.parse_site
    raise "Release[#{year}] does not exist" if release.nil?
    updated_on = release.updated_on
  end

  album_parser = KryonAumYearAlbumParser.new
  album_parser.trainers.filepath = build_train_kryon_filepath
  album_parser.updated_on = updated_on unless updated_on.nil?

  albums = release.albums

  if !begin_album.nil?
    album_index = find_kryon_aum_year_album(artist,begin_album,year,index)[1]
    albums = albums[album_index..-1]
  end

  albums.each do |album_id|
    album = artist.albums[album_id]
    log.info("Hacking album[#{album.date_begin},#{album.date_end},#{album.title}]")
    #album = album_parser.parse_site(artist,album.url)
  end

  if @no_clobber
    puts release.to_s
  else
    artist.save_to_file(build_hax_kryon_aums_filepath(year))
  end
end

#train_kryon_aum_year(year) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
# File 'lib/unclekryon/hacker.rb', line 217

def train_kryon_aum_year(year)
  year_parser = create_kryon_aum_year_parser(year)
  year_parser.training = true
  year_parser.parse_site

  if @no_clobber
    puts year_parser.trainers.to_s
  else
    year_parser.trainers.save_to_file
  end
end

#train_kryon_aum_year_album(date, year = nil, index = nil) ⇒ Object



229
230
231
232
233
234
235
236
237
238
239
# File 'lib/unclekryon/hacker.rb', line 229

def train_kryon_aum_year_album(date,year=nil,index=nil)
  album_parser = create_kryon_aum_year_album_parser(date,year,index)
  album_parser.training = true
  album_parser.parse_site

  if @no_clobber
    puts album_parser.trainers.to_s
  else
    album_parser.trainers.save_to_file
  end
end

#train_kryon_aum_year_albums(year, begin_album = nil) ⇒ Object



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/unclekryon/hacker.rb', line 241

def train_kryon_aum_year_albums(year,begin_album=nil)
  if !begin_album.nil?
    pd = parse_date(begin_album,year)
    begin_album = pd[:date]
    index = pd[:index]
    year = pd[:year]
  end

  # Try the yaml file
  artist = ArtistDataData.load_file(build_hax_kryon_aums_filepath(year))
  release = artist.releases[year]

  if release.nil?
    # Try manually from the site
    year_parser = create_kryon_aum_year_parser(year)
    artist = year_parser.artist
    release = year_parser.parse_site
    raise "Release[#{year}] does not exist" if release.nil?
  end

  albums = release.albums

  if !begin_album.nil?
    album_index = find_kryon_aum_year_album(artist,begin_album,year,index)[1]
    albums = albums[album_index..-1]
  end

  albums.each do |album_id|
    album = artist.albums[album_id]

    album_parser = KryonAumYearAlbumParser.new
    album_parser.album = album
    album_parser.trainers.filepath = build_train_kryon_filepath
    album_parser.training = true

    log.info("Training album[#{album.date_begin},#{album.date_end},#{album.title}]")
    #album = album_parser.parse_site(artist,album.url)

    if @no_clobber
      puts album_parser.trainers.to_s
    else
      album_parser.trainers.save_to_file
    end
  end
end