4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Qiita APIを触ってみた

Last updated at Posted at 2014-10-19

Qiitaの投稿とtwitterを連携しようと思い,Qiita APIにちょっと触れてみた.今回はさくっとやりたいため,pythonラッパーを使用した.

今回やろうとしたのは,以下の2点

  • 定期的に投稿させるため,一定期間更新がない場合に通知
  • 一定期間ごとの自分の行動の統計を取る

このための下準備として,まず一定期間ごとの自分の投稿の取得を試してみた.

ラッパーの使い方は基本的には上記のリンクページに書いてあり,詳しく知りたい人はpipで落としてきたコードを参照.
今回の操作には必要ないが,一応Oauth認証から投稿リスト取得までは以下の通り.

client = Client(url_name = self.user_name, password = self.user_pass)
token = client.token # post等はここで取得したtokenを使用
users = Users()
user_items = users.user_items(url_name=self.user_name, params={'page':1, 'per_page':100})

返ってくる値の形式は公式documentを参照.ここで欲しい値は投稿日時の'created_at'なので,抽出はuser_items[0]['created_at']みたいにできる.取得できる形式は2014-10-11 23:34:14 +0900の様になり,日付・時間の比較のためにはこれをdatetimeの形式に変換する必要がある.この際,最後についているタイムゾーンが邪魔なので削りつつ変換すると以下の様になる.

In [42]: time_str = '2014-10-11 23:34:14 +0900'

In [43]: time = datetime.strptime(time_str[:-6], '%Y-%m-%d %H:%M:%S')

In [44]: (datetime.now() - time).days
Out[44]: 7

こんな感じでデータの抽出をしていけばいけそうな感じ.
現状は本当に導入部分だけど,早めに形にしたい.

追記

daysの差は日付ではなく,24時間単位の切り捨てになるため,一日以内の場合はdays<1で良さそう.

4
5
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
4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?