🖥

#pyhon で #Qiita #API を叩いて 記事作成する簡単な汎用スクリプトの例

2019/04/15に公開

#pyhon で #Qiita #API を叩いて 記事作成する簡単な汎用スクリプトの例

python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# https://qiita.com/api/v2/docs#item

import os, sys, requests, json

if len(sys.argv) >= 3:
  qiita_tags = []

  for tag in sys.argv[3].split(','):
    # ほぼ使われていないであろう versions 指定も必須のようなので、空のリストを指定しておく
    qiita_tags.append({ "name": tag, "versions": [] })
else:
  qiita_tags = [{ "name": "test", "versions": [] }]

item = {
    'title': sys.argv[1],
    'body': sys.argv[2],
    "coediting": False,
    'tags': qiita_tags,
    'private': False,
    'tweet': False,
}

token = os.environ.get('QIITA_TOKEN')

headers = {
 'Authorization': 'Bearer {}'.format(token),
 'Content-Type': 'application/json',
}

api_url = 'https://qiita.com/api/v2/items'
response = requests.post(api_url, headers=headers, json=item)

if response.status_code == 200:
  print(json.dumps(response.json()))
else:
  print(json.dumps(response.json()))

exe

  • 第一引数がタイトル
  • 第二引数がbody
  • 第三引数にタグをカンマ区切りで指定
$ QIITA_TOKEN=$(cat ~/.secret/qiita-token) ./item-post.py test "testing\na" a,b,c
{"rendered_body": "<p>testing\\na</p>\n", "body": "testing\\na\n", "coediting": false, "comments_count": 0, "created_at": "2019-04-15T19:25:48+09:00", "group": null, "id": "b5603eeb7e91cfb93248", "likes_count": 0, "private": true, "reactions_count": 0, "tags": [{"name": "C", "versions": []}, {"name": "a", "versions": []}, {"name": "b", "versions": []}], "title": "test", "updated_at": "2019-04-15T19:25:48+09:00", "url": "https://qiita.com/YumaInaura/private/b5603eeb7e91cfb93248", "user": {"description": "https://www.wantedly.com/users/93140896 /  Ruby on Rails \u696d\u52d9\u7d4c\u9a13 \u7d044\u5e74 / Perl PHP Python Golang Linux Apache MySQL BigQuery Jenkins ansible \u306a\u3069 /  \u3044\u306a\u3046\u3089\u3086\u3046\u307e / YumaInaura / \u7a32\u6d66\u60a0\u99ac", "facebook_id": "yumainaura", "followees_count": 184, "followers_count": 164, "github_login_name": "YumaInaura", "id": "YumaInaura", "items_count": 1239, "linkedin_id": "", "location": "Osaka", "name": "Inaura \u3044\u306a\u3046\u3089 \u7a32\u6d66 Yuma \u3086\u3046\u307e \u60a0\u99ac", "organization": "", "permanent_id": 89618, "profile_image_url": "https://qiita-image-store.s3.amazonaws.com/0/89618/profile-images/1546214964", "team_only": false, "twitter_screen_name": "YumaInaura", "website_url": "http://twitter.com/yumainaura"}, "page_views_count": null}

Original by Github issue

https://github.com/YumaInaura/YumaInaura/issues/1294

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

https://line.me/ti/g2/eEPltQ6Tzh3pYAZV8JXKZqc7PJ6L0rpm573dcQ

Twitter

https://twitter.com/YumaInaura

公開日時

2019-04-15

Discussion