forked from themgt/extractomatic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextractomatic.rb
68 lines (57 loc) · 1.55 KB
/
extractomatic.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
mime_type :json, "application/json"
set :haml, {:format => :html5 }
class UnknownExtractor < Exception; end
class NoUrlPresent < Exception; end
get '/' do
haml :index
end
get '/extract' do
content_type :json
begin
url_string = params[:url]
mode = params[:mode] || "default"
extractors = {
'article' => ArticleExtractor,
'article_sentences' => ArticleSentencesExtractor,
'min_k_words' => KeepEverythingWithMinKWordsExtractor,
'default' => DefaultExtractor,
'largest' => LargestContentExtractor,
'num_words' => NumWordsRulesExtractor
}
raise NoUrlPresent unless url_string && url_string.length > 0
raise UnknownExtractor unless extractors.keys.include?(mode)
url = URL.new(url_string)
content = extractors[mode].instance.getText(url)
rescue java.net.MalformedURLException
return_error("Malformed URL", 100)
rescue java.lang.RuntimeException
return_error("Could not connect", 101)
rescue UnknownExtractor
return_error("Unknown extractor mode", 102)
rescue NoUrlPresent
return_error("No URL requested", 103)
rescue
return_error("Unknown error", 99)
else
return_success(content, url_string)
end
end
private
def return_error(message, code)
{
:error => {
:message => message,
:code => code
},
:status => "error"
}.to_json
end
def return_success(content, url)
{
:response => {
:content => content,
:url => url
},
:status => "success"
}.to_json
end