🖥

#Qiita #API で記事を新規作成する #python スクリプトの例 ( #JSON で標準入力を受け取って複数個同時作成 )

2019/04/16に公開

what

  • JSON配列で複数のオブジェクトを渡す
  • オブジェクトで title / body / tag を指定する
  • private や twitter への投稿なども、記事ごとに指定できるように
  • tag の指定はやや階層が複雑なので注意

script

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

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

import os, sys, requests, json

posts = json.loads(sys.stdin.read())

results = []

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

headers = {
 'Authorization': 'Bearer {}'.format(token),
 'Content-Type': 'application/json',
}
 
api_url = 'https://qiita.com/api/v2/items'

for post in posts:

  if post.get('private'):
    private = True
  else:
    private = False

  if post.get('tweet'):
    tweet = True
  else:
    tweet = False

  item = {
      'title': post['title'],
      'body': post['body'],
      "coediting": False,
      'tags': post['tags'] if 'tags' in post else [{ "name": "test", "versions": [] }],
      'private': private,
      'tweet': tweet,
  }
 
  response = requests.post(api_url, headers=headers, json=item)
  
  results.append(response.json())
 
print(json.dumps(results))


exe

$ echo '[{"title":"text", "body": "test\\n\\ntest", "private": true, "tags" : [ {"name":"wo", "versions": []} ] }, {"title":"text", "body": "test", "private": true }]' | QIITA_TOKEN=$(cat ~/.secret/qiita-token) ./item-post-json.py | jq .
[
  {
    "rendered_body": "<p>test</p>\n\n<p>test</p>\n",
    "body": "test\n\ntest\n",
    "coediting": false,
    "comments_count": 0,
    "created_at": "2019-04-15T22:59:45+09:00",
    "group": null,
    "id": "bf91b70405783b1a2ccf",
    "likes_count": 0,
    "private": true,
    "reactions_count": 0,
    "tags": [
      {
        "name": "wo",
        "versions": []
      }
    ],
    "title": "text",
    "updated_at": "2019-04-15T22:59:45+09:00",
    "url": "https://qiita.com/YumaInaura/private/bf91b70405783b1a2ccf",
    "user": {
      "description": "https://www.wantedly.com/users/93140896 /  Ruby on Rails 業務経験 約4年 / Perl PHP Python Golang Linux Apache MySQL BigQuery Jenkins ansible など /  いなうらゆうま / YumaInaura / 稲浦悠馬",
      "facebook_id": "yumainaura",
      "followees_count": 184,
      "followers_count": 164,
      "github_login_name": "YumaInaura",
      "id": "YumaInaura",
      "items_count": 1241,
      "linkedin_id": "",
      "location": "Osaka",
      "name": "Inaura いなうら 稲浦 Yuma ゆうま 悠馬",
      "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
  },
  {
    "rendered_body": "<p>test</p>\n",
    "body": "test\n",
    "coediting": false,
    "comments_count": 0,
    "created_at": "2019-04-15T22:59:45+09:00",
    "group": null,
    "id": "08e1d783e4f5a50fa814",
    "likes_count": 0,
    "private": true,
    "reactions_count": 0,
    "tags": [
      {
        "name": "test",
        "versions": []
      }
    ],
    "title": "text",
    "updated_at": "2019-04-15T22:59:45+09:00",
    "url": "https://qiita.com/YumaInaura/private/08e1d783e4f5a50fa814",
    "user": {
      "description": "https://www.wantedly.com/users/93140896 /  Ruby on Rails 業務経験 約4年 / Perl PHP Python Golang Linux Apache MySQL BigQuery Jenkins ansible など /  いなうらゆうま / YumaInaura / 稲浦悠馬",
      "facebook_id": "yumainaura",
      "followees_count": 184,
      "followers_count": 164,
      "github_login_name": "YumaInaura",
      "id": "YumaInaura",
      "items_count": 1241,
      "linkedin_id": "",
      "location": "Osaka",
      "name": "Inaura いなうら 稲浦 Yuma ゆうま 悠馬",
      "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
  }
]

image

image

Original by Github issue

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

チャットメンバー募集

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

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

Twitter

https://twitter.com/YumaInaura

公開日時

2019-04-16

Discussion