Skip to content

Commit

Permalink
Merge branch 'master' into dockerize
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamie0 committed Mar 29, 2018
2 parents f060fc4 + 4d66473 commit 3f23100
Show file tree
Hide file tree
Showing 17 changed files with 283 additions and 107 deletions.
9 changes: 5 additions & 4 deletions app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
require 'sinatra/xsendfile'
require 'yaml'

require './modules'
$: << '.'
require_relative 'modules'

Bundler.require
$config = YAML::load(File.read("config.yml"))
$genres = YAML::load(File.read("genres.yml"))
$config = YAML::load(File.read(File.join(File.dirname(__FILE__), "config.yml")))
$genres = YAML::load(File.read(File.join(File.dirname(__FILE__), "genres.yml")))
$env = {:slave => false}

require './database'
require_relative 'database'

Resque::Plugins::Status::Hash.expire_in = (60 * 60) * 24

Expand Down
5 changes: 3 additions & 2 deletions app/mixin/spotify.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def self.connect_to_api
def self.search track, token

title_clean = track.title.split(/(ft\.|feat\.?\s)/)[0].gsub('"', '').gsub(/[\(\[][a-zA-Z0-9\ \.\-]+[\)\]]/, '').split("feat.")[0].strip
artist_clean = track.artist.split(/(ft\.|feat\.?\s)/)[0].gsub('"', '').split("feat.")[0].strip
artist_clean = track.artist.name.split(/(ft\.|feat\.?\s)/)[0].gsub('"', '').split("feat.")[0].strip
http_response = RestClient.get("https://api.spotify.com/v1/search", {params: {q: 'track:"' + title_clean + '" artist:"' + artist_clean + '"', type: "track", market: "GB"}, authorization: token})
response = JSON.parse(http_response.body) rescue nil

Expand Down Expand Up @@ -54,6 +54,7 @@ def self.created track
images = result['album']['images'] rescue []
images.sort! { | a, b | b['width'] <=> a['width'] }

track.extra ||= {}
track.extra['spotify_id'] = spotify_id
track.extra['album_art'] = images[0]['url'] if images.length and images[0]['width'] > 400
track.save
Expand Down Expand Up @@ -111,4 +112,4 @@ def self.pre_publish track, playout

end

end; end
end; end
10 changes: 10 additions & 0 deletions app/models/cache_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ def exists?
Track.where('external_id=?', external_id).count > 0
end

def init_by data

data['year'] = data['year'] == '' ? 0 : data['year']
data['track'] = data['title']

data = data.select {|x| self.class.attribute_names.index(x) }
assign_attributes data

end

