forked from collectiveidea/searchparty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
searchparty.rb
49 lines (40 loc) · 1.15 KB
/
searchparty.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
require 'rubygems'
require 'sinatra'
require 'json'
helpers do
def query
escape_html params[:q] if params[:q]
end
end
require 'mechanize'
class Delicious
URL = "http://delicious.com/search?p=%s&u=%u&context=userposts"
def initialize(username)
@url = URL.sub('%u', username.to_s)
end
def search(query)
page = WWW::Mechanize.new.get @url.sub('%s', query)
user, everyone = %w(context-bookmarklist everyones-bookmarklist).map do |list|
(page.root / "##{list} li.post").map do |item|
{
:title => (item / '.taggedlink').inner_html,
:description => (item / '.description').inner_html,
:url => (item / '.taggedlink').first.attributes['href'],
:user => (item / '.user').inner_html
# :tags => (item / 'a[rel=tag]').map {|tag| tag.inner_html }
}
end
end
{:user => user, :everyone => everyone}
end
end
get '/' do
erb :index
end
get '/search' do
erb :search
end
get '/delicious' do
content_type 'application/json', :charset => 'utf-8'
"#{params[:callback]}(#{Delicious.new(request.cookies['delicious.username']).search(params[:q]).to_json})"
end