]name: Generate M3U Playlist with Auto-Organization on: push: branches: - main workflow_dispatch: jobs: build-and-organize: runs-on: ubuntu-22.04 steps: - name: Checkout Repository uses: actions/checkout@v4 - name: Set up Python uses: actions/setup-python@v5 with: python-version: '3.11' - name: Configure Git run: | git config --local user.email "actions@forgejo.plainrock127.xyz" git config --local user.name "IPTV Playlist Bot" - name: Run Comprehensive Cleanup run: | echo "Running comprehensive cleanup..." if [ -f comprehensive_cleanup.py ]; then python comprehensive_cleanup.py echo "Comprehensive cleanup completed" else echo "Cleanup already completed" fi - name: Basic Repository Maintenance run: | echo "Running basic maintenance..." find . -type d -name "__pycache__" -exec rm -rf {} + 2>/dev/null || true find . -name "*.pyc" -delete 2>/dev/null || true find . -name "*.tmp" -delete 2>/dev/null || true find . -name "*~" -delete 2>/dev/null || true mkdir -p reports/logs reports/daily reports/archive backups config templates find . -maxdepth 1 -name "*.log" -exec mv {} reports/logs/ \; 2>/dev/null || true if [ ! -f scripts/__init__.py ]; then echo '# IPTV Scripts Package' > scripts/__init__.py fi echo "Basic maintenance completed" - name: Check Import File run: | echo "Checking import file..." if [ -f bulk_import.m3u ]; then LINES=$(wc -l < bulk_import.m3u) echo "Found bulk_import.m3u with $LINES lines" if [ "$LINES" -gt 2 ]; then echo "Contains channels to process" fi else echo "Creating empty bulk_import.m3u" echo '#EXTM3U' > bulk_import.m3u fi - name: Run Playlist Generation run: | echo "Running playlist generation..." if [ -f scripts/generate_playlist.py ]; then python scripts/generate_playlist.py echo "Playlist generation completed" else echo "Error: generate_playlist.py not found" exit 1 fi - name: Enhanced Results Analysis and Report Management run: | echo "=== Enhanced Results Analysis with Smart Report Management ===" if [ -f playlist.m3u ]; then CHANNELS=$(grep -c "^#EXTINF" playlist.m3u || echo "0") SIZE=$(du -h playlist.m3u | cut -f1) echo "โœ… Generated playlist: $CHANNELS channels ($SIZE)" # Show country distribution echo "๐ŸŒ Top countries:" if grep -q 'group-title=' playlist.m3u; then grep 'group-title=' playlist.m3u | \ sed 's/.*group-title="//; s/".*//' | \ sort | uniq -c | sort -nr | head -10 | \ while read count country; do echo " $country: $count channels" done else echo " No country grouping found" fi # ============ SMART REPORT MANAGEMENT ============ echo "=== Smart Report Management ===" # Auto-cleanup duplicate reports (keep only 3 most recent) echo "๐Ÿงน Managing report count..." if [ -d reports/daily ]; then cd reports/daily REPORT_COUNT=$(ls -1 playlist_report_*.md report_*.md 2>/dev/null | wc -l) echo " Found $REPORT_COUNT existing reports" if [ "$REPORT_COUNT" -gt 3 ]; then # Keep only 3 most recent, archive the rest ls -t playlist_report_*.md report_*.md 2>/dev/null | tail -n +4 | while read old_report; do REPORT_DATE=$(echo "$old_report" | sed 's/.*_//; s/\.md//; s/\(....\)\(..\)\(..\).*/\1-\2/') ARCHIVE_DIR="../archive/$REPORT_DATE" mkdir -p "$ARCHIVE_DIR" mv "$old_report" "$ARCHIVE_DIR/" echo " ๐Ÿ“š Archived: $old_report โ†’ archive/$REPORT_DATE/" done REMAINING=$(ls -1 playlist_report_*.md report_*.md 2>/dev/null | wc -l) echo " โœ… Kept $REMAINING most recent reports" else echo " โœ… Report count ($REPORT_COUNT) is optimal" fi cd - > /dev/null fi # Auto-archive old reports (older than 7 days) echo "๐Ÿ—„๏ธ Auto-archiving old reports..." CUTOFF_DATE=$(date -d '7 days ago' +%s 2>/dev/null || date -v-7d +%s 2>/dev/null || echo "0") ARCHIVED_COUNT=0 for report_file in reports/daily/playlist_report_*.md reports/daily/report_*.md; do if [ -f "$report_file" ]; then FILE_DATE=$(stat -c %Y "$report_file" 2>/dev/null || stat -f %m "$report_file" 2>/dev/null || echo "0") if [ "$FILE_DATE" -lt "$CUTOFF_DATE" ] && [ "$FILE_DATE" -gt "0" ]; then BASENAME=$(basename "$report_file") REPORT_DATE=$(echo "$BASENAME" | sed 's/.*_//; s/\.md//; s/\(....\)\(..\)\(..\).*/\1-\2/') ARCHIVE_DIR="reports/archive/$REPORT_DATE" mkdir -p "$ARCHIVE_DIR" mv "$report_file" "$ARCHIVE_DIR/" ARCHIVED_COUNT=$((ARCHIVED_COUNT + 1)) echo " ๐Ÿ“š Auto-archived: $BASENAME โ†’ archive/$REPORT_DATE/" fi fi done if [ "$ARCHIVED_COUNT" -gt 0 ]; then echo " โœ… Auto-archived $ARCHIVED_COUNT old reports" else echo " โœ… No old reports to archive" fi # Create enhanced daily report DATE=$(date +%Y%m%d_%H%M%S) REPORT="reports/daily/playlist_report_$DATE.md" # Get countries count and repository stats COUNTRIES_COUNT=$(grep 'group-title=' playlist.m3u | sed 's/.*group-title="//; s/".*//' | sort -u | wc -l 2>/dev/null || echo "0") TOTAL_FILES=$(find . -type f -not -path './.git/*' | wc -l) REPO_SIZE=$(du -sh . 2>/dev/null | cut -f1 || echo "unknown") # Calculate quality score CACHE_DIRS=$(find . -type d -name "__pycache__" | wc -l) TEMP_FILES=$(find . -name "*.tmp" -o -name "*_temp*" | wc -l) LOG_FILES_ROOT=$(find . -maxdepth 1 -name "*.log" | wc -l) TOTAL_ISSUES=$((CACHE_DIRS + TEMP_FILES + LOG_FILES_ROOT)) if [ "$TOTAL_ISSUES" -eq 0 ]; then QUALITY_SCORE="100/100" QUALITY_STATUS="๐Ÿ† Pristine" elif [ "$TOTAL_ISSUES" -le 2 ]; then QUALITY_SCORE="90/100" QUALITY_STATUS="โœ… Excellent" else QUALITY_SCORE="75/100" QUALITY_STATUS="๐Ÿงน Good" fi cat > "$REPORT" << EOF # ๐Ÿ“บ IPTV Playlist Report - $(date '+%Y-%m-%d %H:%M') ## ๐Ÿ“Š Summary - **Total Channels**: $CHANNELS - **Countries/Groups**: $COUNTRIES_COUNT - **File Size**: $SIZE - **Generated**: $(date '+%Y-%m-%d %H:%M:%S UTC') - **Repository Status**: $QUALITY_STATUS ($QUALITY_SCORE) ## ๐ŸŒ Country Distribution \`\`\` $(grep 'group-title=' playlist.m3u | sed 's/.*group-title="//; s/".*//' | sort | uniq -c | sort -nr | head -15 || echo "No country data available") \`\`\` ## ๐Ÿ“ˆ Repository Health - **Total Files**: $TOTAL_FILES - **Repository Size**: $REPO_SIZE - **Quality Score**: $QUALITY_SCORE - **Organization**: Professional โœ… - **Cleanup**: Automated โœ… - **Reports**: Auto-managed โœ… - **Archives**: Auto-organized โœ… ## ๐Ÿ“‹ Report Management Status - **Active Reports**: $(ls reports/daily/playlist_report_*.md report_*.md 2>/dev/null | wc -l)/3 (auto-managed) - **Archive Status**: Auto-archiving enabled (7+ days) - **Log Management**: Centralized in reports/logs/ - **Cleanup Status**: Automated on every run ## ๐Ÿ—‚๏ธ Archive Summary $(if [ -d reports/archive ]; then echo "- **Archive Folders**: $(find reports/archive -type d -mindepth 1 | wc -l)" echo "- **Archived Reports**: $(find reports/archive -name "*.md" | wc -l)" else echo "- **Archive**: No archived reports yet" fi) ## ๐Ÿš€ Next Steps 1. Add new channels to \`bulk_import.m3u\` 2. Push changes to trigger automatic processing 3. Reports will be auto-managed (keep 3 recent, archive old) 4. Check \`reports/archive/YYYY-MM/\` for historical reports --- *Generated automatically by IPTV Playlist Bot with Smart Report Management* EOF echo "๐Ÿ“‹ Created enhanced report: $REPORT" # Report management summary ACTIVE_REPORTS=$(ls reports/daily/playlist_report_*.md report_*.md 2>/dev/null | wc -l) ARCHIVED_REPORTS=$(find reports/archive -name "*.md" 2>/dev/null | wc -l) echo "๐Ÿ“Š Report Management Summary:" echo " ๐Ÿ“‹ Active reports: $ACTIVE_REPORTS/3" echo " ๐Ÿ“š Archived reports: $ARCHIVED_REPORTS" else echo "โŒ No playlist generated" if [ -f reports/logs/playlist_update.log ]; then echo "๐Ÿ“‹ Last few log entries:" tail -5 reports/logs/playlist_update.log fi fi - name: Advanced Archive Management run: | echo "=== Advanced Archive Management ===" # Archive very old reports (older than 30 days) into monthly folders echo "๐Ÿ“š Managing long-term archives..." CUTOFF_DATE=$(date -d '30 days ago' +%s 2>/dev/null || date -v-30d +%s 2>/dev/null || echo "0") ARCHIVED_COUNT=0 for report_file in reports/daily/playlist_report_*.md reports/daily/report_*.md; do if [ -f "$report_file" ]; then FILE_DATE=$(stat -c %Y "$report_file" 2>/dev/null || stat -f %m "$report_file" 2>/dev/null || echo "0") if [ "$FILE_DATE" -lt "$CUTOFF_DATE" ] && [ "$FILE_DATE" -gt "0" ]; then BASENAME=$(basename "$report_file") REPORT_DATE=$(echo "$BASENAME" | sed 's/.*_//; s/\.md//; s/\(....\)\(..\)\(..\).*/\1-\2/') ARCHIVE_DIR="reports/archive/$REPORT_DATE" mkdir -p "$ARCHIVE_DIR" mv "$report_file" "$ARCHIVE_DIR/" ARCHIVED_COUNT=$((ARCHIVED_COUNT + 1)) echo " ๐Ÿ“š Long-term archived: $BASENAME โ†’ archive/$REPORT_DATE/" fi fi done if [ "$ARCHIVED_COUNT" -gt 0 ]; then echo " โœ… Long-term archived $ARCHIVED_COUNT reports" else echo " โœ… No reports need long-term archiving" fi # Compress very old archives (older than 90 days) echo "๐Ÿ—œ๏ธ Compressing very old archives..." COMPRESSED_COUNT=0 CUTOFF_DATE=$(date -d '90 days ago' +%s 2>/dev/null || date -v-90d +%s 2>/dev/null || echo "0") for archive_dir in reports/archive/*/; do if [ -d "$archive_dir" ]; then DIR_DATE=$(basename "$archive_dir") DIR_TIMESTAMP=$(date -d "${DIR_DATE}-01" +%s 2>/dev/null || date -j -f "%Y-%m-%d" "${DIR_DATE}-01" +%s 2>/dev/null || echo "0") if [ "$DIR_TIMESTAMP" -lt "$CUTOFF_DATE" ] && [ "$DIR_TIMESTAMP" -gt "0" ]; then REPORT_COUNT=$(find "$archive_dir" -name "*.md" | wc -l) if [ "$REPORT_COUNT" -gt 0 ]; then SUMMARY_FILE="reports/archive/${DIR_DATE}_summary.md" cat > "$SUMMARY_FILE" << EOF # Monthly Archive Summary - $DIR_DATE ## Summary - **Month**: $DIR_DATE - **Reports Archived**: $REPORT_COUNT - **Compressed**: $(date '+%Y-%m-%d') ## Reports in Archive $(find "$archive_dir" -name "*.md" -exec basename {} \; | sort) --- *Compressed archive summary* EOF rm -rf "$archive_dir" COMPRESSED_COUNT=$((COMPRESSED_COUNT + 1)) echo " ๐Ÿ—œ๏ธ Compressed: $DIR_DATE ($REPORT_COUNT reports โ†’ summary)" fi fi fi done if [ "$COMPRESSED_COUNT" -gt 0 ]; then echo " โœ… Compressed $COMPRESSED_COUNT old archive folders" else echo " โœ… No archives need compression yet" fi # Final archive summary ACTIVE_REPORTS=$(ls reports/daily/playlist_report_*.md report_*.md 2>/dev/null | wc -l) ARCHIVE_FOLDERS=$(find reports/archive -type d -mindepth 1 2>/dev/null | wc -l) ARCHIVE_REPORTS=$(find reports/archive -name "*.md" 2>/dev/null | wc -l) echo "๐Ÿ“Š Final Archive Status:" echo " ๐Ÿ“‹ Active reports: $ACTIVE_REPORTS (max 3)" echo " ๐Ÿ“ Archive folders: $ARCHIVE_FOLDERS" echo " ๐Ÿ“š Archived reports: $ARCHIVE_REPORTS" - name: Clean Import File run: | echo "Cleaning import file..." if [ -f bulk_import.m3u ]; then LINES=$(wc -l < bulk_import.m3u) if [ "$LINES" -gt 2 ]; then echo '#EXTM3U' > bulk_import.m3u echo "Cleaned import file for next use" fi fi - name: Final Cleanup run: | echo "Final cleanup..." find . -name "*.pyc" -delete 2>/dev/null || true find . -name "*~" -delete 2>/dev/null || true find . -name "*.swp" -delete 2>/dev/null || true rm -f comprehensive_cleanup.py 2>/dev/null || true echo "Final cleanup completed" - name: Commit Changes run: | echo "Committing changes..." git add . if ! git diff --staged --quiet; then CHANNELS="0" COUNTRIES="0" if [ -f playlist.m3u ]; then CHANNELS=$(grep -c "^#EXTINF" playlist.m3u || echo "0") COUNTRIES=$(grep 'group-title=' playlist.m3u | sed 's/.*group-title="//; s/".*//' | sort -u | wc -l || echo "0") fi REPO_SIZE=$(du -sh . 2>/dev/null | cut -f1 || echo "unknown") ACTIVE_REPORTS=$(ls reports/daily/playlist_report_*.md report_*.md 2>/dev/null | wc -l) COMMIT_MSG="๐Ÿ“บ Updated playlist: $CHANNELS channels across $COUNTRIES countries ๐ŸŽฏ Repository Status: ๐Ÿ“บ Channels: $CHANNELS ๐ŸŒ Countries: $COUNTRIES ๐Ÿ“ Size: $REPO_SIZE ๐Ÿ“‹ Active Reports: $ACTIVE_REPORTS/3 ๐Ÿ—‚๏ธ Archive: Auto-managed ๐Ÿงน Status: Clean & Organized ๐Ÿš€ Auto-maintained with smart report management Generated: $(date '+%Y-%m-%d %H:%M UTC')" git commit -m "$COMMIT_MSG" git push echo "โœ… Changes committed successfully" else echo "โ„น๏ธ No changes to commit" fi - name: Success Summary run: | echo "=== ๐ŸŽ‰ Workflow Completed Successfully! ===" if [ -f playlist.m3u ]; then CHANNELS=$(grep -c "^#EXTINF" playlist.m3u || echo "0") COUNTRIES=$(grep 'group-title=' playlist.m3u | sed 's/.*group-title="//; s/".*//' | sort -u | wc -l || echo "0") echo "๐Ÿ“บ Playlist: $CHANNELS channels across $COUNTRIES countries" fi ACTIVE_REPORTS=$(ls reports/daily/playlist_report_*.md report_*.md 2>/dev/null | wc -l) ARCHIVED_REPORTS=$(find reports/archive -name "*.md" 2>/dev/null | wc -l) echo "๐Ÿ“Š Report Management:" echo " ๐Ÿ“‹ Active: $ACTIVE_REPORTS/3 reports" echo " ๐Ÿ“š Archived: $ARCHIVED_REPORTS reports" echo " ๐ŸŽฏ Status: Fully automated" echo "" echo "๐Ÿš€ Repository is clean, organized, and ready for next import!"