見出し画像

【Note自動投稿】PythonからNoteの記事を投稿するライブラリを開発した話

お久しぶりンゴ、なおくんです。

今年の4月、無事に私立のFラン大学に入学し大学生になりました!!

今回はNoteの非公式ライブラリを開発したので紹介します。

開発の経緯

私はClaude2とChatGPTを使ってブログの自動運用テストを行っております。ブログはレンタルサーバーを借りてwordpressで構築しています。

運用にあたって作成した記事の宣伝をするためにnoteに集客用の記事を投稿したかったのですが、自動運用が目標なのでnoteで記事を書くことすらも自動化する必要がありました。

そこで、noteの公式APIを調べましたが記事を投稿できる機能はなく、非公式のライブラリやAPIも調べましたが見つからなかったので、自分で作ることにしました。(もしかしたら調べたときに見落として元々あったのかも…)

ライブラリの機能

現在(2023/10/5)の機能は記事の投稿のみです。

投稿する記事の内容はテキストファイルにマークダウン記法で記述します。使用できる構文は大見出し・小見出し・箇条書き・段落番号・区切り線のみです。今後のアップデートで目次の挿入や太字の変換などを実装したいと思います。

また記事のタイトル、アイキャッチ画像、タグを指定することができます。アイキャッチ画像はローカル(パソコンなど)に入ってる画像をアップロードする処理を実装できなかったので、すでに用意されているおすすめの写真から検索してアイキャッチ画像を挿入する仕様になっています。

アイキャッチ画像の検索も指定することができます。

インストール方法

pip install NoteClient

使い方

サンプルコード

from Note_Client import Note

EMAIL = 'your email'
PASSWORD = 'your password'
USER_ID = 'your user_id'

TITLE = 'Sample'
CONTENT_PATH = 'content.txt'
TAG_LIST = ['sample_tag']

# > If an image is specified, the index number is entered; if not, no description is given.
# INDEX = 0

# > True if the article is to be published, False if the article is to be saved as a draft; if not specified, the article is saved as a draft.
# POST_SETTING = True

# > True if the execution screen is not displayed, False if it is displayed, or not displayed if not specified.
# HEADLESS = False

# To specify the above three options, add them to the function arguments.

note = Note(email=EMAIL, password=PASSWORD, user_id=USER_ID)
print(note.create_article(title=TITLE, file_name=CONTENT_PATH, input_tag_list=TAG_LIST))

テキストファイル(content.txt)

Insert your opening greeting here
## Major Heading
### Minor Heading
## How to Use Bullet Points
Write the content for heading 1 here.
You can also write bullet points like this.

- Item 1
- Item 2
- Item 3
## How to Use Paragraph Numbers
You can also write paragraph numbers like this.

1. Paragraph 1
2. Paragraph 2
3. Paragraph 3
## How to Use Horizontal Lines
By using horizontal lines
---
You can separate the content of the text like this.

実行結果

## If successful(Public).
# {'run':'success','title':'Sample','file_path':'content.txt','tag_list':['sample_tag'],'post_setting':'Public','post_url':'https://note.com/USER_ID/n/abc123'}

## If successful(Draft).
# {'run':'success','title':'Sample','file_path':'content.txt','tag_list':['sample_tag'],'post_setting':'Draft'}

## If unsuccessful.
# 'Required data is missing.'

ソースコードの書き方

from Note_Client import Note

EMAIL = 'your email'
PASSWORD = 'your password'
USER_ID = 'your user_id'

TITLE = 'Sample'
CONTENT_PATH = 'content.txt'
TAG_LIST = ['sample_tag']

# アイキャッチ画像を指定する場合はインデックス番号を記述する
INDEX = 0

# 記事を公開する場合はTrue、下書き保存する場合はFalse、初期値では下書き保存する
POST_SETTING = True

# 実行画面を表示しない場合はTrue、表示する場合はFalse、初期値では表示しない
HEADLESS = False

note = Note(email=EMAIL, password=PASSWORD, user_id=USER_ID)
print(note.create_article(title=TITLE, file_name=CONTENT_PATH, input_tag_list=TAG_LIST, image_index=INDEX, post_setting=POST_SETTING, headless=HEADLESS))

テキストファイルの書き方

テキストファイルはマークダウン記法で記述します。書き方はこんな感じ。

★大見出し★

## 大見出しの内容

★小見出し★

### 小見出しの内容

★箇条書き★

- 項目
- 項目
- 項目

★段落番号★

1. 項目1
2. 項目2
3. 項目3

★区切り線★

---

PyPIはこちら

GitHubはこちら

ライブラリのソースコードはGitHubにアップロードしています。


この記事が気に入ったらサポートをしてみませんか?