-
Notifications
You must be signed in to change notification settings - Fork 3
/
ttt.cgi
executable file
·238 lines (220 loc) · 6.67 KB
/
ttt.cgi
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#!/usr/bin/env ruby
require 'rubygems'
require 'cgi'
require 'net/http'
require 'uri'
require 'hpricot'
require 'time'
require 'json'
require 'fileutils'
require 'webrick'
load './credentials.rb'
class Tumblr
def initialize(email, password, host = nil)
Net::HTTP.version_1_2
@email = email
@password = password
refresh_cookie()
end
def refresh_cookie(force = false)
@cookie_data = nil
while @cookie_data == nil
cookie_data_path = "./.cookie_#{@email}"
if force
FileUtils.remove(cookie_data_path)
end
cookie_data = nil
if File.exists?(cookie_data_path)
cookie_data = Marshal.load(File.open(cookie_data_path))
unless cookie_data.is_a?(Hash) || (cookie_data[:expires] && cookie_data[:expires] < Time.now)
cookie_data = nil
end
end
unless cookie_data
res = call_api(:post, "www.tumblr.com", "/login",
{:email => @email, :password => @password})
cookies = res["set-cookie"].split("httponly,")
raise RuntimeError.new("login failed") if cookies.size < 2
cookies = cookies.select{|str| ! (str =~ /tmgioct/)}
cookies = cookies.map{|str| str.strip.split(";").map(&:strip)}
values = Hash.new
expires = Time.parse(cookies[0][1].split("=")[1])
cookies.each do |cookie_entry|
k,v = cookie_entry[0].split("=")
values[k] = v
end
cookie_data = Hash.new
cookie_data[:expires] = expires
cookie_data[:values] = values
File.open(cookie_data_path, "w") do |file|
file.print(Marshal.dump(cookie_data))
end
end
@cookie_data = cookie_data
end
end
def dashboard(options = {})
res = call_api(:get, "www.tumblr.com", "/iphone", options)
if res
convert_dashboard_page(res.body).to_json
else
refresh_cookie(true)
end
end
def convert_dashboard_page(html_str)
doc = Hpricot(html_str)
posts = doc.search("//ol[@id='posts']/li").select{|post| post[:id] =~ /post_(\d+)/}
post_controls = doc.search("//ol[@id='posts']/li").select{|post| post[:id] =~ /post_controls_(\d+)/}
posts = posts.zip(post_controls).map{|args|
post = args[0]
post_control = args[1]
ret = Hash.new
post[:id] =~ /post_(\d+)/
ret[:id] = $~[1]
reblog_path = post_control.search('button').select{|btn|
btn[:onclick] =~ /reblog/
}[0]
if reblog_path
reblog_path = reblog_path[:onclick].gsub(/\Alocation\.href='([^']+)';/, "\\1")
reblog_path =~ /\/reblog\/\d+\/([^?]+)/
ret[:reblog_key] = $~[1]
end
user = post.search("div.meta > div > a")[0].innerHTML
ret[:user] = user
ret[:permalink] = post.search("div.meta > div > a")[0][:href]
post = post.search(".post")[0]
case post[:class].split[1]
when /\Aphoto_post\Z/
ret[:type] = "photo"
photo_url_small = post.search("img")[0][:src]
photo_url_large = if post.search("a")[0][:onclick] =~ URI.regexp
$~[0].gsub(/["']+$/, "")
else
photo_url_small
end
ret[:photo_url] = photo_url_small
ret[:photo_url_small] = photo_url_small
ret[:photo_url_large] = photo_url_large
if post.search(".caption").empty?
caption = ""
else
caption = post.search(".caption")[0].innerHTML
end
ret[:content] = "<div class=\"photo-caption\">#{caption}</div>"
when /\Aquote_post\Z/
ret[:type] = "quote"
text = post.search("div.quote").innerHTML
source = post.search(".source").innerHTML
ret[:content] = <<EOS
<div class="quote-text">#{text}</div>
<div class="quote-source">#{source}</div>
EOS
when /\Atext_post\Z/
ret[:type] = "text"
ret[:content] = post.innerHTML
when /\Avideo_post\Z/
ret[:type] = "video"
ret[:content] = post.innerHTML
when /\Achat_post\Z/
ret[:type] = "chat"
ret[:content] = post.innerHTML
when /\Alink_post\Z/
ret[:type] = "link"
ret[:content] = post.innerHTML
else
ret = nil
end
ret
}.compact
end
def reblog(id, reblog_key)
res = call_api(:post, "www.tumblr.com", "/api/reblog",
{"email" => @email, "password" => @password,
"post-id" => id, "reblog-key" => reblog_key})
if res
res.body
else
refresh_cookie(true)
nil
end
end
def call_api(method, host, path, params = nil)
retry_num = 5
while retry_num > 0
req = nil
if method == :get
req = Net::HTTP::Get.new(path)
elsif method == :post
req = Net::HTTP::Post.new(path)
else
return nil
end
if params
if method == :get
query_string = params.map{|k,v|
URI.encode(k.to_s) + "=" + URI.encode(v.to_s)
}.join("&")
req = Net::HTTP::Get.new(path + "?" + query_string)
elsif method == :post
req.set_form_data(params)
req["content-type"] = "application/x-www-form-urlencoded"
end
end
if @cookie_data
req["cookie"] = @cookie_data[:values].map{|k,v| "#{k}=#{v}"}.join("; ")
end
res = Net::HTTP.start(host, 80) {|http|
response = http.request(req)
response
}
if res.code == "200" || res.code == "201"
return res
end
retry_num -= 1
end
nil
end
end
def main(argv)
res = case $cgi.params["method"][0]
when /\Adashboard\Z/
tumblr = Tumblr.new($email, $password)
params = Hash.new
params[:offset] = $cgi.params["offset"][0] if $cgi.params["offset"]
params[:page] = $cgi.params["page"][0] if $cgi.params["page"]
tumblr.dashboard(params)
when /\Areblog\Z/
if $cgi.params["reblog_key"] && $cgi.params["id"]
reblog_key = $cgi.params["reblog_key"][0]
id = $cgi.params["id"][0]
_pwd = Dir.pwd
Process.fork{
WEBrick::Daemon.start
Dir.chdir(_pwd)
tumblr = Tumblr.new($email, $password)
50.times do
begin
if tumblr.reblog(id, reblog_key)
break
end
rescue Timeout::Error => err
end
end
}
"true"
else
nil
end
when /\Asleep\Z/
sleep(10)
"sleep"
end
res ||= "null"
$cgi.out("application/json") do
res
end
end
if __FILE__ == $0
$cgi = CGI.new
main(ARGV.dup)
end