-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
60 lines (47 loc) · 1.59 KB
/
app.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
require "sinatra"
require "sinatra/config_file"
require "sinatra/reloader" if development?
require "rom"
class App < Sinatra::Base
register Sinatra::ConfigFile
config_file "config/settings.yml"
db = settings.database_name
db_options = {
username: settings.database_user,
password: settings.database_pass,
encoding: "UTF8"
}
rom = ROM.container(:sql, "postgres://#{settings.database_host}/#{db}", db_options) do |config|
config.relation(:podcasts) do
schema(infer: true)
auto_struct true
end
config.relation(:episodes) do
schema(infer: true)
auto_struct true
end
end
get "/" do
redirect "https://5by5.tv"
end
get "/d/*/*/audio/broadcasts/*/*/:filename" do
filename = params[:filename].gsub(".mp3", "").split("-")
podcast_slug = filename.first
episode_slug = filename.last
podcast_slug = "backtowork" if podcast_slug=="b2w"
podcast_slug = "therubyonrailspodcast" if podcast_slug=="rubyonrails"
podcasts = rom.relations[:podcasts]
podcast = podcasts.where(slug: podcast_slug).one
return 404 if podcast.nil?
episodes = rom.relations[:episodes]
episode = episodes.where(slug: episode_slug, podcast_id: podcast.id).one
return 404 if episode.nil?
if podcast.tracking_prefix_enabled==true && podcast.tracking_prefix_url!=nil && podcast.tracking_prefix_url!=""
base_url = "#{podcast.tracking_prefix_url}/aphid.fireside.fm"
else
base_url = "https://aphid.fireside.fm"
end
url = "#{base_url}/d/1437767933/#{podcast.token}/#{episode.token}.mp3"
redirect url, 301
end
end