Class: FbRuby::Facebook

Inherits:
Login::Cookie_Login show all
Defined in:
lib/FbRuby/facebook.rb

Overview

class Facebook di gunakan agar penggunaan library jadi lebih mudah dan cepat

Instance Attribute Summary

Attributes inherited from Login::Cookie_Login

#cookies, #free_facebook, #headers, #req, #res, #sessions, #url

Instance Method Summary collapse

Methods inherited from Login::Cookie_Login

#get_cookie_dict, #get_cookie_hash, #get_cookie_str, #get_token, #logout

Constructor Details

#initialize(cookies, free_facebook: false, headers: {}, save_device: true) ⇒ Facebook

Inisialisasi Object Facebook

Parameters:

  • cookies (String)

    Cookie akun facebook dalam format string

  • free_facebook (Boolean) (defaults to: false)

    Gunakan web free facebook ketika login

  • headers (Hash) (defaults to: {})

    Headers Yang akan di gunakan untuk requests sessions

  • save_device (Boolean) (defaults to: true)

    Simpan Informasi Login



23
24
25
# File 'lib/FbRuby/facebook.rb', line 23

def initialize(cookies, free_facebook: false, headers: {}, save_device: true)
  super
end

Instance Method Details

#accept_friends_request(target) ⇒ Object

Terima Permintaan Pertemanan

Parameters:

  • target (String)

    Username / id akun target



213
214
215
# File 'lib/FbRuby/facebook.rb', line 213

def accept_friends_request(target)
  return get_profile(target).accept_friends_requests()
end

#add_friends(target) ⇒ Object

Tambahkan Teman

Parameters:

  • target (String)

    Username / id akun target



199
200
201
# File 'lib/FbRuby/facebook.rb', line 199

def add_friends(target)
  return get_profile(target).add_friends()
end

#cancel_friends_requests(target) ⇒ Object

Batalkan permintaan pertemanan

Parameters:

  • target (String)

    Username / id akun target



206
207
208
# File 'lib/FbRuby/facebook.rb', line 206

def cancel_friends_requests(target)
  return get_profile(target).cancel_friends_requests()
end

#create_new_group(name, privacy) ⇒ Object

Buat Grup Baru

Parameters:

  • name (String)

    Nama Group

  • privacy (String)

    Privasi Grup “public,private”

Raises:



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
# File 'lib/FbRuby/facebook.rb', line 162

def create_new_group(name, privacy)
  privacy.downcase!
  priv = ['public','private']
  raise FbRuby::Exceptions::FacebookError.new("Invalid Privacy!!!") if !priv.include? (privacy)
  html = @sessions.get(URI.join(@url, 'groups/create')).parse_html
  formName = html.at_css("form[action^='/groups/create']")
  formData = {"group_name"=>name,"verify"=>"Submit"}
  formName.css("input[type = 'hidden']").each{|i| formData[i['name']] = i['value']}
  namaSubmit = @sessions.get(URI.join(@url, formName['action']), formData).parse_html

  if !namaSubmit.at_css("input[name='group_name'][type = 'text']").nil?
    errMsg = namaSubmit.at_css("form[action^='/groups/create'] span")
    errMsg = (errMsg.nil? ? "Terjadi Kesalahan :(" : errMsg.text)
    raise FbRuby::Exceptions::FacebookError.new(errMsg)
  end
    
  formPrivacy = namaSubmit.at_css("form[action^='/groups/create/privacy']")
  formPrivacyData = {"verify"=>"Submit"}
  privasi = formPrivacy.css("input[type = 'radio']")
  formPrivacy.css("input[type = 'hidden']").each{|i| formPrivacyData[i['name']] = i['value']}
  privacyIndex = priv.index(privacy)
  formPrivacyData[privasi[privacyIndex]['name']] = privasi[privacyIndex]['value']
  create = @sessions.post(URI.join(@url, formPrivacy['action']), data = formPrivacyData).parse_html
  gpUid = create.at_css("a[href^='/groups/'][href*='view=members']")

  if gpUid.nil? and !create.at_css("input[name='group_name'][type = 'text']").nil?
    errMsg = create.at_css("form[action^='/groups/create'] span")
    errMsg = (errMsg.nil? ? "Terjadi Kesalahan :(" : errMsg.text)
    raise FbRuby::Exceptions::FacebookError.new(errMsg)
  end

  return get_groups((gpUid['href'].match(/\/groups\/(\d+)/)[1]).strip)
