Created
March 6, 2021 09:47
-
-
Save rishi-raj-jain/a8a73e6709f6222b01734d730c8fbecf to your computer and use it in GitHub Desktop.
get-unsplash-photos-from-user-handle
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
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