{
  "name": "Flickr GovZA",
  "nodes": [
    {
      "parameters": {
        "path": "/:ratio_w/:ratio_h",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "33583cb8-99f2-421b-90bc-2acc0ce30c5b",
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        300,
        480
      ],
      "webhookId": "33583cb8-99f2-421b-90bc-2acc0ce30c5b"
    },
    {
      "parameters": {
        "url": "https://www.flickr.com/services/rest",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "method",
              "value": "flickr.people.getPhotos"
            },
            {
              "name": "user_id",
              "value": "governmentza"
            },
            {
              "name": "per_page",
              "value": "14"
            },
            {
              "name": "format",
              "value": "json"
            },
            {
              "name": "extras",
              "value": "url_o,date_upload,description"
            },
            {
              "name": "api_key",
              "value": "={{ $json.apiKey }}"
            },
            {
              "name": "nojsoncallback",
              "value": "1"
            }
          ]
        },
        "options": {
          "response": {
            "response": {
              "responseFormat": "json"
            }
          }
        }
      },
      "id": "9ac3c011-0bc0-435f-a274-7f71fab11ef6",
      "name": "Flickr GovZA",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 3,
      "position": [
        980,
        480
      ]
    },
    {
      "parameters": {
        "respondWith": "binary",
        "responseDataSource": "set",
        "options": {
          "responseCode": 200
        }
      },
      "id": "6bad9fcf-2610-4527-9fa9-f7720741bcc2",
      "name": "Respond to Webhook",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [
        1600,
        720
      ]
    },
    {
      "parameters": {
        "url": "https://www.flickr.com/photos/governmentza/",
        "options": {
          "response": {
            "response": {
              "responseFormat": "text",
              "outputPropertyName": "html"
            }
          }
        }
      },
      "id": "69563396-fb18-433e-9f37-3764c42906f6",
      "name": "Get Flickr Page",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 3,
      "position": [
        520,
        480
      ]
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [
            {
              "name": "apiKey",
              "value": "={{ $json.html.match(/root.YUI_config.flickr.api.site_key = \"(\\w+)\";/)[1] }}"
            }
          ]
        },
        "options": {}
      },
      "id": "1542a0f0-4568-446e-ba7d-48bafbd9e1e0",
      "name": "Extract APIKey",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        740,
        480
      ]
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [
            {
              "name": "photo_url",
              "value": "={{ \ndiff_h = (new Date().getTime() - parseInt($json.photos.photo[0].dateupload)*1000)/(1000*60*60), \nidx = Math.trunc(diff_h / 24), \n$json[\"photos\"][\"photo\"][idx][\"url_o\"]\n}}"
            }
          ]
        },
        "options": {}
      },
      "id": "b08fa615-021c-4c45-950f-ad7cbc1898c5",
      "name": "Pick out photo",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        1200,
        480
      ]
    },
    {
      "parameters": {
        "url": "={{ $json.photo_url }}",
        "options": {
          "response": {
            "response": {
              "responseFormat": "file"
            }
          }
        }
      },
      "id": "bc36ebb7-dfa0-47e8-bf3b-b688c2d69a8c",
      "name": "Download Image",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 3,
      "position": [
        1400,
        480
      ]
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "number": [
            {
              "name": "target_width_ratio",
              "value": "={{ $json.params.ratio_w }}"
            },
            {
              "name": "target_height_ratio",
              "value": "={{ $json.params.ratio_h }}"
            }
          ]
        },
        "options": {}
      },
      "id": "65fe41a0-6dee-41c3-8c0d-4a01204d604e",
      "name": "Get Aspect Ratio",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        520,
        720
      ]
    },
    {
      "parameters": {
        "mode": "combine",
        "combinationMode": "mergeByPosition",
        "options": {}
      },
      "id": "6dc562bc-e273-4511-890f-a5df967ff958",
      "name": "Merge",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 2,
      "position": [
        940,
        720
      ]
    },
    {
      "parameters": {
        "operation": "crop",
        "width": "={{ $json.final_w }}",
        "height": "={{ $json.final_h }}",
        "positionX": "={{ $json.x_offset }}",
        "positionY": "={{ $json.y_offset }}",
        "options": {}
      },
      "id": "0e12e817-bf1e-4c47-a7fb-1dafba7a4448",
      "name": "Crop to AR",
      "type": "n8n-nodes-base.editImage",
      "typeVersion": 1,
      "position": [
        1380,
        720
      ]
    },
    {
      "parameters": {
        "operation": "information"
      },
      "id": "767f6818-5d0a-4427-ae7f-799f0ada19a9",
      "name": "Extract Info",
      "type": "n8n-nodes-base.editImage",
      "typeVersion": 1,
      "position": [
        1600,
        480
      ]
    },
    {
      "parameters": {
        "mode": "runOnceForEachItem",
        "jsCode": "const inputJson = $input.item.json;\nconst w_mult = inputJson.size.width / inputJson.target_width_ratio;\nconst target_h = inputJson.target_height_ratio * w_mult;\n\nconst final_h = Math.trunc(Math.min(target_h, inputJson.size.height));\n\nlet final_w;\nif (target_h <= inputJson.size.height){\n  final_w = inputJson.size.width;\n}else{\n  const crop_ratio = target_h/inputJson.size.height;\n  final_w = Math.trunc(inputJson.size.width * (1/crop_ratio));\n}\n\n$input.item.json = {\n  original_size: $input.item.json.size,\n  target_ratio: {w: inputJson.target_width_ratio, \n                 h: inputJson.target_height_ratio},\n  final_h, final_w, \n  x_offset: Math.trunc(Math.abs(inputJson.size.width - final_w)/2),\n  y_offset: Math.trunc(Math.abs(inputJson.size.height - final_h)/2),\n};\n\nreturn $input.item;"
      },
      "id": "36f6f30d-1c2d-40d5-91e5-0b4ee715c4a2",
      "name": "Calculate crop",
      "type": "n8n-nodes-base.code",
      "typeVersion": 1,
      "position": [
        1160,
        720
      ]
    },
    {
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": "={{ isNaN($json.target_width_ratio) }}",
              "value2": true
            },
            {
              "value1": "={{ isNaN($json.target_height_ratio) }}",
              "value2": true
            }
          ]
        },
        "combineOperation": "any"
      },
      "id": "1e587f20-146f-4d12-8323-ae85c4208df6",
      "name": "IF Nan",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        700,
        720
      ]
    },
    {
      "parameters": {
        "respondWith": "text",
        "responseBody": "=Dink jy regtig \"{{ $json.target_width_ratio }}\" : \"{{ $json.target_height_ratio }}\" is 'n goeie aspect ratio?",
        "options": {
          "responseCode": 400
        }
      },
      "id": "70586c6e-a2c6-4de5-9e61-20fd922d74d0",
      "name": "Respond to Webhook1",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [
        940,
        920
      ]
    }
  ],
  "pinData": {
    "Webhook": [
      {
        "json": {
          "headers": {
            "host": "n8n.singularity.net.za",
            "connection": "close",
            "user-agent": "curl/7.87.0",
            "accept": "*/*",
            "x-forwarded-for": "165.73.116.162",
            "x-forwarded-host": "n8n.singularity.net.za",
            "x-forwarded-port": "443",
            "x-forwarded-proto": "https",
            "x-forwarded-server": "1f3f2a9f06e3",
            "x-real-ip": "165.73.116.162",
            "accept-encoding": "gzip"
          },
          "params": {
            "ratio_w": "16",
            "ratio_h": "8"
          },
          "query": {},
          "body": {}
        }
      }
    ]
  },
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Get Flickr Page",
            "type": "main",
            "index": 0
          },
          {
            "node": "Get Aspect Ratio",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Flickr GovZA": {
      "main": [
        [
          {
            "node": "Pick out photo",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Flickr Page": {
      "main": [
        [
          {
            "node": "Extract APIKey",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract APIKey": {
      "main": [
        [
          {
            "node": "Flickr GovZA",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Pick out photo": {
      "main": [
        [
          {
            "node": "Download Image",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Aspect Ratio": {
      "main": [
        [
          {
            "node": "IF Nan",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Download Image": {
      "main": [
        [
          {
            "node": "Extract Info",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge": {
      "main": [
        [
          {
            "node": "Calculate crop",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Info": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Calculate crop": {
      "main": [
        [
          {
            "node": "Crop to AR",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Crop to AR": {
      "main": [
        [
          {
            "node": "Respond to Webhook",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF Nan": {
      "main": [
        [
          {
            "node": "Respond to Webhook1",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 1
          }
        ]
      ]
    }
  },
  "active": true,
  "settings": {},
  "versionId": "d6209d5b-0ac0-4218-b4c0-eb44a39ead6d",
  "id": "1",
  "meta": {},
  "tags": []
}