end

#create_timeline_groups(target, message, file: nil, location: nil, feeling: nil, filter_type: '-1', **kwargs) ⇒ Object

Buat Postingan di Grup

Ada 3 tipe filter

-1 Tanpa Filter
2 Hitam Putih
3 Retro

Parameters:

  • target (String)

    Id / Username akun facebook

  • message (String)

    Caption dari postingan

  • file (String) (defaults to: nil)

    Path dari foto (hanya bisa posting 1 foto)

  • location (String) (defaults to: nil)

    Nama Kota / Nama tempat

  • feeling (String) (defaults to: nil)

    Nama Perasaan

  • filter_type (String) (defaults to: '-1')

    Tipe Filter



154
155
156
# File 'lib/FbRuby/facebook.rb', line 154

def create_timeline_groups(target,message, file: nil, location: nil,feeling: nil,filter_type: '-1', **kwargs)
  return get_groups(target).create_timeline(message, file:file, location:location,feeling:feeling,filter_type:filter_type, **kwargs)
end

#create_timeline_user(target, message, file: nil, location: nil, feeling: nil, filter_type: '-1', **kwargs) ⇒ Object

Buat Postingan di akun pengguna

Ada 3 tipe filter

-1 Tanpa Filter
2 Hitam Putih
3 Retro

Parameters:

  • target (String)

    Id / Username akun facebook

  • message (String)

    Caption dari postingan

  • file (String) (defaults to: nil)

    Path dari foto (hanya bisa posting 1 foto)

  • location (String) (defaults to: nil)

    Nama Kota / Nama tempat

  • feeling (String) (defaults to: nil)

    Nama Perasaan

  • filter_type (String) (defaults to: '-1')

    Tipe Filter



138
139
140
# File 'lib/FbRuby/facebook.rb', line 138

def create_timeline_user(target,message, file: nil, location: nil,feeling: nil,filter_type: '-1', **kwargs)
  return get_profile(target).create_timeline(message, file:file, location:location,feeling:feeling,filter_type:filter_type, **kwargs)
end

#delete_friends_requests(target) ⇒ Object

Hapus Permintaan Pertemanan

Parameters:

  • target (String)

    Username / id akun target



220
221
222
# File 'lib/FbRuby/facebook.rb', line 220

def delete_friends_requests(target)
  return get_profile(target).delete_friends_requests()
end

#get_albums(target, limit) ⇒ Object

Dapatkan Album Dari Akun Pengguna

Parameters:

  • target (String)

    Id / username akun facebook

  • limit (Integer)

    Jumblah maksimal album yang akan di ambil



85
86
87
# File 'lib/FbRuby/facebook.rb', line 85

def get_albums(target, limit)
  return get_profile(target).get_albums(limit)
end

#get_friends(target, limit, return_hash = true) ⇒ Object

Dapatkan daftar teman

Parameters:

  • target (String)

    Username / id akun target

  • return_hash (Boolean) (defaults to: true)

    Kembalikan Hash jika true



235
236
237
# File 'lib/FbRuby/facebook.rb', line 235

def get_friends(target, limit, return_hash = true)
  return get_profile(target).get_friends(limit, return_hash)
end

#get_friends_requests(limit, return_hash = true) ⇒ Object

Dapatkan daftar permintaan pertemanan

Parameters:

  • limit (Integer)

    Batas maksimal

  • return_hash (Boolean) (defaults to: true)

    Kembalikan Hash jika true



243
244
245
# File 'lib/FbRuby/facebook.rb', line 243

def get_friends_requests(limit, return_hash = true)
  return getFriendsCenter("/friends/center/requests","/friends/center/requests",limit, return_hash)
end

#get_friends_requests_send(limit, return_hash = true) ⇒ Object

Dapatkan daftar permintaan pertemanan terkirim

Parameters:

  • limit (Integer)

    Batas maksimal

  • return_hash (Boolean) (defaults to: true)

    Kembalikan Hash jika true



