Help us understand the problem. What is going on with this article?

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

More than 5 years have passed since last update.

最近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はちょっと触ってみたけど。

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away