{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import requests\n", "import pprint" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "with open('data/temp/qiita_access_token.txt') as f:\n", " qiita_access_token = f.read().strip()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "headers = {'Authorization': 'Bearer {}'.format(qiita_access_token)}" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "url_items = 'https://qiita.com/api/v2/items'" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "item_data = {\n", " 'title': 'テスト記事',\n", " 'body': 'テスト',\n", " 'private': True,\n", " 'tags': [{'name': 'test'}],\n", " 'coediting': False,\n", " 'gist': False,\n", " 'tweet': False\n", "}" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "r_post = requests.post(url_items, headers=headers, json=item_data)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "201\n" ] } ], "source": [ "print(r_post.status_code)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'body': 'テスト\\n',\n", " 'coediting': False,\n", " 'comments_count': 0,\n", " 'created_at': '2018-07-12T22:05:19+09:00',\n", " 'group': None,\n", " 'id': '93ead2568150009de5f1',\n", " 'likes_count': 0,\n", " 'page_views_count': None,\n", " 'private': True,\n", " 'reactions_count': 0,\n", " 'rendered_body': '

テスト

\\n',\n", " 'tags': [{'name': 'test', 'versions': []}],\n", " 'title': 'テスト記事',\n", " 'updated_at': '2018-07-12T22:05:19+09:00',\n", " 'url': 'https://qiita.com/nkmk/private/93ead2568150009de5f1',\n", " 'user': {'description': '',\n", " 'facebook_id': '',\n", " 'followees_count': 0,\n", " 'followers_count': 0,\n", " 'github_login_name': None,\n", " 'id': 'nkmk',\n", " 'items_count': 0,\n", " 'linkedin_id': '',\n", " 'location': '',\n", " 'name': '',\n", " 'organization': '',\n", " 'permanent_id': 53096,\n", " 'profile_image_url': 'https://qiita-image-store.s3.amazonaws.com/0/53096/profile-images/1473692950',\n", " 'twitter_screen_name': None,\n", " 'website_url': ''}}\n" ] } ], "source": [ "pprint.pprint(r_post.json())" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "93ead2568150009de5f1\n" ] } ], "source": [ "item_id = r_post.json()['id']\n", "print(item_id)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "r_get = requests.get(url_items + '/' + item_id, headers=headers)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "200\n" ] } ], "source": [ "print(r_get.status_code)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'body': 'テスト\\n',\n", " 'coediting': False,\n", " 'comments_count': 0,\n", " 'created_at': '2018-07-12T22:05:19+09:00',\n", " 'group': None,\n", " 'id': '93ead2568150009de5f1',\n", " 'likes_count': 0,\n", " 'page_views_count': 0,\n", " 'private': True,\n", " 'reactions_count': 0,\n", " 'rendered_body': '

テスト

\\n',\n", " 'tags': [{'name': 'test', 'versions': []}],\n", " 'title': 'テスト記事',\n", " 'updated_at': '2018-07-12T22:05:19+09:00',\n", " 'url': 'https://qiita.com/nkmk/private/93ead2568150009de5f1',\n", " 'user': {'description': '',\n", " 'facebook_id': '',\n", " 'followees_count': 0,\n", " 'followers_count': 0,\n", " 'github_login_name': None,\n", " 'id': 'nkmk',\n", " 'items_count': 0,\n", " 'linkedin_id': '',\n", " 'location': '',\n", " 'name': '',\n", " 'organization': '',\n", " 'permanent_id': 53096,\n", " 'profile_image_url': 'https://qiita-image-store.s3.amazonaws.com/0/53096/profile-images/1473692950',\n", " 'twitter_screen_name': None,\n", " 'website_url': ''}}\n" ] } ], "source": [ "pprint.pprint(r_get.json())" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'title': 'テスト記事更新', 'body': 'テスト', 'private': True, 'tags': [{'name': 'test'}], 'coediting': False, 'gist': False, 'tweet': False}\n" ] } ], "source": [ "item_data_updated = item_data.copy()\n", "item_data_updated['title'] = 'テスト記事更新'\n", "print(item_data_updated)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "r_patch = requests.patch(url_items + '/' + item_id, headers=headers, json=item_data_updated)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "200\n" ] } ], "source": [ "print(r_patch.status_code)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'body': 'テスト\\n',\n", " 'coediting': False,\n", " 'comments_count': 0,\n", " 'created_at': '2018-07-12T22:05:19+09:00',\n", " 'group': None,\n", " 'id': '93ead2568150009de5f1',\n", " 'likes_count': 0,\n", " 'page_views_count': None,\n", " 'private': True,\n", " 'reactions_count': 0,\n", " 'rendered_body': '

テスト

\\n',\n", " 'tags': [{'name': 'test', 'versions': []}],\n", " 'title': 'テスト記事更新',\n", " 'updated_at': '2018-07-12T22:05:19+09:00',\n", " 'url': 'https://qiita.com/nkmk/private/93ead2568150009de5f1',\n", " 'user': {'description': '',\n", " 'facebook_id': '',\n", " 'followees_count': 0,\n", " 'followers_count': 0,\n", " 'github_login_name': None,\n", " 'id': 'nkmk',\n", " 'items_count': 0,\n", " 'linkedin_id': '',\n", " 'location': '',\n", " 'name': '',\n", " 'organization': '',\n", " 'permanent_id': 53096,\n", " 'profile_image_url': 'https://qiita-image-store.s3.amazonaws.com/0/53096/profile-images/1473692950',\n", " 'twitter_screen_name': None,\n", " 'website_url': ''}}\n" ] } ], "source": [ "pprint.pprint(r_patch.json())" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "r_delete = requests.delete(url_items + '/' + item_id, headers=headers)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "204\n" ] } ], "source": [ "print(r_delete.status_code)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "url_tag = 'https://qiita.com/api/v2/tags/{}/following'" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "tag = 'python'" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "r_put = requests.put(url_tag.format(tag), headers=headers)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "204\n" ] } ], "source": [ "print(r_put.status_code)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "r_delete_tag = requests.delete(url_tag.format(tag), headers=headers)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "204\n" ] } ], "source": [ "print(r_delete_tag.status_code)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.0" } }, "nbformat": 4, "nbformat_minor": 2 }