GIF 搜尋 (Tenor API)
直接透過 curl 使用 Tenor API 搜尋及下載 GIF。無需額外工具。
使用時機
適合尋找反應 GIF、建立視覺內容,以及在聊天中傳送 GIF。
設定
在環境變數中設定你的 Tenor API 金鑰(請加入 ${HERMES_HOME:-~/.hermes}/.env):
TENOR_API_KEY=your_key_here
前往 https://developers.google.com/tenor/guides/quickstart 取得免費 API 金鑰 — Google Cloud Console 的 Tenor API 金鑰完全免費,且有寬裕的使用限制。
先決條件
curl和jq(macOS/Linux 標準工具)TENOR_API_KEY環境變數
搜尋 GIF
# 搜尋並取得 GIF 網址
curl -s "https://tenor.googleapis.com/v2/search?q=thumbs+up&limit=5&key=${TENOR_API_KEY}" | jq -r '.results[].media_formats.gif.url'
# 取得較小/預覽版本
curl -s "https://tenor.googleapis.com/v2/search?q=nice+work&limit=3&key=${TENOR_API_KEY}" | jq -r '.results[].media_formats.tinygif.url'
下載 GIF
# 搜尋並下載第一個結果
URL=$(curl -s "https://tenor.googleapis.com/v2/search?q=celebration&limit=1&key=${TENOR_API_KEY}" | jq -r '.results[0].media_formats.gif.url')
curl -sL "$URL" -o celebration.gif
取得完整中繼資料
curl -s "https://tenor.googleapis.com/v2/search?q=cat&limit=3&key=${TENOR_API_KEY}" | jq '.results[] | {title: .title, url: .media_formats.gif.url, preview: .media_formats.tinygif.url, dimensions: .media_formats.gif.dims}'
API 參數
| 參數 | 說明 |
|---|---|
q | 搜尋查詢(空格用 + 編碼) |
limit | 最大結果數(1-50,預設 20) |
key | API 金鑰(來自 $TENOR_API_KEY 環境變數) |
media_filter | 篩選格式:gif, tinygif, mp4, tinymp4, webm |
contentfilter | 安全等級:off, low, medium, high |
locale | 語言:en_US, es, fr 等 |
可用媒體格式
每個結果在 .media_formats 下有多種格式:
| 格式 | 使用情境 |
|---|---|
gif | 完整品質 GIF |
tinygif | 小型預覽 GIF |
mp4 | 影片版本(檔案較小) |
tinymp4 | 小型預覽影片 |
webm | WebM 影片 |
nanogif | 極小縮圖 |
備註
- 查詢字串需進行 URL 編碼:空格用
+,特殊字元用%XX - 若要在聊天中傳送,
tinygif網址較為輕量 - GIF 網址可直接用於 Markdown:
