Update README.md #72
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Update Views Every Day Badge | |
on: | |
push: | |
branches: | |
- main # Этот workflow запускается при пуше в основную ветку | |
schedule: | |
- cron: '59 23 * * *' # Работает каждый день в 23:59 UTC | |
jobs: | |
fetch_traffic: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v2 | |
- name: Fetch traffic data from GitHub API | |
id: fetch_traffic | |
run: | | |
# Получаем данные о трафике с GitHub API, используя переменную ${{ github.repository }} | |
response=$(curl -H "Authorization: token ${{ secrets.PUBLIC_REPO_TOKEN }}" \ | |
-H "Accept: application/vnd.github.v3+json" \ | |
https://api.github.com/repos/${{ github.repository }}/traffic/views) | |
# Извлекаем данные за текущий день | |
today=$(date -I) # Получаем текущую дату в формате YYYY-MM-DD | |
today_data=$(echo "$response" | jq --arg today "$today" '.views[] | select(.timestamp | startswith($today))') | |
# Если данные за текущий день есть, извлекаем count и uniques | |
total_views=$(echo "$today_data" | jq '.count') | |
unique_views=$(echo "$today_data" | jq '.uniques') | |
echo "TOTAL_VIEWS=$total_views" >> $GITHUB_ENV | |
echo "UNIQUE_VIEWS=$unique_views" >> $GITHUB_ENV | |
- name: Read current values from README.md | |
id: read_values | |
run: | | |
# Извлекаем текущие значения Total Views и Unique Views из README.md | |
current_total=$(grep -oP 'Total%20Views%20All%20Time-\K\d+' README.md) | |
current_unique=$(grep -oP 'Unique%20Views%20All%20Time-\K\d+' README.md) | |
echo "CURRENT_TOTAL=$current_total" >> $GITHUB_ENV | |
echo "CURRENT_UNIQUE=$current_unique" >> $GITHUB_ENV | |
- name: Update badge in README.md | |
run: | | |
# Прибавляем новые значения к текущим | |
new_total_views=$((TOTAL_VIEWS + CURRENT_TOTAL)) | |
new_total_uniques=$((UNIQUE_VIEWS + CURRENT_UNIQUE)) | |
echo "${TOTAL_VIEWS}+${CURRENT_TOTAL}=${new_total_views}" | |
echo "${UNIQUE_VIEWS}+${CURRENT_UNIQUE}=${new_total_uniques}" | |
# Формируем URL для картинки с уникальными просмотрами | |
BADGE_TOTAL_URL="https://img.shields.io/badge/Total%20Views%20All%20Time-$new_total_views-blue" | |
BADGE_UNIQUE_URL="https://img.shields.io/badge/Unique%20Views%20All%20Time-$new_total_uniques-green" | |
echo ${BADGE_TOTAL_URL} | |
echo ${BADGE_UNIQUE_URL} | |
# Обновляем файл с новым значением | |
sed -i \ | |
-e "s|!\[Total Views All Time](.*)|![Total Views All Time](${BADGE_TOTAL_URL})|" \ | |
-e "s|!\[Unique Views All Time](.*)|![Unique Views All Time](${BADGE_UNIQUE_URL})|" \ | |
README.md | |
- name: Set git configuration | |
run: | | |
git config --global user.name "cortez24rus" | |
git config --global user.email "cortez24rus@gmail.com" | |
- name: Pull latest changes | |
run: | | |
git pull origin main || echo "No changes to pull" | |
- name: Commit and push changes to README.md | |
run: | | |
git add README.md | |
git commit -m "Update traffic data for $today in README.md" || echo "No changes to commit" | |
git push origin main |