JSON API for Social Media Share Counts

Published on by Morgan Ney

Recently I needed to implement social media buttons with a custom look and feel. The "custom look and feel" part meant that I couldn't simply use the vendor supplied embed scripts. If I did then yes, I could use CSS to hide the vendor's button, but the browser's restriction on third-party iframe content meant I still couldn't access the share counts. I would need to use the vendor API's for obtaining share counts.

After a little research (Googling) I realized each vendor had a slightly different API. Some supported CORS or JSONP and were publicly documented, while others supported neither and were not public (I'm looking at you Google+). In short, there was no way to get the share counts I needed for the most popular vendors directly from the browser. A server-side script or service was required.

I figured I'd write a Node.js app that's a local proxy exposing a JSON API for obtaining social media share counts: socoapi. This way front end logic can be a bit simpler making only one XHR request to socoapi for obtaining the share counts. You can install socoapi with Node's package manager npm: npm install socoapi. Now you can use it as a standalone server or embedded in some other application.

Caching is also supported by the socoapi server. Here is the signature to the listen() method: listen(port, cachettl [,callback]). cachettl are the milliseconds to cache API responses for. This way vendor servers are not overburdened needlessly.

The socoapi API supports CORS (but currently not JSONP) and has two endpoints. The first endpoint is really all you need:

GET /counts?url=[the url to get share counts for]

This returns the share counts for url from all supported vendors. Be sure to percent-encode any query component to the share url. For example, GET /counts?url=http://www.foobar.baz/%3Fq1%3Dv1%26q2%3Dv2

If using socoapi as an embedded component to an application running behind a reverse proxy an Apache Virtual Host configuration might look something like this:

socoapi source code is hosted on github where issues and pull requests are welcome. The package is also hosted on npm.

Relevant to  Node.js, JSON, API, Social Media

Recent Posts

A RESTful JSON API for the NextBus XML Feed

Published on by Morgan Ney

The NextBus XML feed provides useful data for real-time transit applications but the service isn't very friendly to web developers. Use "restbus" to access the NextBus XML feed over a RESTful JSON API.

Relevant to  REST, JSON, API, Node.js

Dynamic Gists Using MutationObservers and Promises

Published on by Morgan Ney

GitHub Gists are great for your blog if they're part of the initial payload before the browser's window has fired its onload event; just use the provided embed script. If you need to embed Gists into XHR loaded content, it's still possible with a bit more work. MutationObservers and Promises can make coordinating the retrieval of the Gist and its subsequent DOM placement a lot easier.

Relevant to  MutationObserver, Promise, Backbone, jQuery

Simple CSS3 Ribbons

Published on by Morgan Ney

Browsers have come a long way in their support for CSS3, in particular generated content of pseudo-elements ::before and ::after. Using these two pseudo-elements it's simple to create a CSS3 ribbon using only one HTML element.

Relevant to  HTML5, CSS3

Installing CouchDB 1.5 on AWS EC2 CentOS 6 (x86_64)

Published on by Morgan Ney

As of January 21st, 2014 the CentOS 6 EPEL repository only supports version 1.0.4 of CouchDB which is currently on version 1.5. Luckily, installing CouchDB from source on an Amazon EC2 instance running CentOS 6 is pretty simple.

Relevant to  CouchDB, EC2, CentOS

Using JavaScript Promises with Backbone.sync

Published on by Morgan Ney

Backbone is a great MVC library for JavaScript, but applications involving fetch() sequences can start to get ugly with deeply nested callbacks. Using ES6 Promises with Backbone can prevent ugly callback chains and make your code more understandable and flexible.

Relevant to  JavaScript, Backbone, Promise, jQuery

HTML Excerpt with PHP

Published on by Morgan Ney

HTML excerpts or previews of an article can reuse the CSS developed to style the entire article, making for a more eye-catching experience. However, it is not always clear how to prevent breaking your HTML when building the excerpt so most authors tend to deliver plain text excerpts. Using PHP's DOMDocument class, it's possible to create a valid HTML excerpt.

Relevant to  PHP, HTML, DOM
Get in Touch

BizzyBtyes is owned by Morgan Ney, a web developer from the San Francisco Bay Area.