#!/usr/bin/env python3 import os import sys import requests import webbrowser
API = os.environ.get(“GITHUB_API”, “https://api.github.com”)
def read_clipboard() -> str: try: import pyperclip # pip install pyperclip return str(pyperclip.paste() or “”) except Exception: pass
import tkinter as tk
r = tk.Tk()
r.withdraw()
try:
return str(r.clipboard_get() or "")
finally:
r.destroy()
def non_empty_line_count(text: str) -> int: # 空行(空白のみも含む)を除外して行数カウント return sum(1 for line in text.splitlines() if line.strip() != “”)
def gh_headers(token: str) -> dict: return { “Authorization”: f”token {token}”, “Accept”: “application/vnd.github+json”, }
def main() -> int: token = (os.environ.get(“GITHUB_TOKEN”) or “”).strip() if not token: print(“ERROR: Set GITHUB_TOKEN env (GitHub PAT with ‘gist’ permission).”, file=sys.stderr) return 2
content = read_clipboard()
if not content:
print("ERROR: Clipboard is empty.", file=sys.stderr)
return 3
cnt = non_empty_line_count(content)
min_lines = 4
if cnt < min_lines:
print(
f"ABORTED: Clipboard content is too short ({cnt} non-empty line(s)). "
f"Potentially sensitive (e.g., password/API key). "
f"Use --force to override.",
file=sys.stderr,
)
return 4
payload = {
"public": True,
"files": {"gist.md": {"content": content}},
}
resp = requests.post(
f"{API}/gists",
headers=gh_headers(token),
json=payload,
timeout=30,
)
if resp.status_code not in (200, 201):
raise RuntimeError(f"GitHub API error: {resp.status_code} {resp.text}")
url = resp.json()["html_url"]
print(url)
# 既定ブラウザで開く(関連付けに従う)
webbrowser.open(url, new=2) # new=2: 新しいタブ(可能なら)
return 0
if name == “main”: raise SystemExit(main())