LoginSignup
9
9

More than 5 years have passed since last update.

Qiita Contributions API を Google Cloud Functions に作ってみた

Last updated at Posted at 2017-04-13

Qiita Contributions API を Google Cloud Functions に作ってみた

Qiita にはちゃんと API が用意されています。Qiita API v2

が、なぜかユーザー詳細を取得しても、その中には Contributions 数が含まれていません。含まれない理由はわかりませんが、この数値を入手するには Qiita からスクレイピングする必要があります。

せっかく Qiita API を使って何かを作っても、Contributions を取得するためにWebからスクレイピングするのは興ざめです。ですので今回、Google Cloud Functions を使って Contributions を取得する API を作成してみようと思います。

完成品

$ curl https://us-central1-charismatic-web-161410.cloudfunctions.net/qiita-contributions-api?user=takeo-asai
{
  "id": "takeo-asai",
  "contributions": "303"
}

?user= で取得したい Qiita ユーザーIDを指定するだけです。

ソースコード

https://github.com/takeo-asai/qiita-contributions-api

index.js
const cheerio = require('cheerio');

const request = require('request');

function qiitaContributions(user) {
    return new Promise((resolve, reject) => {
        request('http://qiita.com/' + user, (error, response, body) => {
            if (error || response.statusCode !== 200) reject(response);
            const $ = cheerio.load(body)
            const contributions = $('span.userActivityChart_statCount');
            contributions.each((i, element) => {
                if (i === 1) resolve(element.children[0].data);
            });
            resolve(0);
        });
    });
}

exports.qiitaContributions = (request, response) => {
    const user = request.query.user;
    qiitaContributions(user).then(contributions => {
        const json = {
            id: user,
            contributions: contributions,
        };
        response.send(JSON.stringify(json));
    }).catch(error => {
        response.send(JSON.stringify({}));
    });
};
package.json
{
  "name": "qiita-contributions-api",
  "version": "1.0.0",
  "description": "Qiita Contributions Api",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node index.js"
  },
  "keywords": [
    "node",
    "qiita"
  ],
  "author": "takeo-asai",
  "license": "MIT",
  "devDependencies": {
    "webpack": "^2.3.3"
  },
  "dependencies": {
    "cheerio": "^0.22.0",
    "request": "^2.81.0"
  }
}

Google Cloud Functions へ Deploy する

Google Cloud Source を使ったり、zip で固めてアップロードしたりと色々な方法でデプロイできますが、今回は量が少ないのでインラインエディタに直接コピペする方法で行います。

Google Cloud Console から Google Cloud Functions を選択。
新規に関数を作成します。

create-function.png

  1. トリガーを http へ変更する
    • API に URL が紐付けられます
  2. ソースコードをインラインエディタへコピペします
    • index.js へ上記の JavaScript のコードを
    • package.json へ上記の JSON をコピペします
  3. ステージバケットを適当に作成して選択します
    • ソースコードと Log が保存されます
  4. 実行する関数を exports.xxx で指定した qiitaContributions へ変更する

edit-functions.png

これだけです。簡単ですね。

ローカルでテストする

cloud-functions-emulator という Google謹製のローカル用テストツールが存在します。

インストールすると functions というコマンドが使えるようになるのですが、zsh を使っていると組み込み関数と衝突してしまうので、これを使う時だけは bash を使ったりするなど配慮が必要です。

9
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
9