Skip to content

Commit 95063d3

Browse files
authored
Merge pull request #61 from rigon/demo
Demo
2 parents c12c85d + d428247 commit 95063d3

File tree

3 files changed

+70
-31
lines changed

3 files changed

+70
-31
lines changed

.github/workflows/docker-image.yml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ jobs:
6767
steps:
6868
- name: Checkout
6969
uses: actions/checkout@v3
70+
- name: Setup Node
71+
uses: actions/setup-node@v4
72+
with:
73+
node-version: 18
74+
- name: Obtain demo gallery
75+
env:
76+
PIXABAY_API_KEY: ${{ secrets.PIXABAY_API_KEY }}
77+
run: node demo-download.js
7078
- name: Extract metadata (tags, labels) for Docker
7179
id: meta
7280
uses: docker/metadata-action@v4
@@ -92,5 +100,3 @@ jobs:
92100
platforms: linux/amd64,linux/arm64,linux/arm/v6
93101
tags: ${{ steps.meta.outputs.tags }}
94102
labels: ${{ steps.meta.outputs.labels }}
95-
secrets: |
96-
pixabay=${{ secrets.PIXABAY_API_KEY }}

Dockerfile.demo

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,3 @@
1-
FROM python:alpine
2-
3-
RUN pip install pixabay
4-
5-
RUN --mount=type=secret,id=pixabay \
6-
PIXABAY_API_KEY=$(cat /run/secrets/pixabay) && \
7-
python <<EOF
8-
import pixabay.core, os
9-
10-
terms=["Nature", "Architecture", "Animals", "Travel", "People", "Autumn", "The Grand Canyon", "Great Barrier Reef", "Maldives", "Paris", "Iceland",
11-
"Wallpapers/Moutains", "Wallpapers/Rivers", "Wallpapers/Lanscapes", "Wallpapers/Abstract", "Wallpapers/Gradients", "Wallpapers/Patterns"]
12-
13-
px = pixabay.core(os.environ["PIXABAY_API_KEY"])
14-
for term in terms:
15-
os.makedirs("/photos/"+term)
16-
search = px.query(
17-
query = term.replace("/", " "),
18-
perPage = 50,
19-
minWidth = 500,
20-
minHeight = 500,
21-
safeSearch = True,
22-
)
23-
for i in range(50):
24-
filename = "/photos/%s/%d.jpg" % (term, i+1)
25-
print(filename)
26-
search[i].download(filename, "largeImage")
27-
EOF
28-
291
FROM rigon/photo-gallery:latest
30-
COPY --from=pixabay /photos /photos
2+
COPY photos /photos
313
RUN touch /photos/Favorites.PG-ALBUM

demo-download.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import * as fs from "node:fs";
2+
import * as path from "node:path";
3+
import * as https from "node:https";
4+
import * as querystring from "node:querystring";
5+
6+
const OUT_DIR = "photos/";
7+
const API_URL = "https://pixabay.com/api/";
8+
const API_KEY = process.env.PIXABAY_API_KEY;
9+
const terms = ["Nature", "Architecture", "Animals", "Travel", "People", "Autumn", "The Grand Canyon", "Great Barrier Reef", "Maldives", "Paris", "Iceland",
10+
"Wallpapers/Moutains", "Wallpapers/Rivers", "Wallpapers/Lanscapes", "Wallpapers/Abstract", "Wallpapers/Gradients", "Wallpapers/Patterns"]
11+
12+
13+
const options = {
14+
key: API_KEY,
15+
image_type: "photo",
16+
per_page: 50,
17+
safesearch: true,
18+
}
19+
20+
function getPage(link) {
21+
return new Promise(function (resolve, reject) {
22+
const req = https.get(link, res => {
23+
let chunks = [];
24+
res.on('data', chunk => {
25+
// Not the most efficient thing
26+
chunks.push(chunk); //.toString(); //('latin1');
27+
});
28+
res.on('end', function () {
29+
resolve(Buffer.concat(chunks));
30+
});
31+
});
32+
33+
req.on('error', error => {
34+
reject(error);
35+
});
36+
});
37+
}
38+
39+
async function run() {
40+
for (const term of terms) {
41+
fs.mkdirSync(path.join(OUT_DIR, term), { recursive: true });
42+
43+
const query = querystring.encode({
44+
...options,
45+
q: term.replace("/", " "),
46+
});
47+
const url = `${API_URL}?${query}`;
48+
const page = await getPage(url);
49+
const data = JSON.parse(page.toString());
50+
51+
let i = 0;
52+
for (const hit of data.hits) {
53+
i++;
54+
const filename = path.join(OUT_DIR, term, i + ".jpg");
55+
console.log(filename);
56+
const filedata = await getPage(hit.largeImageURL);
57+
fs.writeFileSync(filename, filedata, { encoding: 'binary' });
58+
}
59+
}
60+
}
61+
run();

0 commit comments

Comments
 (0)