251
252
253
# File 'lib/FbRuby/facebook.rb', line 251

def get_friends_requests_send(limit, return_hash = true)
  return getFriendsCenter("/friends/center/requests/outgoing","/friends/center/requests/outgoing",limit, return_hash)
end

#get_groups(group_id) ⇒ Groups

Parsing Groups

Parameters:

  • group_id (String)

    Id group

Returns:



53
54
55
# File 'lib/FbRuby/facebook.rb', line 53

def get_groups(group_id)
  return FbRuby::Groups.new(group_id: group_id, requests_sessions: @sessions)
end

#get_home_posts(limit) ⇒ Object

Dapatkan postingan dari beranda

Parameters:

  • limit (Integer)

    Jumblah maksimal Postingan yang akan di ambil



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/FbRuby/facebook.rb', line 106

def get_home_posts(limit)
  post = []
  html = @sessions.get(URI.join(@url, "home.php")).parse_html

  while post.length < limit
    url = html.css("a[href*='#footer_action_list']")

    for i in url
      break if post.length >= limit
      post << post_parser((i['href'].include?('http') ? i['href'] : URI.join(@url, i['href']).to_s))
    end

    next_url = html.at_css("a[href^='/stories.php?aftercursorr']")
    break if next_url.nil? or post.length >= limit
    html = @sessions.get(URI.join(@url, next_url['href'])).parse_html
  end

  return post
end

#get_mygroups(limit = 5) ⇒ Object

Dapat daftar grup saya

Parameters:

  • limit (Integer) (defaults to: 5)

    Batas maksimal grup yang di ambil



343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/FbRuby/facebook.rb', line 343

def get_mygroups(limit = 5)
  group = []
  html = @sessions.get(URI.join(@url, "/groups/?seemore")).parse_html

  while group.length < limit
    for url in html.css("a[href^='http'][href*='/groups'], a[href^='/groups']")
      break if group.length >= limit
      uid = url['href'].match(/\/groups\/(\d+)/)
      next if uid.nil?
      group << get_groups(uid[1])
    end

    next_url = html.at_css("a[href^='/groups?seemore']")
    break if group.length >= limit or next_url.nil?
    html = @sessions.get(URI.join(@url, next_url['href'])).parse_html
  end

  return group
end

#get_notifications(limit) ⇒ Object

Dapatkan notifikasi terbaru

Parameters:

  • limit (Integer)

    Batas maksimal notifikasi yang di ambil



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/FbRuby/facebook.rb', line 316

def get_notifications(limit)
  notif = []
  html = @sessions.get(URI.join(@url, "/notifications.php")).parse_html

  while notif.length < limit
    for i in html.css("a[href^='/a/notifications.php']")
      break if notif.length >= limit
      next if !i.at_css("img").nil?
      notifData = {"message"=>nil,"time"=>nil,"redirect_url"=>URI.join(@url, i['href']).to_s}
      msg = i.at_css("div span")
      time = i.at_css("div abbr")
      notifData["message"] = msg.text if !msg.nil?
      notifData["time"] = time.text if !time.nil?
      notif << notifData
    end

    next_url = html.at_css("a[href^='/notifications.php?more&']")
    break if notif.length >= limit or next_url.nil?
    html = @sessions.get(URI.join(@url, next_url['href'])).parse_html
  end

  return notif
end

#get_people_by_search(keyword, limit = 5) ⇒ Object

Cari Akun di pencarian

Parameters:

  • keyword (String)

    Keyword Pencarian

  • limit (Integer) (defaults to: 5)

    Limit Pencarian



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/FbRuby/facebook.rb', line 275

def get_people_by_search(keyword, limit = 5)
  user = []
  html = @sessions.get(URI.join(@url, "/search/people?q=#{ERB::Util.url_encode(keyword)}")).parse_html

  while user.length < limit
    for usr in html.css("img[alt*='profile picture']")
      next if usr.parent['href'].nil?
      break if user.length >= limit
      findUid = usr.parent['href'].match(/\/(?:profile\.php\?id=(\d+)|([a-zA-Z0-9_.-]+)\?)/)
      uid = (findUid[1].nil? ? findUid[2] : findUid[1])
      user << FbRuby::User.new(username: uid, requests_sessions: @sessions)
    end

    next_url = html.at_css("a[href*='/search/people']")
    
    break if next_url.nil? or user.length >= limit
    html = @sessions.get(next_url['href']).parse_html
  end

  return user
