I needed a way to easily query my medium posts client-side for my personal blog & wanted to try out writing my first Google Cloud Shell Tutorial. Through this post, we will create a publicly accessible endpoint to get a json feed of your medium posts.
Before you begin:
- Make sure you have a medium account in mind that you want posts from
- Have a Google Cloud Platform account
- Familiarity with Google Cloud Platform command line tools will help.
Click the Continue button to move to the next step.
Yes! Medium has an API that seems primarily designed to let you publish posts. That won't help me much in getting my content. What is really helpful is the undocumented json format that you can request of just about every medium page. All you need to do is append ?format=json to your request. For me, my latest posts are listed at https://medium.com/@tristansokol/latest and https://medium.com/@tristansokol/latest?format=json has all that information in a somewhat convenient json format. The only reason I don't pump that into my page is the explicit preventions that Medium prepends to the json (])}while(1);</x>). Oh well, nobody can stop determination & javascript!
First things first, you need to set your current cloud project.
gcloud config set project YOUR_PROJECT_NAMENext, see if cloud functions are enabled for your project with a command like:
gcloud functions listIf they are not, you should get a response like
API [cloudfunctions.googleapis.com] not enabled on project
[single-cirrus-142722]. Would you like to enable and retry? (y/N)?
and a simple y will enable the appropriate APIs.
Take a look at walkthrough editor-open-file medium-get-latest-api-function-tutorial/index.js "index.js".
You can edit a file stored in Cloud Shell using Cloud Shell’s built-in text editor, isn't that neat! The code is pretty basic, basically a wrapper around a single request and stripping out the cross site preventions.
exports.getLatest = (req, res) => {
let username = 'yourusername';
request('https://medium.com/@' + username + '/latest?format=json', function(error, response, body) {
res.send(JSON.parse(body.substring(16)));
});
};Edit the username to your Medium username!
After you have saved your username to the index.js, deploy your function with:
gcloud functions deploy getLatest --trigger-httpThe big G handles all of your dependencies by reading the package.json, so you don't have to worry about uploading anything else.
After your deploy is complete, you should see some output similar to
httpsTrigger:
url: https://us-central1-super-burrito-212121.cloudfunctions.net/getLatestAnd you can then try out your new API:
curl https://us-central1-super-burrito-212121.cloudfunctions.net/getLatestand you should get back a bunch of json about your latest posts on Medium.