-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix song and lyric import and creation
- Loading branch information
kellen hart
committed
Oct 20, 2013
1 parent
1ba8341
commit c3d1143
Showing
15 changed files
with
490 additions
and
484 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
class Song < ActiveRecord::Base | ||
attr_accessible :chords, :date_written, :lyrics, :name | ||
attr_accessible :date_written, :lyrics, :name, :capo_instructions | ||
|
||
has_many :song_albums | ||
has_many :albums, through: :song_albums | ||
|
||
validates :chords, :date_written, :lyrics, :name, presence: true | ||
validates :lyrics, :name, presence: true | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,23 @@ | ||
# This file should contain all the record creation needed to seed the database with its default values. | ||
# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup). | ||
# | ||
require 'faker' | ||
require 'seeds_helper' | ||
require 'seeds_development_words' | ||
# require 'seeds_production_data' | ||
require 'seeds_production_data' | ||
include SeedsHelper | ||
|
||
ALBUM_BANDCAMP_NUMBERS = ['3132328764', '987155316'] | ||
ALBUM_BANDCAMP_NAMES = ["rainbow-gardens","sing-me-home"] #10 songs / 9 songs | ||
ActiveRecord::Base.transaction do | ||
songs = [] | ||
albums = [] | ||
|
||
14.times do | ||
songs << Song.create!( | ||
chords: "A B C\n1 2 3#{(80+rand(23)).chr}", | ||
date_written: Time.now - rand(200).days, | ||
lyrics: make_lyrics(verses: "~4"), | ||
name: make_title | ||
) | ||
end | ||
|
||
2.times do |n| | ||
albums << Album.create!( | ||
title: make_title, | ||
description: Faker::Lorem.paragraph(5), | ||
date_recorded: Time.now - rand(200).days, | ||
title: ALBUM_NAMES[n], | ||
date_recorded: ALBUM_DATES[n], | ||
image_path: "album_#{n}", | ||
bandcamp_name: ALBUM_BANDCAMP_NAMES[n], | ||
bandcamp_id: ALBUM_BANDCAMP_NUMBERS[n] | ||
) | ||
end | ||
|
||
# attach the songs to an album | ||
song_sets = [(0..8), (9..13)] | ||
2.times do |album_number| | ||
track_counter_offset = song_sets[album_number].first - 1 | ||
|
||
song_sets[album_number].each do |song_number| | ||
SongAlbum.create!( | ||
album_id: albums[album_number].id, | ||
song_id: songs[song_number].id, | ||
track_number: song_number - track_counter_offset | ||
) | ||
end | ||
end | ||
parse_production_lyrics | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,69 @@ | ||
module SeedsHelper | ||
def make_lyrics(opts) | ||
num_verses = opts[:verses] || 3 + rand(3) | ||
lines_per_verse = opts[:lines_per_verse] || 3 + rand(5) | ||
words_per_line = opts[:words_per_line] || 3 + rand(7) | ||
lyrics = "" | ||
|
||
if num_verses.is_a?(String) && num_verses[0] == "~" | ||
num_verses = num_verses[1..-1].to_i | ||
num_verses = num_verses/2 + rand(num_verses) | ||
else | ||
num_verses = num_verses.to_i | ||
end | ||
ALBUM_BANDCAMP_NUMBERS = ['987155316', '3132328764'] | ||
ALBUM_BANDCAMP_NAMES = ["sing-me-home", "rainbow-gardens"] #7 songs / 10 songs | ||
ALBUM_NAMES = ["Sing Me Home", "Rainbow Gardens"] | ||
ALBUM_DATES = [Date.new(2001, 8, 1), Date.new(2013, 8, 1)] | ||
|
||
num_verses.times { lyrics << construct_a_verse(lines_per_verse, words_per_line) } | ||
def parse_production_lyrics | ||
lyrics_array = PRODUCTION_LYRICS.lines.to_a | ||
song_count = 0 | ||
|
||
lyrics.strip | ||
loop do | ||
song_attrs = {} | ||
song_count <= 9 ? album_index = 0 : album_index = 1 | ||
|
||
song_attrs[:name], song_attrs[:date_written], song_attrs[:capo_instructions] = | ||
get_name_date_capo( lyrics_array.shift ) | ||
|
||
# move past empty lines | ||
lyrics_array.shift until lyrics_array.first.strip != "" | ||
|
||
# get lyrics | ||
song_attrs[:lyrics] = get_lyrics(lyrics_array) | ||
song = Song.create!(song_attrs) | ||
|
||
song_set = SongAlbum.create!( | ||
album: Album.find_by_bandcamp_name(ALBUM_BANDCAMP_NAMES[album_index]), | ||
song: song, | ||
track_number: song_count % 10 + 1 | ||
) | ||
|
||
song_count += 1 | ||
break if lyrics_array.empty? | ||
end | ||
end | ||
|
||
def construct_a_verse(lines_per_verse, words_per_line) | ||
verse = "" | ||
lines_per_verse.times do | ||
words_per_line.times { verse << "#{WORDS_ARRAY.sample} " } | ||
verse << "\n" | ||
def get_name_date_capo(string) | ||
metadata_array = string.strip.split(/\s{3,}/) | ||
|
||
name = metadata_array.shift | ||
begin | ||
date_string = metadata_array.pop | ||
date_written = Date::strptime(date_string,"%Y") unless metadata_array.empty? | ||
rescue Error | ||
puts "error on #{date_string}" | ||
end | ||
capo_instructions = metadata_array.pop unless metadata_array.empty? | ||
|
||
verse << "\n" | ||
[name, date_written, capo_instructions] | ||
end | ||
|
||
def make_title | ||
num_words = 1 + rand(4) | ||
title = "" | ||
num_words.times { title << "#{WORDS_ARRAY.sample} "} | ||
def get_lyrics(lyrics_array) | ||
empty_lines_in_a_row = 0 | ||
next_line = nil | ||
lyrics = "" | ||
while empty_lines_in_a_row < 4 | ||
next_line = lyrics_array.shift | ||
return lyrics if next_line == nil | ||
|
||
title.strip | ||
if next_line.strip == "" | ||
empty_lines_in_a_row += 1 | ||
else | ||
empty_lines_in_a_row = 0 | ||
end | ||
lyrics += next_line | ||
end | ||
|
||
lyrics.strip | ||
end | ||
end |
Oops, something went wrong.