<commit_before>set :environment, :production
enable :sessions
use Rack::Flash, :sweep => true

SITE_TITLE = "Playr"
SCRIPTS = %w(mootools-core mootools-more humane mustache simple-modal fileuploader swfobject web_socket main)
STYLES = %w(bootstrap jackedup style)

configure :production do
	set :port, 80
	
	use Sinatra::CacheAssets, :max_age => (60 * 60 * 24 * 7)
	
	compressed_js = ''
	SCRIPTS.each do |js|
		code = File.read("public/js/#{js}.js")
		compressed_js << Packr.pack(code) + "\n"
	end
	File.open('public/js/app.min.js', 'wb') { |f| f.write(compressed_js) }
	SCRIPT_TAG = '<script src="/js/app.min.js"></script>'
	
	compressed_css = ''
	STYLES.each do |css|
		style = File.read("public/css/#{css}.css")
		compressed_css << Rainpress.compress(style)
	end
	File.open('public/css/app.min.css', 'wb') { |f| f.write(compressed_css) }
	STYLE_TAG = '<link rel="stylesheet" href="/css/app.min.css" />'
end

configure :development, :testing do
	script_tag = ''
	SCRIPTS.each do |js|
		script_tag << "<script src=\"/js/#{js}.js\"></script>"
	end
	SCRIPT_TAG = script_tag
	
	style_tag = ''
	STYLES.each do |css|
		style_tag << "<link rel=\"stylesheet\" href=\"/css/#{css}.css\" />"
	end
	STYLE_TAG = style_tag
end

helpers do
	def album_artwork(album, artist)
		return redis.hget "album:artwork", album + ":" + artist if redis.hexists "album:artwork", album + ":" + artist
		artwork = $lastfm.album_artwork album, artist
		redis.hset "album:artwork", album + ":" + artist, artwork
		artwork
	end
	
	def artist_info(artist)
		return redis.hget "artist:info", artist if redis.hexists "artist:info", artist
		info = $lastfm.artist artist
		redis.hset "artist:info", artist, info.to_json
		info.to_json
	end
	
	def like(song)
		Vote.up(song, @user.id)
	end
	
	def dislike(song)
		Vote.down(song, @user.id)
	end
	
	def likes(song)
		Vote.count(:song => song, :like => true)
	end
	
	def cache(key, value = nil)
		c = redis.hget "cache", key
		unless c
			redis.hset "cache", key, value.to_json
			c = value.to_json
		end
		JSON.parse(c)
	end
	
	def get_cache(key)
		c = redis.hget "cache", key
		return JSON.parse(c) if c
	end
	
	def set_cache(key, value)
		redis.hset "cache", key, value.to_json
	end
	
	def hashify(obj)
		hash = {}
		obj.instance_variables.each do |var|
			hash[var.to_s.delete("@")] = obj.instance_variable_get(var) if not var.to_s =~ /^@_/
		end
		hash
	end
	
	def likes?(sid)
		like = Vote.get(sid, @user.id)
		return nil unless like
		return like.like
	end
	
	def avatar(email, size = 80)
		hash = Digest::MD5.hexdigest(email.downcase)
		img_src = "http://www.gravatar.com/avatar/#{hash}?s=#{size}&d=mm"
	end
	
	def in_queue(song)
		q = SongQueue.all(:song => song)
		return false if q.empty?
		return true
	end
end

set(:auth) do |val|
	condition do
		redirect '/login', :error => "Must be logged in to see that page." unless @auth and @auth.is_valid?
		redirect '/', :error => "Insufficient privileges." if val == :admin and session[:user_id] != 1
	end
end

before do
	if session[:user_id]
		@user = User.get session[:user_id]
		@auth = Auth.new(@user.password, @user.secret, session, request.env)
	end
	@volume = Playr.volume.to_s
end

############
## ROUTES ##
############

get "/", :auth => true do
	@title = "Home"
	@playing = History.last.song if Playr.playing?
	@albums = repository(:default).adapter.select("SELECT `artist`, `album`, SUM(`vote` + `plays`) AS `rating` FROM `songs` GROUP BY `album` ORDER BY `rating` DESC")
	@album_count = @albums.count
	@albums = @albums[0..5]
	@artists = repository(:default).adapter.select("SELECT `artist`, SUM(`plays`) as `rating` FROM `songs` GROUP BY `artist` ORDER BY `rating` DESC")
	@artist_count = @artists.count
	@artists = @artists[0..5]
	@tracks = repository(:default).adapter.select("SELECT `id`, `title`, `album`, `artist`, SUM(`vote` + `plays`) as `rating` FROM `songs` GROUP BY `id` ORDER BY `rating` DESC")
	@track_count = @tracks.count
	@tracks = @tracks[0..5]
	@your_tracks = Song.all(:user => @user, :order => [:id.desc])
	@your_track_count = @your_tracks.count
	@your_tracks = @your_tracks[0..5]
	erb :index
end<commit_msg>Adjusted popular album algorithm.<commit_after>set :environment, :production
enable :sessions
use Rack::Flash, :sweep => true

