Skip to content

Commit

Permalink
Merge pull request #61 from rigon/demo
Browse files Browse the repository at this point in the history
Demo
  • Loading branch information
rigon authored Dec 23, 2024
2 parents c12c85d + d428247 commit 95063d3
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 31 deletions.
10 changes: 8 additions & 2 deletions .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: 18
- name: Obtain demo gallery
env:
PIXABAY_API_KEY: ${{ secrets.PIXABAY_API_KEY }}
run: node demo-download.js
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v4
Expand All @@ -92,5 +100,3 @@ jobs:
platforms: linux/amd64,linux/arm64,linux/arm/v6
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
secrets: |
pixabay=${{ secrets.PIXABAY_API_KEY }}
30 changes: 1 addition & 29 deletions Dockerfile.demo
Original file line number Diff line number Diff line change
@@ -1,31 +1,3 @@
FROM python:alpine

RUN pip install pixabay

RUN --mount=type=secret,id=pixabay \
PIXABAY_API_KEY=$(cat /run/secrets/pixabay) && \
python <<EOF
import pixabay.core, os

terms=["Nature", "Architecture", "Animals", "Travel", "People", "Autumn", "The Grand Canyon", "Great Barrier Reef", "Maldives", "Paris", "Iceland",
"Wallpapers/Moutains", "Wallpapers/Rivers", "Wallpapers/Lanscapes", "Wallpapers/Abstract", "Wallpapers/Gradients", "Wallpapers/Patterns"]

px = pixabay.core(os.environ["PIXABAY_API_KEY"])
for term in terms:
os.makedirs("/photos/"+term)
search = px.query(
query = term.replace("/", " "),
perPage = 50,
minWidth = 500,
minHeight = 500,
safeSearch = True,
)
for i in range(50):
filename = "/photos/%s/%d.jpg" % (term, i+1)
print(filename)
search[i].download(filename, "largeImage")
EOF

FROM rigon/photo-gallery:latest
COPY --from=pixabay /photos /photos
COPY photos /photos
RUN touch /photos/Favorites.PG-ALBUM
61 changes: 61 additions & 0 deletions demo-download.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import * as fs from "node:fs";
import * as path from "node:path";
import * as https from "node:https";
import * as querystring from "node:querystring";

const OUT_DIR = "photos/";
const API_URL = "https://pixabay.com/api/";
const API_KEY = process.env.PIXABAY_API_KEY;
const terms = ["Nature", "Architecture", "Animals", "Travel", "People", "Autumn", "The Grand Canyon", "Great Barrier Reef", "Maldives", "Paris", "Iceland",
"Wallpapers/Moutains", "Wallpapers/Rivers", "Wallpapers/Lanscapes", "Wallpapers/Abstract", "Wallpapers/Gradients", "Wallpapers/Patterns"]


const options = {
key: API_KEY,
image_type: "photo",
per_page: 50,
safesearch: true,
}

function getPage(link) {
return new Promise(function (resolve, reject) {
const req = https.get(link, res => {
let chunks = [];
res.on('data', chunk => {
// Not the most efficient thing
chunks.push(chunk); //.toString(); //('latin1');
});
res.on('end', function () {
resolve(Buffer.concat(chunks));
});
});

req.on('error', error => {
reject(error);
});
});
}

async function run() {
for (const term of terms) {
fs.mkdirSync(path.join(OUT_DIR, term), { recursive: true });

const query = querystring.encode({
...options,
q: term.replace("/", " "),
});
const url = `${API_URL}?${query}`;
const page = await getPage(url);
const data = JSON.parse(page.toString());

let i = 0;
for (const hit of data.hits) {
i++;
const filename = path.join(OUT_DIR, term, i + ".jpg");
console.log(filename);
const filedata = await getPage(hit.largeImageURL);
fs.writeFileSync(filename, filedata, { encoding: 'binary' });
}
}
}
run();

0 comments on commit 95063d3

Please sign in to comment.