-
Notifications
You must be signed in to change notification settings - Fork 1
/
photoIso.rb
298 lines (264 loc) · 8.51 KB
/
photoIso.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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# encoding: utf-8
require './plurk.rb'
require './setting.rb'
require './log.rb'
require 'time'
require 'net/http'
require 'open-uri'
require 'exifr'
class PhotoIso
REGEXP_CHANNEL = /^CometChannel\.scriptCallback\((.*)\);/
attr_reader :setting
def initialize(m)
puts m + " loaded..."
@setting = Setting.new
@plurkApi = Plurk.new(@setting.api_key, @setting.api_secret)
@plurkApi.authorize(@setting.token_key, @setting.token_secret)
@me = @plurkApi.post '/APP/Users/me'
puts "My id is #{@me["id"]}"
end
public
def listenChannel
getChannel while @channelUri.nil?
getChannel if @channelOffset == -3
params = { :channel => @channelName, :offset => @channelOffset }
params = params.map { |k,v| "#{k}=#{v}" }.join('&')
uri = URI.parse(@channelUri + "?" + params)
http = Net::HTTP.new(uri.host, uri.port)
http.read_timeout = 170
retryGetting = 0
begin
res = http.start do |h|
h.get(uri.path+"?"+params)
end
rescue
retryGetting += 1
sleep 3
if retryGetting == 5
getChannel
return false
end
retry
end
res = REGEXP_CHANNEL.match res.body
json = JSON.parse res[1]
readed = []
@channelOffset = json["new_offset"].to_i
return if json["data"].nil?
json["data"].each do |plurk|
case plurk["type"]
when "new_response"
when "new_plurk"
responseNewPlurk plurk
when "friendship_request"
p "friend request"
end
end
end
def addPlurk(content, options = {})
options = { qualifier: ':', lang: 'tr_ch' }.merge options
begin
json = @plurkApi.post '/APP/Timeline/plurkAdd', options.merge(content: content)
log %(#{Time.now.to_s} [EVENT] add Plurk: #{content})
rescue
str = %(#{Time.now.to_s} [ERROR] Adding plurk has error: #{$!.to_s})
if json
str << "(#{json["error_text"]})" if json.key? "error_text"
end
log str
sleep 120
retry
end
return json
end
def addPrivatePlurk(content, user_id, options = {})
options = { qualifier: ':', lang: 'tr_ch' }.merge options
begin
json = @plurkApi.post '/APP/Timeline/plurkAdd', options.merge(content: content, limited_to: [[user_id]])
log %(#{Time.now.to_s} [EVENT] add PrivatePlurk: #{content})
rescue
str = %(#{Time.now.to_s} [ERROR] Adding private plurk has error: #{$!.to_s})
if json
str << "(#{json["error_text"]})" if json.key? "error_text"
end
log str
sleep 120
retry
end
end
def responsePlurk(plurk_id, content, options = {})
if content != ""
options = { qualifier: ':', lang: 'tr_ch' }.merge options
begin
log %(#{Time.now.to_s} [EVENT] Responsing plurk: #{content})
res = @plurkApi.post '/APP/Responses/responseAdd', options.merge(plurk_id: plurk_id, content: content)
log %(#{Time.now.to_s} [EVENT] Responsing plurk response: #{res})
rescue
log %(#{Time.now.to_s} [ERROR] Responsing plurk has error: #{$!.to_s})
end
end
end
def checkUnreadPlurk
json = @plurkApi.post '/APP/Timeline/getUnreadPlurks', limit: 20
return if json["plurks"].nil?
json["plurks"].each do |plurk|
responseNewPlurk plurk
end
end
def addAllAsFriends
json = @plurkApi.post '/APP/Alerts/addAllAsFriends'
end
def getUnreadPlurk
begin
json = @plurkApi.post '/APP/Timeline/getUnreadPlurks'
log %(#{Time.now.to_s} [EVENT] get UnreadPlurk: #{json})
rescue
log %(#{Time.now.to_s} [ERROR] Getting unread plurks has error: #{$!.to_s})
sleep 5
retry
end
return json
end
private
def responsed?(plurk_id)
begin
json = @plurkApi.post '/APP/Responses/get', plurk_id: plurk_id
rescue
log %(#{Time.now.to_s} [ERROR] Getting response has error: #{$!.to_s})
sleep 5
retry
end
json["responses"].each do |res|
return false if res["user_id"] == @me["id"]
end
true
end
def responseNewPlurk(plurk)
return unless responsed? plurk["plurk_id"]
resp = []
case plurk["content_raw"]
when /http[s]*:\/\/emos.plurk.com[\S]*.((jpg)|(jpeg))/
imageUrl = $&.to_s
log %(#{Time.now.to_s} [EVENT] New image: #{imageUrl} form #{plurk["plurk_id"].to_s})
log %(\tBut it is Emoticons)
return
when /http[s]*:\/\/[\S]*.((jpg)|(jpeg))/
imageUrl = $&.to_s
log %(#{Time.now.to_s} [EVENT] New image: #{imageUrl} form #{plurk["plurk_id"].to_s})
#load the image file
begin
open('image.jpg', 'wb') do |file|
if imageUrl =~ /https:\/\/[\S]*/
file << open(imageUrl,{ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE}).read
else
file << open(imageUrl,).read
end
end
log %(#{Time.now.to_s} [EVENT] Load image: #{imageUrl})
rescue
log %(#{Time.now.to_s} [ERROR] Load image #{imageUrl} has error: #{$!.to_s})
return
end
#read the EXIF info and put it in the response string
readEXIF =~ /([\S\s]+)##([\S\s]*)|(你的照片沒有EXIF資訊歐 OAO[\S\s]*)/
res1 = $1
res2 = $2
res3 = $3
if res1 != nil
resp << res1
p resp
responsePlurk(plurk["plurk_id"], resp * " ")
end
if res2 != nil
resp = []
resp << res2
p resp
responsePlurk(plurk["plurk_id"], resp * " ")
end
if res3 != nil
resp = []
resp << res3
p resp
responsePlurk(plurk["plurk_id"], resp * " ")
end
end
return if resp.empty?
#responsePlurk(plurk["plurk_id"], resp * " ")
@plurkApi.post '/APP/Timeline/mutePlurks', ids: [[plurk["plurk_id"]]]
end
def getChannel
begin
resp = @plurkApi.post("/APP/Realtime/getUserChannel")
rescue Timeout::Error
sleep 2 # let it take a rest
retry
end
if resp["comet_server"].nil?
log %(#{Time.now.to_s} [ERROR] Failed to get channel.)
return false
end
@channelUri = resp["comet_server"]
@channelName = resp["channel_name"]
@channelOffset = -1
log %(#{Time.now.to_s} [EVENT] Get channel uri: #{@channelUri})
log %(#{Time.now.to_s} [EVENT] Get channel name: #{@channelName})
return true
end
def readEXIF
begin
@setting.read
s = ""
a = EXIFR::JPEG.new('./image.jpg')
if a.exif?
if @setting.time == "true"
s += "拍攝日期:#{a.date_time.to_s}\n" unless a.date_time.nil?
end
if @setting.size == "true"
s += "照片大小:#{a.width} * #{a.height}\n" unless a.width.nil? or a.height.nil?
end
if @setting.exposure_time == "true"
s += "快門時間:#{( a.exif.exposure_time.to_r.rationalize(10**-8))}\n" unless a.exif.exposure_time.nil?
end
if @setting.f_number == "true"
s += "光圈大小:F #{a.exif.f_number.to_s}\n" unless a.exif.f_number.nil?
end
if @setting.focal_length == "true"
s += "焦距:#{a.exif.focal_length.to_f.to_s}mm\n" unless a.exif.focal_length.nil?
end
if @setting.ISO == "true"
s += "ISO值:#{a.exif.iso_speed_ratings.to_s}\n" unless a.exif.iso_speed_ratings.nil?
end
if @setting.white_balance == "true"
s += "白平衡:#{a.exif.white_balance.to_s}\n" unless a.exif.white_balance.nil?
end
if @setting.model == "true"
s += "設備:" unless a.exif.make.nil? and a.exif.model.nil?
s += "#{a.exif.make.to_s} " unless a.exif.make.nil?
s += "#{a.exif.model.to_s}" unless a.exif.model.nil?
end
#s += "##"
if @setting.GPS == "true"
s += "拍攝地點: " unless a.gps_longitude.nil? and a.gps_latitude.nil?
s += "#{a.gps_longitude_ref.to_s} #{a.gps_longitude.to_f.round(4)} " unless a.gps_longitude.nil?
s += "#{a.gps_latitude_ref.to_s} #{a.gps_latitude.to_f.round(4)} \n" unless a.gps_latitude.nil?
#s += "https://www.google.com/maps/place/#{a.gps_longitude_ref.to_s}#{a.gps_longitude.to_f}+#{a.gps_latitude_ref.to_s}#{a.gps_latitude.to_f} \n" unless a.gps_longitude.nil? and a.gps_latitude.nil?
s += "海拔#{a.gps_altitude.to_f.round(3)}公尺\n" unless a.gps_altitude.nil?
end
s += "##"
if @setting.GPS == "true"
s += "https://www.google.com/maps/place/#{a.gps_longitude_ref.to_s}#{a.gps_longitude.to_f}+#{a.gps_latitude_ref.to_s}#{a.gps_latitude.to_f} \n" unless a.gps_longitude.nil? and a.gps_latitude.nil?
end
else
if @setting.size == "true"
s += "你的照片沒有EXIF資訊歐 OAO\n"
s += "照片大小:#{a.width} * #{a.height}\n" unless a.width.nil? or a.height.nil?
end
end
return s
rescue
log %(#{Time.now.to_s} [ERROR] Make response string has error: #{$!.to_s})
s = "喔哦 出了一些問題"
return s
end
end
end