SITE_TITLE = "Playr"
SCRIPTS = %w(mootools-core mootools-more humane mustache simple-modal fileuploader swfobject web_socket main)
STYLES = %w(bootstrap jackedup style)

configure :production do
	set :port, 80
	
	use Sinatra::CacheAssets, :max_age => (60 * 60 * 24 * 7)
	
	compressed_js = ''
	SCRIPTS.each do |js|
		code = File.read("public/js/#{js}.js")
		compressed_js << Packr.pack(code) + "\n"
	end
	File.open('public/js/app.min.js', 'wb') { |f| f.write(compressed_js) }
	SCRIPT_TAG = '<script src="/js/app.min.js"></script>'
	
	compressed_css = ''
	STYLES.each do |css|
		style = File.read("public/css/#{css}.css")
		compressed_css << Rainpress.compress(style)
	end
	File.open('public/css/app.min.css', 'wb') { |f| f.write(compressed_css) }
	STYLE_TAG = '<link rel="stylesheet" href="/css/app.min.css" />'
end

configure :development, :testing do
	script_tag = ''
	SCRIPTS.each do |js|
		script_tag << "<script src=\"/js/#{js}.js\"></script>"
	end
	SCRIPT_TAG = script_tag
	
	style_tag = ''
	STYLES.each do |css|
		style_tag << "<link rel=\"stylesheet\" href=\"/css/#{css}.css\" />"
	end
	STYLE_TAG = style_tag
end

helpers do
	def album_artwork(album, artist)
		return redis.hget "album:artwork", album + ":" + artist if redis.hexists "album:artwork", album + ":" + artist
		artwork = $lastfm.album_artwork album, artist
		redis.hset "album:artwork", album + ":" + artist, artwork
		artwork
	end
	
	def artist_info(artist)
		return redis.hget "artist:info", artist if redis.hexists "artist:info", artist
		info = $lastfm.artist artist
		redis.hset "artist:info", artist, info.to_json
		info.to_json
	end
	
	def like(song)
		Vote.up(song, @user.id)
	end
	
	def dislike(song)
		Vote.down(song, @user.id)
	end
	
	def likes(song)
		Vote.count(:song => song, :like => true)
	end
	
	def cache(key, value = nil)
		c = redis.hget "cache", key
		unless c
			redis.hset "cache", key, value.to_json
			c = value.to_json
		end
		JSON.parse(c)
	end
	
	def get_cache(key)
		c = redis.hget "cache", key
		return JSON.parse(c) if c
	end
	
	def set_cache(key, value)
		redis.hset "cache", key, value.to_json
	end
	
	def hashify(obj)
		hash = {}
		obj.instance_variables.each do |var|
			hash[var.to_s.delete("@")] = obj.instance_variable_get(var) if not var.to_s =~ /^@_/
		end
		hash
	end
	
	def likes?(sid)
		like = Vote.get(sid, @user.id)
		return nil unless like
		return like.like
	end
	
	def avatar(email, size = 80)
		hash = Digest::MD5.hexdigest(email.downcase)
		img_src = "http://www.gravatar.com/avatar/#{hash}?s=#{size}&d=mm"
	end
	
	def in_queue(song)
		q = SongQueue.all(:song => song)
		return false if q.empty?
		return true
	end
end

set(:auth) do |val|
	condition do
		redirect '/login', :error => "Must be logged in to see that page." unless @auth and @auth.is_valid?
		redirect '/', :error => "Insufficient privileges." if val == :admin and session[:user_id] != 1
	end
end

before do
	if session[:user_id]
		@user = User.get session[:user_id]
		@auth = Auth.new(@user.password, @user.secret, session, request.env)
	end
	@volume = Playr.volume.to_s
end

############
## ROUTES ##
############

get "/", :auth => true do
	@title = "Home"
	@playing = History.last.song if Playr.playing?
	@albums = repository(:default).adapter.select("SELECT `artist`, `album`, (`vote` + `plays`) AS `rating` FROM `songs` GROUP BY `album` ORDER BY `rating` DESC")
	@album_count = @albums.count
	@albums = @albums[0..5]
	@artists = repository(:default).adapter.select("SELECT `artist`, SUM(`plays`) as `rating` FROM `songs` GROUP BY `artist` ORDER BY `rating` DESC")
	@artist_count = @artists.count
	@artists = @artists[0..5]
	@tracks = repository(:default).adapter.select("SELECT `id`, `title`, `album`, `artist`, SUM(`vote` + `plays`) as `rating` FROM `songs` GROUP BY `id` ORDER BY `rating` DESC")
	@track_count = @tracks.count
	@tracks = @tracks[0..5]
	@your_tracks = Song.all(:user => @user, :order => [:id.desc])
	@your_track_count = @your_tracks.count
	@your_tracks = @your_tracks[0..5]
	erb :index
end
{'fraction_non_alphanumeric': 0.16923824565666384, 'fraction_numerical': 0.005102660673065241, 'mean_word_length': 4.135371179039302, 'pattern_counts': {'":': 6, '<': 31, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2579233', 'n_tokens_mistral': 3394, 'n_tokens_neox': 3129, 'n_words': 764}