end

#get_photo(target, limit, albums_url = nil) ⇒ Object

Dapatkan Foto Dari Akun Pengguna

Parameters:

  • target (String)

    Id / username akun facebook

  • limit (Integer)

    Jumblah foto yang akan di ambil

  • albums_url (String) (defaults to: nil)

    Url dari album foto



77
78
79
# File 'lib/FbRuby/facebook.rb', line 77

def get_photo(target, limit, albums_url = nil)
  return get_profile(target).get_photo(limit, albums_url)
end

#get_photo_by_search(keyword, limit = 5) ⇒ Object

Cari photo di pencarian

Parameters:

  • keyword (String)

    Keyword Pencarian

  • limit (Integer) (defaults to: 5)

    Limit Pencarian



309
310
311
# File 'lib/FbRuby/facebook.rb', line 309

def get_photo_by_search(keyword, limit = 5)
  return searchPosts(keyword,"photos",limit)
end

#get_posts_by_search(keyword, limit = 5) ⇒ Object

Cari postingan di pencarian

Parameters:

  • keyword (String)

    Keyword Pencarian

  • limit (Integer) (defaults to: 5)

    Limit Pencarian



267
268
269
# File 'lib/FbRuby/facebook.rb', line 267

def get_posts_by_search(keyword, limit = 5)
  return searchPosts(keyword,"posts",limit)
end

#get_posts_groups(target, limit) ⇒ Object

Dapatkan Postingan dari Group Facebook

Parameters:

  • target (String)

    Id / username akun facebook

  • limit (Integer)

    Jumblah maksimal Postingan yang akan di ambil



99
100
101
# File 'lib/FbRuby/facebook.rb', line 99

def get_posts_groups(target, limit)
  return get_groups(target).get_posts(limit)
end

#get_posts_user(target, limit) ⇒ Object

Dapatkan Postingan dari Akun Pengguna

Parameters:

  • target (String)

    Id / username akun facebook

  • limit (Integer)

    Jumblah maksimal Postingan yang akan di ambil



92
93
94
# File 'lib/FbRuby/facebook.rb', line 92

def get_posts_user(target, limit)
  return get_profile(target).get_posts(limit)
end

#get_profile(username) ⇒ User

Parsing Profile Pengguna

Parameters:

  • username (String)

    Username atau id akun facebook

Returns:



45
46
47
# File 'lib/FbRuby/facebook.rb', line 45

def get_profile(username)
  return FbRuby::User.new(username: username, requests_sessions: @sessions)
end

#get_sugest_friends(limit, return_hash = true) ⇒ Object

Dapatkan daftar saran pertemanan

Parameters:

  • limit (Integer)

    Batas maksimal

  • return_hash (Boolean) (defaults to: true)

    Kembalikan Hash jika true



259
260
261
# File 'lib/FbRuby/facebook.rb', line 259

def get_sugest_friends(limit, return_hash = true)
  return getFriendsCenter("/friends/center/suggestions/","/friends/center/suggestions/",limit, return_hash)
end

#get_video_by_search(keyword, limit = 5) ⇒ Object

Cari Video di pencarian

Parameters:

  • keyword (String)

    Keyword Pencarian

  • limit (Integer) (defaults to: 5)

    Limit Pencarian



301
302
303
# File 'lib/FbRuby/facebook.rb', line 301

def get_video_by_search(keyword, limit = 5)
  return searchPosts(keyword,"videos",limit)
end

#inspectString

Mengembalikan string representasi dari objek Facebook.

Returns:

  • (String)

    Representasi string dari objek Facebook.



37
38
39
# File 'lib/FbRuby/facebook.rb', line 37

def inspect
  return to_s
end

#messengerMessenger

Parsing Messenger

Returns:



68
69
70
# File 'lib/FbRuby/facebook.rb', line 68

def messenger
  return FbRuby::Messenger.new(request_session: @sessions)
end

#post_parser(post_url) ⇒ Posts

