-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
96 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |