13
15

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.

PythonでChatWork APIとQiita APIを使ってみる

Last updated at Posted at 2014-10-05

最近JetBrains製品って学生なら無料でとれるということに気づいた。で、Python触ってみたいな〜って思ってたから、PyCharmとってみました。まあコミュニティエディションもあるんですが。

で、チャットワークとQiitaのAPI使ってみたのでメモ程度に。

(作りはちょっと雑かも)

ChatWork API

chatwork.py
#coding: UTF-8
print "Content-Type: application/json"

import pycurl
import urllib

c = pycurl.Curl()
api_token = 'Your API Key'
me = 'https://api.chatwork.com/v1/me'
status = 'https://api.chatwork.com/v1/my/status'
tasks = 'https://api.chatwork.com/v1/my/tasks'
room_message = 'https://api.chatwork.com/v1/rooms/{room_id}/messages'

# ===== GET =====
print "GET"
print "============="

c.setopt(pycurl.HTTPHEADER, ['X-ChatWorkToken:' + api_token])

print "----- 私 -----"
c.setopt(pycurl.URL, me)
c.perform()

print()
print "----- ステータス -----"
c.setopt(pycurl.URL, status)
c.perform()

print()
print "----- タスク -----"
c.setopt(pycurl.URL, 'https://api.chatwork.com/v1/my/tasks')
c.perform()

print()

# ===== POST =====

option = {'body': 'Python ChatWork API Test'}
c.setopt(pycurl.URL, room_message)
c.setopt(pycurl.HTTPHEADER, ['X-ChatWorkToken:' + api_token])
c.setopt(pycurl.POST, 1)
c.setopt(pycurl.POSTFIELDS, urllib.urlencode(option))
c.perform()

# アップデートは特に使う予定ないから書かない。

Qiita API

qiita.py
#coding: UTF-8
import pycurl
import urllib
import urllib2
import cookielib
import json

def get_user(username):
    """
    指定したユーザ情報を取得する
    :param username:
    :return:
    """
    url = "https://qiita.com/api/v1/users/" + username
    c = pycurl.Curl()
    c.setopt(pycurl.URL, url)
    c.perform()

def get_my_stock(token):
    """
    自分のストック投稿を取得する
    :return:
    """
    url = "https://qiita.com/api/v1/stocks" + "?token=" + token
    c = pycurl.Curl()
    c.setopt(pycurl.URL, url)
    c.perform()

# http is urllib2.HTTPHandler(debuglevel=1)
opener = urllib2.build_opener(urllib2.HTTPSHandler(debuglevel=1),
                          urllib2.HTTPCookieProcessor(cookielib.CookieJar()))
urllib2.install_opener(opener)

# 認証
login_post = {'url_name': 'Your User Name', 'password': 'Your Password'}
params = urllib.urlencode(login_post)
request = urllib2.Request('https://qiita.com/api/v1/auth', params)
request_read = urllib2.urlopen(request)
token = json.loads(request_read.read())
token = token['token']

# 自分のストック投稿を取得する
print get_my_stock(token)

# 自分の情報を取得する
print get_user('UserName')

Pythonのフレームワークって何がいいんだろう。。。
Pyramidはちょっと触ってみたけど。

13
15
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
13
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?