Guest User

Untitled

a guest
Mar 20th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. class Grabbers::FacebookAttachments
  2.  
  3. def initialize(api_client)
  4. @api_client = api_client
  5. end
  6.  
  7. def grab_for_post(post)
  8. result = { 'attachment' => { 'media' => [] }}
  9.  
  10. begin
  11. post = @api_client.get_object(post['id'], fields: 'attachments')
  12.  
  13. post_attachments(post).each do |subattachment|
  14. if subattachment['target'].present? && subattachment['target']['id'].present?
  15. attachment = grab_attachment_by(subattachment['target']['id'])
  16.  
  17. if attachment.present?
  18. attachment['photo']['fbid'] = subattachment['target']['id']
  19. attachment['href'] = subattachment['target']['url']
  20.  
  21. result['attachment']['media'] << attachment
  22. end
  23. end
  24. end
  25.  
  26. if result['attachment']['media'].compact.empty?
  27. result['attachment']['media'] = default_attachments_for(post)
  28. end
  29. rescue Exception => error
  30. ErrorsService.log(error, title: 'Cant grab attachments for post', data: {post: post})
  31. end
  32.  
  33. result
  34. end
  35.  
  36. private
  37.  
  38. def grab_attachment_by(object_id)
  39. begin
  40. attachment = @api_client.get_object(object_id)
  41.  
  42. image = attachment['images'].max_by { |image| [image['width'], image['height']].max }
  43.  
  44. {'type' => 'photo', 'src' => image['source'], 'photo' => {'width' => image['width'], 'height' => image['height']} }
  45. rescue Exception
  46. return nil
  47. end
  48. end
  49.  
  50. def default_attachments_for(post)
  51. post_attachments(post).map do |subattachment|
  52. {
  53. 'type' => 'photo',
  54. 'src' => image_src(subattachment),
  55. 'href' => image_url(subattachment),
  56. 'photo' => {
  57. 'fbid' => image_fbid(subattachment),
  58. 'width' => subattachment['media']['image']['width'],
  59. 'height' => subattachment['media']['image']['height']
  60. }
  61. }
  62. end
  63. end
  64.  
  65. def image_url(subattachment)
  66. subattachment['target'].present? ? subattachment['target']['url'] : subattachment['url']
  67. end
  68.  
  69. def image_src(subattachment)
  70. begin
  71. src = subattachment['media']['image']['src']
  72.  
  73. return CGI::parse(src)['url'].first if src.include?('safe_image.php')
  74.  
  75. src
  76. rescue Exception => error
  77. ErrorsService.log(error, title: 'Cant process image src', data: {subattachment: subattachment}, skip_airbrake_notification: true)
  78. ''
  79. end
  80. end
  81.  
  82. def image_fbid(subattachment)
  83. subattachment['target'].present? ? subattachment['target']['id'] : subattachment['id']
  84. end
  85.  
  86. def post_attachments(post)
  87. return post['attachments']['data'].first['subattachments']['data'] if post['attachments']['data'].first['subattachments'].present?
  88. return post['attachments']['data'] if post['attachments']['data'].first['media'].present?
  89.  
  90. []
  91. end
  92. end
Add Comment
Please, Sign In to add comment