|
| 1 | +name: YouTube Auto-Sync |
| 2 | + |
| 3 | +on: |
| 4 | + schedule: |
| 5 | + # Verifie les nouvelles videos tous les jours a 10h UTC (12h CEST) |
| 6 | + - cron: '0 10 * * *' |
| 7 | + workflow_dispatch: |
| 8 | + |
| 9 | +permissions: |
| 10 | + contents: write |
| 11 | + |
| 12 | +jobs: |
| 13 | + youtube-sync: |
| 14 | + name: Sync dernieres videos YouTube |
| 15 | + runs-on: ubuntu-latest |
| 16 | + |
| 17 | + steps: |
| 18 | + - name: Checkout |
| 19 | + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 |
| 20 | + with: |
| 21 | + ref: gh-pages |
| 22 | + |
| 23 | + - name: Setup Python |
| 24 | + uses: actions/setup-python@v6 |
| 25 | + with: |
| 26 | + python-version: '3.11' |
| 27 | + |
| 28 | + - name: Installer les dependances |
| 29 | + run: pip install requests |
| 30 | + |
| 31 | + - name: Creer le dossier data si necessaire |
| 32 | + run: mkdir -p assets/data |
| 33 | + |
| 34 | + - name: Recuperer les dernieres videos YouTube |
| 35 | + env: |
| 36 | + YOUTUBE_API_KEY: ${{ secrets.YOUTUBE_API_KEY }} |
| 37 | + shell: bash |
| 38 | + run: | |
| 39 | + python <<'PY' |
| 40 | + import requests |
| 41 | + import json |
| 42 | + import os |
| 43 | +
|
| 44 | + api_key = os.environ.get('YOUTUBE_API_KEY', '') |
| 45 | + if not api_key: |
| 46 | + print('YOUTUBE_API_KEY non configure - abandon') |
| 47 | + exit(0) |
| 48 | +
|
| 49 | + channel_handle = 'DavidKRKofficial' |
| 50 | +
|
| 51 | + search_url = ( |
| 52 | + 'https://www.googleapis.com/youtube/v3/search' |
| 53 | + '?key=' + api_key |
| 54 | + + '&q=' + channel_handle |
| 55 | + + '&type=channel&part=snippet&maxResults=1' |
| 56 | + ) |
| 57 | + r = requests.get(search_url, timeout=30) |
| 58 | + data = r.json() |
| 59 | +
|
| 60 | + if 'items' not in data or len(data['items']) == 0: |
| 61 | + print('Erreur: chaine YouTube introuvable') |
| 62 | + print(data) |
| 63 | + exit(1) |
| 64 | +
|
| 65 | + channel_id = data['items'][0]['snippet']['channelId'] |
| 66 | + print('Channel ID trouve: ' + channel_id) |
| 67 | +
|
| 68 | + videos_url = ( |
| 69 | + 'https://www.googleapis.com/youtube/v3/search' |
| 70 | + '?key=' + api_key |
| 71 | + + '&channelId=' + channel_id |
| 72 | + + '&part=snippet,id&order=date&maxResults=6&type=video' |
| 73 | + ) |
| 74 | + r2 = requests.get(videos_url, timeout=30) |
| 75 | + videos = r2.json() |
| 76 | +
|
| 77 | + if 'items' not in videos: |
| 78 | + print('Erreur: impossible de recuperer les videos') |
| 79 | + print(videos) |
| 80 | + exit(1) |
| 81 | +
|
| 82 | + video_list = [] |
| 83 | + for item in videos['items']: |
| 84 | + video_id = item['id']['videoId'] |
| 85 | + title = item['snippet']['title'] |
| 86 | + thumb = item['snippet']['thumbnails']['high']['url'] |
| 87 | + published = item['snippet']['publishedAt'][:10] |
| 88 | + video_list.append({ |
| 89 | + 'id': video_id, |
| 90 | + 'title': title, |
| 91 | + 'thumbnail': thumb, |
| 92 | + 'published': published, |
| 93 | + 'url': 'https://www.youtube.com/watch?v=' + video_id |
| 94 | + }) |
| 95 | +
|
| 96 | + with open('assets/data/youtube-videos.json', 'w', encoding='utf-8') as f: |
| 97 | + json.dump(video_list, f, ensure_ascii=False, indent=2) |
| 98 | +
|
| 99 | + print(str(len(video_list)) + ' videos sauvegardees dans assets/data/youtube-videos.json') |
| 100 | + for v in video_list: |
| 101 | + print(' - [' + v['published'] + '] ' + v['title']) |
| 102 | + PY |
| 103 | +
|
| 104 | + - name: Commit et push si nouvelles videos |
| 105 | + run: | |
| 106 | + git config user.name "github-actions[bot]" |
| 107 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 108 | + git add assets/data/youtube-videos.json |
| 109 | + if git diff --staged --quiet; then |
| 110 | + echo "Aucune nouvelle video detectee." |
| 111 | + else |
| 112 | + git commit -m "Auto-sync: mise a jour des videos YouTube $(date +%Y-%m-%d)" |
| 113 | + git push |
| 114 | + echo "Videos mises a jour et poussees !" |
| 115 | + fi |
0 commit comments