Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rishi-raj-jain/a8a73e6709f6222b01734d730c8fbecf to your computer and use it in GitHub Desktop.
Save rishi-raj-jain/a8a73e6709f6222b01734d730c8fbecf to your computer and use it in GitHub Desktop.
get-unsplash-photos-from-user-handle
import got from "got/dist/source";
import { JSDOM } from "jsdom";
export default async (req, res) => {
let images = [];
const response = await got(`https://unsplash.com/@${req.query.userHandle}`);
if (response.statusCode === 200) {
const dom = new JSDOM(response.body);
let allA = dom.window.document.querySelectorAll(
'[itemProp="contentUrl"] img'
);
for (let i in allA) {
images.push({
srcset: allA[i].srcset,
sizes: allA[i].sizes
});
}
res.setHeader("Content-Type", "application/json");
res.send(
JSON.stringify({
images: images,
code: 1
})
);
} else {
res.setHeader("Content-Type", "application/json");
res.send(
JSON.stringify({
code: 0
})
);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment