Qiita投稿のストック数・コメント数・ツイート数・はてぶ数・いいね数・ページビュー数を取得する



## ストック数・コメント数・ツイート数・はてぶ数・いいね数を取得する

```php:php
<?php

const USERNAME = "suin";
const COUNT = 100;

error_reporting(-1);
ini_set('display_errors', 1);

$filename = 'https://qiita.com/api/v1/users/'.USERNAME.'/items?per_page='.COUNT;

$contents = file_get_contents($filename);

$items = json_decode($contents, 1);

$items = array_map(function (array $item) {
    return [
        'url' => $item['url'],
        'title' => $item['title'],
        'created_at' => (new DateTime($item['created_at']))->format('Y-m-d'),
			'stock_count' => $item['stock_count'],
			'comment_count' => $item['comment_count'],
			'content_length' => mb_strlen($item['raw_body'], 'UTF-8'),
    ];
}, $items);

usort($items, function ($item1, $item2) {
    $created1 = new DateTime($item1['created_at']);
    $created2 = new DateTime($item2['created_at']);

    return ($created1 >= $created2) ? 1 : 0;
});

echo "created: ", date('Y-m-d H:i:s'), PHP_EOL;

echo implode("\t", [
	"date",
	"stocks",
	"comments",
	"hatena",
	"tweets",
	"likes",
	"chars",
	"url",
	"title",
]), PHP_EOL;

foreach ($items as $item) {
	$fb = 0;
	$hateb = 0;
	$tweets = 0;
	$fb = get_fb($item['url']);
	$hateb = get_hatena_count($item['url']);
	$tweets = get_tweets($item['url']);
	
	echo implode("\t", [
		$item['created_at'],
		$item['stock_count'],
		$item['comment_count'],
		$hateb,
		$tweets,
		$fb,
		$item['content_length'],
		$item['url'],
		$item['title'],
	]), PHP_EOL;
}

function get_hatena_count($url)
{
	$url = 'http://api.b.st-hatena.com/entry.count?url='.urlencode($url);
	return file_get_contents($url);
}

function get_tweets($url)
{
	$url = 'http://urls.api.twitter.com/1/urls/count.json?url='.urlencode($url);
	$json = file_get_contents($url);
	return json_decode($json)->count;
}

function get_fb($url)
{
	$url = 'http://graph.facebook.com/?id='.($url);
	$json = file_get_contents($url);
	$data = array_merge(['shares' => 0], json_decode($json, true));
	return $data['shares'];
}

```

# ページビューを取得する

PVはAPIで取れないので、QiitaにログインしChromeのインスペクタ上で実行する

```coffeescript:coffeescript
username = "suin"
count = 100

getQiitaItems = (username) ->
	console.log(1)
	$.ajax
		url: "https://qiita.com/api/v1/users/#{username}/items?per_page=#{count}"
		success: (items)->
			getPV(items)
		dataType: "json"

getPV = (items, result = "") ->
	if items.length is 0
		console.log("Result:" + result)
		return

	item = items.shift()

	console.log("fetch(#{items.length}): #{item.url}...")
	$.get item.url, (data)->
		pv = $(data).find('li.view-count').text().trim().split(/\s/)[0]
		result = result + "\n" + item.url + "\t" + pv
		getPV(items, result) 

getQiitaItems(username)
```

↓ コンパイル後JS

```js:js
// Generated by CoffeeScript 1.6.3
(function() {
  var count, getPV, getQiitaItems, username;

  username = "suin";

  count = 100;

  getQiitaItems = function(username) {
    console.log(1);
    return $.ajax({
      url: "https://qiita.com/api/v1/users/" + username + "/items?per_page=" + count,
      success: function(items) {
        return getPV(items);
      },
      dataType: "json"
    });
  };

  getPV = function(items, result) {
    var item;
    if (result == null) {
      result = "";
    }
    if (items.length === 0) {
      console.log("Result:" + result);
      return;
    }
    item = items.shift();
    console.log("fetch(" + items.length + "): " + item.url + "...");
    return $.get(item.url, function(data) {
      var pv;
      pv = $(data).find('li.view-count').text().trim().split(/\s/)[0];
      result = result + "\n" + item.url + "\t" + pv;
      return getPV(items, result);
    });
  };

  getQiitaItems(username);

}).call(this);
```