0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

GitHub ActionsでXとQiitaへの自動投稿を構築した話

0
Posted at

はじめに

個人開発アプリの情報発信を自動化するため、GitHub ActionsでX(Twitter)とQiitaへの定期投稿を実装しました。

X 自動投稿の仕組み

tweepy ライブラリを使って X API v2 で投稿します。

# scripts/twitter/post.py
import tweepy
import json
import random
from pathlib import Path

from posts import POSTS

def pick_post():
    history_path = Path(__file__).parent / "posted_history.json"
    history = json.loads(history_path.read_text()) if history_path.exists() else []
    unused = [i for i in range(len(POSTS)) if i not in history]
    if not unused:
        history, unused = [], list(range(len(POSTS)))
    idx = random.choice(unused)
    history.append(idx)
    history_path.write_text(json.dumps(history))
    return POSTS[idx]

def main():
    client = tweepy.Client(
        consumer_key=os.getenv("X_API_KEY"),
        consumer_secret=os.getenv("X_API_KEY_SECRET"),
        access_token=os.getenv("X_ACCESS_TOKEN"),
        access_token_secret=os.getenv("X_ACCESS_TOKEN_SECRET"),
    )
    client.create_tweet(text=pick_post())

GIF デモの自動録画・投稿

Playwright でアプリを自動操作してスクリーンショットを撮影し、GIF化して投稿します。

from playwright.sync_api import sync_playwright
from PIL import Image
import io

def record_demo():
    frames = []
    with sync_playwright() as pw:
        browser = pw.chromium.launch(headless=True)
        page = browser.new_page(viewport={"width": 1280, "height": 800})
        page.goto(APP_URL)
        page.wait_for_timeout(2000)
        frames.append(Image.open(io.BytesIO(page.screenshot())))
        # ...操作してスクリーンショット...
        browser.close()

    frames[0].save("demo.gif", save_all=True, append_images=frames[1:],
                   duration=600, loop=0)

GitHub Actions のワークフロー

# .github/workflows/tweet.yml
on:
  schedule:
    - cron: '0 23 * * *'   # 08:00 JST
    - cron: '0 12 * * *'   # 21:00 JST
  workflow_dispatch:

jobs:
  tweet:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with: { python-version: '3.11' }
      - run: pip install -r scripts/twitter/requirements.txt
      - run: playwright install chromium --with-deps
      - run: python scripts/twitter/post.py
        env:
          X_API_KEY: ${{ secrets.X_API_KEY }}
          # ...他のシークレット...
      - uses: stefanzweifel/git-auto-commit-action@v5
        with:
          commit_message: "ci: update post history"

Qiita 自動投稿

# .github/workflows/qiita.yml
on:
  schedule:
    - cron: '0 0 * * 1'  # 毎週月曜 09:00 JST
  workflow_dispatch:

jobs:
  qiita:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with: { python-version: '3.11' }
      - run: pip install python-dotenv
      - run: python scripts/qiita/post.py
        env:
          QIITA_TOKEN: ${{ secrets.QIITA_TOKEN }}
      - uses: stefanzweifel/git-auto-commit-action@v5
        with:
          commit_message: "ci: update qiita post history"

まとめ

GitHub Actions を使うことでPCを起動していなくても定期投稿が動き続けます。
投稿履歴はJSONでリポジトリに保存するため、重複投稿も防止できます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?