Skip to content

Commit

Permalink
PR review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Meldiron committed Jan 23, 2023
1 parent bc4f8f2 commit 5d86706
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
10 changes: 8 additions & 2 deletions deno/generate_website_screenshot/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ _Example input:_

```json
{
"url":"https://google.com/"
"url":"https://appwrite.io/"
}

```
Expand All @@ -17,7 +17,7 @@ _Example output:_
```json
{
"sucess": true,
"screenshot": "base64 png image string"
"screenshot": "iVBORw0KGgoAAAANSUh...XWkulRXfkcAAAAASUVORK5CYII="
}
```

Expand Down Expand Up @@ -49,6 +49,12 @@ docker run -p 3000:3000 -e INTERNAL_RUNTIME_ENTRYPOINT=src/mod.ts -e INTERNAL_RU

Your function is now listening on port `3000`, and you can execute it by sending `POST` request with appropriate authorization headers. To learn more about runtime, you can visit Deno runtime [README](https://github.com/open-runtimes/open-runtimes/tree/main/runtimes/deno-1.24).

4. Execute function:

```
curl http:https://localhost:3000/ -d '{"variables":{"SCREEN_SHOT_API_KEY":"25PVQ6R-R124R86-PKB2H0A-Z61P1JR"},"payload":"{\"url\":\"https://appwrite.io/\"}"}' -H "X-Internal-Challenge: secret-key" -H "Content-Type: application/json"
```

## 📝 Notes
- This function is designed for use with Appwrite Cloud Functions. You can learn more about it in [Appwrite docs](https://appwrite.io/docs/functions).
- This example is compatible with Deno 1.13 and 1.14. Other versions may work but are not guarenteed to work as they haven't been tested.
40 changes: 19 additions & 21 deletions deno/generate_website_screenshot/src/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { base64Encode } from "./deps.ts";

const getBase64FromUrl = async (url: string) => {
const data = await fetch(url);

if (data.ok) {
const blob = await data.blob();
const arr = await blob.arrayBuffer();
Expand All @@ -13,34 +14,31 @@ const getBase64FromUrl = async (url: string) => {
};

export default async function (req: any, res: any) {
const APIkey = req.variables["SCREEN_SHOT_API_KEY"];
try{
const { url } = JSON.parse(req.payload);

let APIkey: any;
let url: any;

}catch(error){
res.json({ success: false, message: "Invalid JSON body" });
return;
}
if (!url) {
res.json({ success: false, message: "URL is required" });
try {
const payload = JSON.parse(req.payload ?? '{}');
url = payload.url;
} catch(error) {
res.json({ success: false, message: "Please provide URL." });
return;
}
if (!APIkey) {
res.json({ success: false, message: "API key is required" });
try {
APIkey = req.variables["SCREEN_SHOT_API_KEY"];
} catch(error) {
res.json({ success: false, message: "Please API key." });
return;
}

const fetchURL = `https://shot.screenshotapi.net/screenshot?token=${APIkey}&url=${url}&output=image&file_type=png&wait_for_event=load`;
try{
const response: { success: boolean; screenshot: string; error: string } =
await getBase64FromUrl(fetchURL);
res.json({ success: true, screenshot: response.screenshot });
return;
}catch(error){

try {
const response = await getBase64FromUrl(fetchURL);
res.json({ success: true, screenshot: response });
return;
} catch(error) {
res.json({ success: false, message: error });
return;

}


}

0 comments on commit 5d86706

Please sign in to comment.