Skip to content

Commit

Permalink
Test build demo gallery
Browse files Browse the repository at this point in the history
  • Loading branch information
rigon committed Dec 16, 2024
1 parent 3b62096 commit f178ccb
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 28 deletions.
10 changes: 8 additions & 2 deletions .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,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 @@ -93,5 +101,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 }}
53 changes: 27 additions & 26 deletions Dockerfile.demo
Original file line number Diff line number Diff line change
@@ -1,35 +1,36 @@
FROM python:alpine AS pixabay
# FROM python:alpine AS pixabay

RUN pip install pixabay
# RUN pip install pixabay

RUN --mount=type=secret,id=pixabay \
python <<EOF
import pixabay.core, os, time
# RUN --mount=type=secret,id=pixabay \
# python <<EOF
# import pixabay.core, os, time

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"]
# 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"]

with open('/run/secrets/pixabay', 'r') as file:
apiKey = file.read()
# with open('/run/secrets/pixabay', 'r') as file:
# apiKey = file.read()

px = pixabay.core(apiKey)
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")
time.sleep(3) # Sleep 3 seconds to avoid rate limit 100reqs/min
# px = pixabay.core(apiKey)
# 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")
# time.sleep(3) # Sleep 3 seconds to avoid rate limit 100reqs/min

EOF
# EOF

FROM rigon/photo-gallery:dev
COPY --from=pixabay /photos /photos
# 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 f178ccb

Please sign in to comment.