From 49b0bbbc58e20d0a0af8207d5588c359f34af17c Mon Sep 17 00:00:00 2001
From: Arnaud Levy <contact@arnaudlevy.com>
Date: Sun, 4 Feb 2024 21:45:31 +0100
Subject: [PATCH] wip

---
 Gemfile.lock                                  | 34 ++++++++++---------
 app/models/communication/website.rb           |  1 +
 .../communication/website/with_screenshot.rb  | 13 +++++++
 app/services/screenshot.rb                    | 30 ++++++++++++++++
 4 files changed, 62 insertions(+), 16 deletions(-)
 create mode 100644 app/models/communication/website/with_screenshot.rb
 create mode 100644 app/services/screenshot.rb

diff --git a/Gemfile.lock b/Gemfile.lock
index efa92a34d..a7bce86fa 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -123,17 +123,17 @@ GEM
     autoprefixer-rails (10.4.16.0)
       execjs (~> 2)
     aws-eventstream (1.3.0)
-    aws-partitions (1.883.0)
-    aws-sdk-core (3.190.3)
+    aws-partitions (1.887.0)
+    aws-sdk-core (3.191.0)
       aws-eventstream (~> 1, >= 1.3.0)
       aws-partitions (~> 1, >= 1.651.0)
       aws-sigv4 (~> 1.8)
       jmespath (~> 1, >= 1.6.1)
-    aws-sdk-kms (1.76.0)
-      aws-sdk-core (~> 3, >= 3.188.0)
+    aws-sdk-kms (1.77.0)
+      aws-sdk-core (~> 3, >= 3.191.0)
       aws-sigv4 (~> 1.1)
-    aws-sdk-s3 (1.142.0)
-      aws-sdk-core (~> 3, >= 3.189.0)
+    aws-sdk-s3 (1.143.0)
+      aws-sdk-core (~> 3, >= 3.191.0)
       aws-sdk-kms (~> 1)
       aws-sigv4 (~> 1.8)
     aws-sigv4 (1.8.0)
@@ -142,7 +142,7 @@ GEM
     bcrypt (3.1.20)
     bigdecimal (3.1.6)
     bindex (0.8.1)
-    bootsnap (1.17.1)
+    bootsnap (1.18.3)
       msgpack (~> 1.2)
     bootstrap (5.3.2)
       autoprefixer-rails (>= 9.1.0)
@@ -157,11 +157,11 @@ GEM
     builder (3.2.4)
     byebug (11.1.3)
     cancancan (3.3.0)
-    capybara (3.39.2)
+    capybara (3.40.0)
       addressable
       matrix
       mini_mime (>= 0.1.3)
-      nokogiri (~> 1.8)
+      nokogiri (~> 1.11)
       rack (>= 1.6.0)
       rack-test (>= 0.6.3)
       regexp_parser (>= 1.5, < 3.0)
@@ -186,7 +186,8 @@ GEM
       unaccent (~> 0.3)
     country_select (8.0.3)
       countries (~> 5.0)
-    crack (0.4.5)
+    crack (0.4.6)
+      bigdecimal
       rexml
     crass (1.0.6)
     csl (2.0.0)
@@ -392,11 +393,11 @@ GEM
     net-smtp (0.4.0.1)
       net-protocol
     nio4r (2.7.0)
-    nokogiri (1.16.0-arm64-darwin)
+    nokogiri (1.16.2-arm64-darwin)
       racc (~> 1.4)
-    nokogiri (1.16.0-x86_64-darwin)
+    nokogiri (1.16.2-x86_64-darwin)
       racc (~> 1.4)
-    nokogiri (1.16.0-x86_64-linux)
+    nokogiri (1.16.2-x86_64-linux)
       racc (~> 1.4)
     oauth2 (2.0.9)
       faraday (>= 0.17.3, < 3.0)
@@ -405,7 +406,8 @@ GEM
       rack (>= 1.2, < 4)
       snaky_hash (~> 2.0)
       version_gem (~> 1.1)
-    octokit (8.0.0)
+    octokit (8.1.0)
+      base64
       faraday (>= 1, < 3)
       sawyer (~> 0.9)
     omniauth (2.1.2)
@@ -436,7 +438,7 @@ GEM
     puma (6.4.2)
       nio4r (~> 2.0)
     racc (1.7.3)
-    rack (3.0.8)
+    rack (3.0.9)
     rack-mini-profiler (2.3.4)
       rack (>= 1.2.0)
     rack-protection (4.0.0)
@@ -585,7 +587,7 @@ GEM
       ethon (>= 0.9.0)
     tzinfo (2.0.6)
       concurrent-ruby (~> 1.0)
-    tzinfo-data (1.2023.4)
+    tzinfo-data (1.2024.1)
       tzinfo (>= 1.0.0)
     unaccent (0.4.0)
     unicode-display_width (2.5.0)
diff --git a/app/models/communication/website.rb b/app/models/communication/website.rb
index aa6b81230..cac6e7952 100644
--- a/app/models/communication/website.rb
+++ b/app/models/communication/website.rb
@@ -69,6 +69,7 @@ class Communication::Website < ApplicationRecord
   include WithReferences
   include WithSpecialPages
   include WithMenus # Menus must be created after special pages, so we can fill legal menu
+  include WithScreenshot
   include WithSecurity
   include WithStyle
   include WithTheme
diff --git a/app/models/communication/website/with_screenshot.rb b/app/models/communication/website/with_screenshot.rb
new file mode 100644
index 000000000..014b85c0f
--- /dev/null
+++ b/app/models/communication/website/with_screenshot.rb
@@ -0,0 +1,13 @@
+module Communication::Website::WithScreenshot
+  extend ActiveSupport::Concern
+  
+  included do
+    has_one_attached :screenshot
+  end
+  
+  def screenshot!
+    screenshot_url = Screenshot.capture(url)
+    downloaded_image = URI.open(screenshot_url)
+    self.screenshot.attach(io: downloaded_image  , filename: "screenshot.png")
+  end
+end
\ No newline at end of file
diff --git a/app/services/screenshot.rb b/app/services/screenshot.rb
new file mode 100644
index 000000000..f46019a94
--- /dev/null
+++ b/app/services/screenshot.rb
@@ -0,0 +1,30 @@
+class Screenshot
+  # https://microlink.io/docs/api/getting-started/overview
+  # https://api.microlink.io/?url=https://www.noesya.coop&screenshot=true&meta=false&device=Macbook%20Pro%2015
+  # {
+  #   "status": "success",
+  #   "data": {
+  #     "url": "https://www.noesya.coop/",
+  #     "screenshot": {
+  #       "size_pretty": "1.25 MB",
+  #       "size": 1253017,
+  #       "type": "png",
+  #       "url": "https://iad.microlink.io/9Yn0uu1Ajg4QX8uKdMqglHSQn0o5nfl9dQiyWfLGUcseMgcnpqjjj_YT0yDDSSnr4YF44cALFaYnEfDafjG09w.png",
+  #       "width": 2880,
+  #       "height": 1800
+  #     }
+  #   }
+  # }
+  def self.capture(url)
+    response = HTTParty.get('https://api.microlink.io', {
+      query: {
+        url: url,
+        screenshot: true,
+        meta: false,
+        device: 'Macbook Pro 15'
+      }
+    })
+    data = JSON.parse(response.body)
+    data.dig('data', 'screenshot', 'url')
+  end
+end
\ No newline at end of file
-- 
GitLab