-
Notifications
You must be signed in to change notification settings - Fork 0
92 lines (79 loc) · 3.72 KB
/
Copy pathfetch-x-feed.yml
File metadata and controls
92 lines (79 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
name: Fetch X media feed
on:
workflow_dispatch:
inputs:
username:
description: 'X username (without @). Defaults to pankun2000_'
required: false
default: 'pankun2000_'
schedule:
- cron: '0 0 * * *'
permissions:
contents: write
jobs:
fetch:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install jq
run: sudo apt-get update && sudo apt-get install -y jq
- name: Fetch recent tweets with media and generate JSON
env:
X_BEARER_TOKEN: ${{ secrets.X_BEARER_TOKEN }}
run: |
set -u
write_empty_feed() {
mkdir -p WebSite/assets
echo '[]' > WebSite/assets/x-feed.json
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add WebSite/assets/x-feed.json
git commit -m "Update x-feed.json (empty due to API error)" || echo "No changes to commit"
git push || echo "git push failed (push may be restricted)"
}
if [ -z "${X_BEARER_TOKEN:-}" ]; then
echo "X_BEARER_TOKEN secret is not set. Exiting (no-op)."
write_empty_feed
exit 0
fi
USERNAME="${{ github.event.inputs.username }}"
if [ -z "$USERNAME" ]; then USERNAME="pankun2000_"; fi
echo "Using username: $USERNAME"
AUTH_HEADER="Authorization: Bearer ${X_BEARER_TOKEN}"
USER_JSON=$(curl --silent --show-error --fail-with-body -H "$AUTH_HEADER" "https://api.twitter.com/2/users/by/username/$USERNAME" || true)
USER_ID=$(echo "$USER_JSON" | jq -r '.data.id // empty' 2>/dev/null || true)
if [ -z "$USER_ID" ]; then
echo "Could not resolve user id for ${USERNAME}. API response:"
echo "$USER_JSON"
echo "Writing empty feed and exiting successfully."
write_empty_feed
exit 0
fi
TWEETS_JSON=$(curl --silent --show-error --fail-with-body -H "$AUTH_HEADER" "https://api.twitter.com/2/users/$USER_ID/tweets?max_results=100&expansions=attachments.media_keys&tweet.fields=created_at,text" || true)
if ! echo "$TWEETS_JSON" | jq -e '.data != null' >/dev/null 2>&1; then
echo "Unable to fetch tweet list for ${USERNAME}; writing empty feed."
echo "$TWEETS_JSON"
write_empty_feed
exit 0
fi
IDS=$(echo "$TWEETS_JSON" | jq -r '.data[]? | select(.attachments != null) | .id' || true)
if [ -z "$IDS" ]; then
IDS=$(echo "$TWEETS_JSON" | jq -r '.data[]? | select(.text | test("pic\\.twitter\\.com")) | .id' || true)
fi
OUTPUT='[]'
for id in $IDS; do
URL="https://x.com/$USERNAME/status/$id"
OEMBED_JSON=$(curl --silent --show-error --max-time 20 "https://publish.twitter.com/oembed?url=$URL&dnt=true" || true)
OEMBED_HTML=$(echo "$OEMBED_JSON" | jq -r '.html // empty' 2>/dev/null || true)
if [ -n "$OEMBED_HTML" ]; then
OUTPUT=$(echo "$OUTPUT" | jq --arg url "$URL" --arg html "$OEMBED_HTML" '. + [ { url: $url, oembed_html: $html } ]')
fi
done
OUTPUT=$(echo "$OUTPUT" | jq '.[0:3]' 2>/dev/null || echo '[]')
mkdir -p WebSite/assets
echo "$OUTPUT" > WebSite/assets/x-feed.json
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add WebSite/assets/x-feed.json
git commit -m "Update x-feed.json (fetched media tweets)" || echo "No changes to commit"
git push || echo "git push failed (push may be restricted)"