forked from sfu/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
delicious_diigo.rb
156 lines (147 loc) · 5.17 KB
/
delicious_diigo.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
#
# Copyright (C) 2011 Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#
module DeliciousDiigo
require 'net/http'
require 'net/https'
require 'uri'
def delicious_generate_request(url, method, user_name, password)
rootCA = '/etc/ssl/certs'
url = URI.parse url
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = (url.scheme == 'https')
if File.directory? rootCA
http.ca_path = rootCA
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
http.verify_depth = 5
else
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
request = nil
if method == 'GET'
path = url.path
path += "?" + url.query if url.query
request = Net::HTTP::Get.new(path)
else
request = Net::HTTP::Post.new(url.path)
end
request.basic_auth user_name, password
[http,request]
end
def bookmark_search(service, query)
bookmarks = []
if service.service == 'diigo'
url = "http://api2.diigo.com/bookmarks?users=#{service.service_user_name}&tags=#{query}"
http,request = diigo_generate_request(url, 'GET', service.service_user_name, service.decrypted_password)
response = http.request(request)
case response
when Net::HTTPSuccess
data = ActiveSupport::JSON.decode(response.body)
data.each do |bookmark|
bookmarks << {
:title => bookmark['title'],
:url => bookmark['url'],
:description => bookmark['desc'],
:tags => bookmark['tags'].split(/\s/).join(",")
}
end
else
response.error!
end
elsif service.service == 'delicious'
url = "https://api.del.icio.us/v1/posts/all?tag=#{query}"
http,request = delicious_generate_request(url, 'GET', service.service_user_name, service.decrypted_password)
response = http.request(request)
case response
when Net::HTTPSuccess
require 'libxml'
document = LibXML::XML::Parser.string(response.body).parse
document.find('/posts/post').each do |post|
bookmarks << {
:title => post['description'],
:url => post['href'],
:description => post['description'],
:tags => post['tags']
}
end
else
response.error!
end
end
bookmarks
end
def delicious_get_last_posted(service)
http,request = delicious_generate_request('https://api.del.icio.us/v1/posts/update', 'GET', service.service_user_name, service.decrypted_password)
response = http.request(request)
case response
when Net::HTTPSuccess
require 'libxml'
updated = LibXML::XML::Parser.string(response.body).parse.child["time"]
return Time.parse(updated)
else
response.error!
end
end
def delicious_post_bookmark(service, tag_url, title, desc, tags)
http,request = delicious_generate_request('https://api.del.icio.us/v1/posts/add', 'POST', service.service_user_name, service.decrypted_password)
request.set_form_data({:url => tag_url, :description => desc, :tags => tags.map{|t| t.to_s.gsub(/\s/, "_")}.join(" ")})
response = http.request(request)
case response
when Net::HTTPSuccess
require 'libxml'
code = LibXML::XML::Parser.string(response.body).parse.child["code"]
return code == 'done'
else
response.error!
end
end
def diigo_generate_request(url, method, user_name, password)
url = URI.parse url
http = Net::HTTP.new(url.host, url.port)
request = nil
if method == 'GET'
path = url.path
path += "?" + url.query if url.query
request = Net::HTTP::Get.new(path)
else
request = Net::HTTP::Post.new(url.path)
end
request.basic_auth user_name, password
[http,request]
end
def diigo_get_bookmarks(service, cnt=10)
http,request = diigo_generate_request('http://api2.diigo.com/bookmarks', 'GET', service.service_user_name, service.decrypted_password)
response = http.request(request)
case response
when Net::HTTPSuccess
return ActiveSupport::JSON.decode(response.body)
else
response.error!
end
end
def diigo_post_bookmark(service, url, title, desc, tags)
http,request = diigo_generate_request('http://api2.diigo.com/bookmarks', 'POST', service.service_user_name, service.decrypted_password)
request.set_form_data({:title => title, :url => url, :tags => tags.join(","), :desc => desc})
response = http.request(request)
case response
when Net::HTTPSuccess
return ActiveSupport::JSON.decode(response.body)
else
response.error!
end
end
end