-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrailerts.rb
executable file
·157 lines (117 loc) · 3.81 KB
/
trailerts.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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
110
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env ruby
require 'rubygems'
require 'bundler'
require 'json'
require 'sinatra'
require 'httparty'
require 'mysql2'
require 'inifile'
require 'themoviedb'
require 'redis'
set :session_secret, ENV["SESSION_KEY"] || 'too secret'
enable :sessions
not_found do
'This is nowhere to be found.'
end
def get_config
IniFile.load('trailerts.ini')
end
def get_if_cached(cache_key, proc, cache_timeout = 43200)
redis = Redis.new
cached_movies = redis[cache_key]
if cached_movies
movies = JSON.parse(cached_movies)
else
@tmdb = Tmdb::Api.key(@config['themoviedb']['key'])
movies = proc.call
redis[cache_key] = movies.to_json
redis.expire(cache_key, cache_timeout) # half a day
end
movies
end
def get_if_cached_genres
cached_genres = get_if_cached('trailerts_genres', Proc.new {
genres = Tmdb::Genre.list
genres['genres'].each do |genre|
genre['slug'] = genre['name'].downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
end
genres
})
end
# Usage: partial :foo
helpers do
def partial(page, options={})
erb page, options.merge!(:layout => false)
end
end
get '/' do
@slug = 'upcoming'
@config = get_config
@genres = get_if_cached_genres
erb :index
end
get '/now_playing' do
@slug = 'now_playing'
@config = get_config
@genres = get_if_cached_genres
erb :index
end
get '/update/genres' do
@config = get_config
secret_configured_and_passed = params.has_key?('secret') && @config.has_section?('trailerts') && @config['trailerts'].has_key?('secret')
secret_correct = (@config['trailerts']['secret'] == params['secret'])
raise Sinatra::NotFound, 'Forbidden' unless params.has_key?('secret')
cached_genres = get_if_cached_genres
cached_genres.to_json
end
get '/api/now_playing' do
response.headers['Content-type'] = "application/json"
@config = get_config
movies = get_if_cached('trailerts_now_playing', Proc.new { Tmdb::Movie.now_playing })
movie = movies.sample
movie.to_json
end
get '/api/upcoming' do
response.headers['Content-type'] = "application/json"
@config = get_config
movies = get_if_cached('trailerts_upcoming', Proc.new { Tmdb::Movie.upcoming })
movie = movies.sample
movie.to_json
end
get '/api/discover' do
response.headers['Content-type'] = "application/json"
@config = get_config
@movie_params = {}
['year', 'language', 'with_companies', 'vote_count.gte', 'vote_average.gte', 'with_genres'].each do |param_name|
@movie_params[param_name.to_sym] = @params[param_name].to_i if @params.has_key?(param_name)
end
redis_key = 'trailerts_discover_%s' % @movie_params.hash.to_s
movies = get_if_cached(redis_key, Proc.new { Tmdb::Movie.discover @movie_params })
movie = movies.sample
movie.to_json
end
get '/api/:slug' do
response.headers['Content-type'] = "application/json"
@config = get_config
@genres = get_if_cached_genres
@matched_genres = @genres['genres'].select { |genre| genre['slug'] == params[:slug] }
raise Sinatra::NotFound, 'Channel does not exist yet.' unless @matched_genres.length == 1
@movie_params = {
'with_genres' => @matched_genres[0]['id']
}
['year', 'language', 'with_companies', 'vote_count.gte', 'vote_average.gte', 'release_date.gte', 'release_date.lte'].each do |param_name|
@movie_params[param_name.to_sym] = @params[param_name].to_s if @params.has_key?(param_name)
end
redis_key = 'trailerts_discover_%s' % @movie_params.hash.to_s
movies = get_if_cached(redis_key, Proc.new { Tmdb::Movie.discover @movie_params })
movie = movies.sample
movie.to_json
end
get '/:slug' do
@slug = params[:slug]
@config = get_config
@genres = get_if_cached_genres
@matched_genres = @genres['genres'].select { |genre| genre['slug'] == params[:slug] }
raise Sinatra::NotFound, 'Channel does not exist yet.' unless @matched_genres.length == 1
erb :index
end