def get_json enhanced = false
return {
"cache_id" => id,
Expand Down
73 changes: 47 additions & 26 deletions app/models/track.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,45 @@ def approved
!!status and status > 3
end

def get_metadata
def update_metadata

result = Nerve::Services::Metadata.match artist.name, (album.name rescue ''), title, true, true
external_id = result['external_id']
get_metadata

result = Nerve::Services::Metadata.match_meta(external_id, true, "external_id")
big = result["big"] rescue nil
end

lyrics = JSON.parse(big["lyrics"])[0] rescue ""
def get_metadata

if !lyrics or lyrics.to_s.empty?
lyrics = Nerve::Services::Metadata.match_lyrics(artist.name, album.name, title)[0] \
rescue "No lyrics available."
result, cache_item = Nerve::Services::Metadata.match_meta(external_id, true, "external_id", true)
_big = result["big"] rescue nil

_lyrics = (cache_item.lyrics or JSON.parse(_big["lyrics"])[0]) rescue ""
_lyrics = _lyrics[0] if _lyrics.is_a? Array
_lyrics = '' if !_lyrics or _lyrics.to_s.empty? or _lyrics.to_s == '0' or _lyrics.to_s == 'false'

if _lyrics == ''
_lyrics = "No lyrics available."
begin
_lyrics = Nerve::Services::Metadata.match_lyrics(artist.name, (album.name rescue ''), title)[0]
if cache_item
cache_item.lyrics = _lyrics
cache_item.save
end
rescue
_lyrics = "No lyrics available."
end
end

[result, lyrics, (result["year"] rescue nil)]
[result, _lyrics, (result["year"] rescue nil)]

end

# Unguaranteed to be absolutely safe, but it's more safe than sorry.
def is_safe

return false if explicit or status == 0
extended, lyrics = get_metadata
_extended, _lyrics = get_metadata

return true if instrumental

Expand All @@ -53,42 +71,45 @@ def is_safe
r = Regexp.new("\\b((#{$config["words"]["banned"].join("|")})[^\\s\\b,.\<\>]*)\\b", 7)

# If the size is 0, then it's "safe" ish
return false if !lyrics or lyrics.to_s.empty?
return false if !_lyrics or _lyrics.to_s.empty? or _lyrics == 0

# TODO: make this cleaner
lyrics.scan(r).size == 0 && !!lyrics && lyrics != "" && lyrics != "No lyrics available."
_lyrics.scan(r).size == 0 && !!_lyrics && _lyrics != "" && _lyrics != "No lyrics available."

end

def set_unsafe
explicit = 1
explicit = true
end

def why_unsafe

return "determined as risky during upload" if status == 0 # only works before submission
return "has parental advisory/explicit flag" if explicit
extended, lyrics = get_metadata
reasons = []
reasons << "determined as risky during upload" if status == 0 # only works before submission
reasons << "has parental advisory/explicit flag" if explicit
_extended, _lyrics = get_metadata

r = Regexp.new("\\b((#{$config["words"]["banned"].join("|")})[^\\s\\b,.\<\>]*)\\b", 7)
s = lyrics.scan(r)

return "it contains (at least one) expletive (#{s[0]})" if s.size > 0
return "no lyrics were found" if !lyrics or lyrics == "No lyrics available."
if !_lyrics.to_s.empty? && _lyrics != 0 && _lyrics != "No lyrics available."
r = Regexp.new("\\b((#{$config["words"]["banned"].join("|")})[^\\s\\b,.\<\>]*)\\b", 7)
s = _lyrics.scan(r)
reasons << "it contains (at least one) expletive (#{s[0]})" if s.size > 0
else
reasons << "no lyrics were found"
end

false
reasons.length > 0 ? reasons : false

end

def delete! soft = false
local_path = $config["export"]["directory"] + "/" + local_path
File.unlink(local_path) rescue nil
_local_path = $config["export"]["directory"] + "/" + local_path
File.unlink(_local_path) rescue nil
if soft
self.status = 6
self.save
else
File.unlink(local_path + ".ogg") rescue nil
File.unlink(local_path + ".dat") rescue nil
File.unlink(_local_path + ".ogg") rescue nil
File.unlink(_local_path + ".dat") rescue nil
destroy
end
end
Expand All @@ -114,7 +135,7 @@ def get_json extended = false
"is_automation" => is_automation,
"playout_id" => playout_id,
"end_type" => end_type,
"created_by" => user.get_json,
"created_by" => user && user.get_json,
"flagged" => flagged,
"instrumental" => instrumental,
"extra" => extra
Expand Down
37 changes: 20 additions & 17 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,32 @@ class User < ActiveRecord::Base
alias_attribute :moderator, :is_moderator
alias_attribute :playout_username, :playout

attr_reader :permissions
serialize :permissions, JSON

def after_initialize

@permissions = {
:override_bitrate => false,
:override_compressor => false,
:upload_library => false,
:upload_automation => false,
:instrumental => false,
@_permissions = {
'override_bitrate' => false,
'override_compressor' => false,
'upload_library' => false,
'upload_automation' => false,
'instrumental' => false,
# Safety net ensures the first 3 uploads for a user are moderated. TODO
:safety_net => true,
'safety_net' => true,
}

permissions.each { | k, v| @permissions[k] = v }
permissions.each { | k, v| @_permissions[k] = v } if permissions.is_a? Hash

if admin or moderator
@permissions[:override_bitrate] = true
@permissions[:override_compressor] = true
@permissions[:upload_library] = true
@permissions[:instrumental] = true
@permissions[:upload_automation] = true
@permissions[:safety_net] = false
@_permissions['override_bitrate'] = true
@_permissions['override_compressor'] = true
@_permissions['upload_library'] = true
@_permissions['instrumental'] = true
@_permissions['upload_automation'] = true
@_permissions['safety_net'] = false
end

permissions = @permissions
write_attribute :permissions, @_permissions

end

Expand All @@ -62,6 +62,9 @@ def to_json state = nil
get_json.to_json
end

after_initialize :after_initialize
after_find :after_initialize

end

end; end
end; end
12 changes: 6 additions & 6 deletions auth/cortex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ def authorize session, params, request
user.admin = !session[:user]["groups"][admin].nil?


if !session[:user]["groups"][specialist].nil?
user.permissions[:override_bitrate] = true
user.permissions[:override_compressor] = true
user.permissions[:instrumental] = true
if !session[:user]["groups"][specialist].nil? or user.admin or user.moderator
user.permissions['override_bitrate'] = true
user.permissions['override_compressor'] = true
user.permissions['instrumental'] = true
else
user.permissions[:override_bitrate] = false
user.permissions[:override_compressor] = false
user.permissions['override_bitrate'] = false
user.permissions['override_compressor'] = false
end

user.save!
Expand Down
26 changes: 13 additions & 13 deletions db/migrate/20180112204406_startup.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
class Startup < ActiveRecord::Migration
def change

execute <<-SQL
begin
execute "alter table albums drop key `in_artist`;"

execute "alter table albums drop key `external_id`;"

alter table albums drop key `in_artist`;
alter table albums drop key `external_id`;
execute "alter table artists drop key `external_id`;"
execute "alter table artists drop key in_artist;"
execute "alter table artists drop key index_artist;"

alter table artists drop key `external_id`;
alter table artists drop key in_artist;
alter table artists drop key index_artist;
execute "alter table migrate_cache drop key full;"
execute "alter table nerve_cache drop key `external_id`, drop key `n_uuid`, drop key `n_tr`, drop key `n_td`;"

alter table migrate_cache drop key full;
alter table nerve_cache drop key `external_id`, drop key `n_uuid`, drop key `n_tr`, drop key `n_td`;
execute "alter table tracks drop key in_artist, drop key in_album, drop key in_title, drop key index_title;"

alter table tracks drop key in_artist, drop key in_album, drop key in_title, drop key index_title, drop key external_id;
alter table users drop key external_ref;
SQL
execute "alter table users drop key external_ref;"
rescue
end

end
end
13 changes: 13 additions & 0 deletions db/migrate/20180217105112_update_track_cols.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class UpdateTrackCols < ActiveRecord::Migration
def change

change_column :tracks, :explicit, :boolean
change_column :tracks, :flagged, :boolean
change_column :tracks, :explicit, :boolean
change_column :tracks, :instrumental, :boolean
change_column :tracks, :is_library, :boolean
change_column :tracks, :explicit, :boolean
change_column :tracks, :is_automation, :boolean

end
end
8 changes: 8 additions & 0 deletions db/migrate/20180217115144_col_ab.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class ColAb < ActiveRecord::Migration
def change

add_column :tracks, :category_a, :integer
add_column :tracks, :category_b, :integer

end
end
Loading

0 comments on commit 3f23100

Please sign in to comment.