This repository was archived by the owner on Mar 18, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy paththorrents.rb
More file actions
190 lines (147 loc) · 3.61 KB
/
thorrents.rb
File metadata and controls
190 lines (147 loc) · 3.61 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
require 'haml'
require 'sass'
require 'sinatra/base'
# require "sinatra/reloader"
require 'json'
require 'net/https'
path = File.expand_path "../", __FILE__
APP_PATH = path
# monkeypatching
class NilClass
def blank?
self.nil?
end
end
class String
def blank?
self.nil? || self == ""
end
end
module KeysSymbolizer
def symbolize_keys!
self.replace(self.symbolize_keys)
end
def symbolize_keys
inject({}) do |options, (key, value)|
options[(key.to_sym rescue key) || key] = value
options
end
end
def recursive_symbolize_keys!
symbolize_keys!
# symbolize each hash in .values
values.each{|h| h.recursive_symbolize_keys! if h.is_a?(Hash) }
# symbolize each hash inside an array in .values
values.select{|v| v.is_a?(Array) }.flatten.each{|h| h.recursive_symbolize_keys! if h.is_a?(Hash) }
self
end
end
class Hash
include KeysSymbolizer
end
class String
def titleize
# self.split("_").map{ |w| w.capitalize }.join(" ").capitalize
self.split("_").join(" ").capitalize
end
def urlize
# js: self.replace(/[^a-z0-9]+/gi, " ").trim().replace(/\s/g, "_").toLowerCase()
self.gsub(/[^a-z0-9]+/i, ' ').strip.gsub(/\s/, "_").downcase
end
end
class NilClass
def titleize
nil
end
def urlize
nil
end
end
# app
require "#{APP_PATH}/models/thorz"
class Thorrents < Sinatra::Base
require "#{APP_PATH}/config/env"
HOST = "localhost:3000"
configure :development do
set :public_folder, "public"
set :static, true
end
set :haml, { :format => :html5 }
set :method_override, true
def not_found(object=nil)
halt 404, "404 - Page Not Found"
end
helpers do
def in_search
/^\/search\/(.+)/
end
def meta_description
request.path =~ in_search ? "Download thorrent with magnet link!" : "Thorrents is a social magnet links search engine. Now you can share music/movies/software with your friends! Remember to buy things if you like them!"
end
def search_title
if request.path =~ in_search
" search: #{CGI.escape $1}"
else
" - Smash old fashioned HTTP downloaders! Thor agrees!"
end
end
end
# hooks
before do
headers "Access-Control-Allow-Origin" => "*"
end
# TODO: use function instead of hook as it will be faster - or switch to roda
# routes
get "/" do
haml :index
end
get "/docs" do
haml :docs
end
get "/recommended_clients" do
haml :recommended_clients
end
def load_results
unless @query.blank?
thor = Thorz.new @query
thor.search
thor.results
end || []
end
def https(url)
uri = URI.parse url
http = Net::HTTP.new uri.host, uri.port
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Get.new uri.request_uri
response = http.request request
end
get '/search/:query.json' do
@query = params[:query]
results = load_results
#content_type :json
content_type "application/javascript"
callback = request.params["callback"]
if callback.blank?
{ results: results }.to_json
else
"#{callback}("+ { results: results }.to_json + ')'
end
end
get '/search*' do |query|
query = query.gsub(/^\//, '')
@query, @result = query.split "/"
@results = load_results
@query.gsub!(/_+/, ' ')
haml :result
end
# TODO: move search route up for speed
# NOTE: this was an interesting idea:
# def load_results_alt
# unless @query.blank?
# thor = Thorz.new @query
# thor.proxied_search if ENV["RACK_ENV"] == "development"
# thor.results
# end || []
# end
end