Module: FbRuby::Utils
- Defined in:
- lib/FbRuby/utils.rb
Overview
utility
Defined Under Namespace
Classes: Session, ThreadPool
Class Method Summary collapse
- .convert_file_size(size_in_bytes) ⇒ Object
- .cookie_hash_to_str(cookie_hash, chomp = true, strip = true) ⇒ Object
- .create_timeline(nokogiri_obj, request_session, message, file = nil, location = nil, feeling = nil, filter_type = '-1', **kwargs) ⇒ Object
- .parse_cookies_header(value) ⇒ Object
- .randomString(length) ⇒ Object
- .upload_photo(request_session, upload_url, files, data = {}, headers = {}, separator = '|', default_key = 'file', max_number = 3) ⇒ Object
Class Method Details
.convert_file_size(size_in_bytes) ⇒ Object
298 299 300 301 302 303 304 305 306 |
# File 'lib/FbRuby/utils.rb', line 298 def self.convert_file_size(size_in_bytes) units = %w[bytes KB MB GB TB PB EB] return "#{size_in_bytes} #{units[0]}" if size_in_bytes < 1024 exponent = (Math.log(size_in_bytes) / Math.log(1024)).to_i converted_size = size_in_bytes.to_f / 1024 ** exponent converted_size = converted_size.round(2) return "#{converted_size} #{units[exponent]}" end |
.cookie_hash_to_str(cookie_hash, chomp = true, strip = true) ⇒ Object
288 289 290 291 292 293 294 295 296 |
# File 'lib/FbRuby/utils.rb', line 288 def self.(, chomp = true, strip = true) if .instance_of? (Hash) = .map{|key, value| "#{key}=#{value}"}.compact.join(';') .strip! if strip .chomp! if chomp return end end |
.create_timeline(nokogiri_obj, request_session, message, file = nil, location = nil, feeling = nil, filter_type = '-1', **kwargs) ⇒ Object
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 |
# File 'lib/FbRuby/utils.rb', line 308 def self.create_timeline(nokogiri_obj, request_session, , file = nil, location = nil,feeling = nil,filter_type = '-1', **kwargs) host = URI("https://mbasic.facebook.com/") form = nokogiri_obj.at_xpath("//form[starts-with(@action,'/composer/mbasic')]") action = URI.join(host,form['action']) formData = {} form.css("input[type = 'hidden'][name][value]").each{|i| formData[i['name']] = i['value']} # lanjutkan nanti untuk foto unless file.nil? file_data = formData.clone file_data['view_photo'] = "Submit" formFile = request_session.post(action, data = file_data).parse_html.at_xpath("//form[starts-with(@action,'/composer/mbasic')]") dataFile = {"add_photo_done"=>"Submit","filter_type"=>filter_type} formFile.css("input[type = 'hidden']").each{|i| dataFile[i['name']] = i['value']} upload = FbRuby::Utils::upload_photo(request_session, URI.join(host,formFile['action']), file, dataFile) imgIds = [] upload.each do |f| action = URI.join(host,f.parse_html.at_css('form')['action']) f.parse_html.css("input[type = 'hidden'][name][value]").each do |t| if t['name'] == "photo_ids[]" imgIds << t['value'] else formData[t['name']] = t['value'] end end end formData['photo_ids[]'] = imgIds.join(' , ') end unless location.nil? lok_data = formData.clone lok_data['view_location'] = "Submit" formLok = request_session.post(action, data = lok_data).parse_html.at_xpath("//form[starts-with(@action,'/places/selector')]") dataLok = {"query"=>location} formLok.css("input[type = 'hidden'][name][value]").each{|i| dataLok[i['name']] = i['value']} cari = request_session.get(URI.join(host,formLok['action']), params = dataLok).parse_html result = cari.at_xpath("//a[starts-with(@href,'/composer/mbasic') and contains(@href,'at=')] | //a[starts-with(@href,'%2Fcomposer%2Fmbasic') and contains(@href,'at=')]") raise FbRuby::Exceptions::FacebookError.new("Lokasi dengan nama #{location} tidak di temukan:(") if result.nil? formData['at'] = result['href'].match(/at=(\d+)/)[1] end unless feeling.nil? fel_data = formData.clone fel_data['view_minutiae'] = "Submit" felUrl = request_session.post(action, data = fel_data).parse_html.at_xpath("//a[starts-with(@href,'/composer/mbasic') and contains(@href,'ogaction')]") raise FbRuby::Exceptions::FacebookError.new("Tidak dapat menembahkan feeling:(") if felUrl.nil? felForm = request_session.get(URI.join(host,felUrl['href'])).parse_html.at_xpath("//form[starts-with(@action,'/composer/mbasic')]") felData = {"mnt_query"=>feeling} felForm.css("input[type = 'hidden'][name][value]").each{|i| felData[i['name']] = i['value']} cari = request_session.get(URI.join(host,felForm['action']), params = felData).parse_html result = cari.at_xpath("//a[starts-with(@href,'/composer/mbasic') and contains(@href,'ogaction')]") raise FbRuby::Exceptions::FacebookError.new("Feeling dengan nama #{feeling} tidak di temukan!") if result.nil? formData['ogaction'] = result['href'].match(/ogaction=(\d+)/)[1] formData['ogphrase'] = feeling end formData['view_post'] = "Submit" formData['xc_message'] = formData.update(kwargs) begin posting = request_session.post(action, data = formData) return posting.ok? rescue FbRuby::Exceptions::PageNotFound return true end end |
.parse_cookies_header(value) ⇒ Object
278 279 280 281 282 283 284 285 286 |
# File 'lib/FbRuby/utils.rb', line 278 def self.(value) return {} unless value value.split(/; */n).each_with_object({}) do |, | next if .empty? key, value = .split('=', 2) [key] = (unescape(value) rescue value) unless .key?(key) end end |
.randomString(length) ⇒ Object
273 274 275 276 |
# File 'lib/FbRuby/utils.rb', line 273 def self.randomString(length) chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' return Array.new(length) { chars[rand(chars.size)] }.join end |
.upload_photo(request_session, upload_url, files, data = {}, headers = {}, separator = '|', default_key = 'file', max_number = 3) ⇒ Object
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 |
# File 'lib/FbRuby/utils.rb', line 377 def self.upload_photo(request_session, upload_url, files, data = {}, headers = {}, separator = '|', default_key = 'file', max_number = 3) max_size = 4194304 # Maksimal ukuran foto (4MB) support_file = ['.jpg','.png','.webp','.gif','.tiff','.heif','.jpeg'] photo = [] reqList = [] unless files.kind_of?(Hash) number = 0 files = files.split(separator) if files.kind_of? (String) files.each do |f| number = 0 if number >= max_number number += 1 photo << {"#{default_key}#{number}"=>f} end else files.each{|k,v| photo << {k=>v}} end photo.each_slice(max_number.to_i) do |img| mydata = data.clone img.each do |khaneysia| key = khaneysia.keys.first path = khaneysia.values.first ext = File.extname(path) raise FbRuby::Exceptions::FacebookError.new("Ukuran file #{File.basename(path)} terlalu besar sehingga file tersebut tidak bisa di upload. File harus berukuran kurang dari #{FbRuby::Utils::convert_file_size(max_size)} :)") if File.size(path) > max_size raise FbRuby::Exceptions::FacebookError.new("Hanya bisa mengupload file dengan extensi #{support_file.join(', ')}, tidak bisa mengupload file dengan extensi #{ext}") unless support_file.include? (ext) mydata.update({key=>File.new(path,"rb")}) end reqList << request_session.post(upload_url,mydata,headers) end return reqList end |