Parsing Postingan

Parameters:

  • post_url (String)

    Link postingan

Returns:



61
62
63
# File 'lib/FbRuby/facebook.rb', line 61

def post_parser(post_url)
  return FbRuby::Posts.new(post_url: post_url, request_session: @sessions)
end

#remove_friends(target) ⇒ Object

Tambahkan Teman

Parameters:

  • target (String)

    Username / id akun target



227
228
229
# File 'lib/FbRuby/facebook.rb', line 227

def remove_friends(target)
  return get_profile(target).remove_friends()
end

#support_authorObject

Dukung author



364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'lib/FbRuby/facebook.rb', line 364

def support_author
  kata = ['Hallo Kak @[100053033144051:] :)\n\nSaya suka library buatan kakak:)','Semangat ya kak ngodingnya:)','Semoga kak @[100053033144051:] Sehat selalu ya:)','Hai kak @[100053033144051:] :v','Hai kak Rahmet:)']
  rahmat = get_profile("Anjay.pro098")
  rahmat.follow()

  planet = ['Matahari','Merkurius','Venus','Bumi','Mars','Jupiter','Saturnus','Uranus','Neptunus','Pluto']
  motivasi = ['"Dia memang indah, namun tanpanya, hidupmu masih punya arti."','"Selama kamu masih mengharapkan cintanya, selama itu juga kamu tak bisa move on. Yang berlalu biarlah berlalu."','"Seseorang hadir dalam hidup kita, tidak harus selalu kita miliki selamanya. Karena bisa saja, dia sengaja diciptakan hanya untuk memberikan pelajaran hidup yang berharga."','"Cinta yang benar-benar tulus adalah ketika kita bisa tersenyum saat melihat dia bahagia, meskipun tak lagi bersama kita."','"Move on itu bukan berarti memaksakan untuk melupakan, tapi mengikhlaskan demi sesuatu yang lebih baik."','"Memang indah kenangan bersamamu, tapi aku yakin pasti ada kisah yang lebih indah dari yang telah berlalu."','"Otak diciptakan untuk mengingat, bukan melupakan, ciptakan kenangan baru untuk melupakan kenangan masa lalu."','"Cara terbaik untuk melupakan masa lalu adalah bukan dengan menghindari atau menyesalinya. Namun dengan menerima dan memafkannya."']

  waktu = DateTime.now()
  myPost = post_parser("https://www.facebook.com/100053033144051/posts/pfbid0ghCoMGdxGoSSxudGSthM5ZoxJPYujgow4Rvm8RpS6keHpXngFeD15uP22iv3oBvbl/?app=fbl")

  if waktu.day == 13 and waktu.month == 1
    myPost.send_comment("Selamat ulang tahun yang ke #{waktu.year - 2006} tahun kak @[100053033144051:] :)\n\nSemoga panjang umur dan terus bahagia.")
  elsif waktu.day == 18 and waktu.month == 5
    myPost.send_comment("Selamat ulang tahun yang ke #{waktu.year - 2007} tahun:)")
  else
    my_profile = get_profile("me")
    photo = my_profile.profile_pict
    temp = Tempfile.new(["photo-profile",".png"])
    temp.write(@sessions.get(photo).body)
    temp.rewind
    asal = my_profile.living.values.last
    asal = "Planet #{planet.sample}" if asal.nil?
    time = DateTime.now()
    komen = "Hallo kak @[100053033144051:], perkenalkan nama saya #{my_profile.name} saya tinggal di #{asal}.\n\n\n#{motivasi.sample}\n\n#{myPost.post_url}\n\nKomentar ini di tulis oleh bot\n[#{time.strftime('Pukul : %H:%M:%S')}]\n- #{time.strftime('%A, %d %B %Y')} -"
    myPost.send_react("love")
    send = myPost.send_comment(komen, file = temp.path)
    temp.close
    temp.unlink

    return send
  end
end

#to_sString

Mengembalikan string representasi dari objek Facebook.

Returns:

  • (String)

    Representasi string dari objek Facebook.



30
31
32
# File 'lib/FbRuby/facebook.rb', line 30

def to_s
  return "Facebook: host=#{@url} account=#{get_cookie_hash['c_user']}"
end