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: Smart Report Management run: | echo "Managing reports..." 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)" # Clean old reports (keep only 3 most recent) if [ -d reports/daily ]; then cd reports/daily REPORT_COUNT=$(ls -1 playlist_report_*.md report_*.md 2>/dev/null | wc -l) if [ "$REPORT_COUNT" -gt 3 ]; then 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/') mkdir -p "../archive/$REPORT_DATE" mv "$old_report" "../archive/$REPORT_DATE/" echo "Archived: $old_report" done fi cd - > /dev/null fi # Create new report DATE=$(date +%Y%m%d_%H%M%S) REPORT="reports/daily/playlist_report_$DATE.md" COUNTRIES=$(grep 'group-title=' playlist.m3u | sed 's/.*group-title="//; s/".*//' | sort -u | wc -l || echo "0") echo "# IPTV Playlist Report - $(date)" > "$REPORT" echo "" >> "$REPORT" echo "## Summary" >> "$REPORT" echo "- **Channels**: $CHANNELS" >> "$REPORT" echo "- **Countries**: $COUNTRIES" >> "$REPORT" echo "- **Size**: $SIZE" >> "$REPORT" echo "- **Generated**: $(date)" >> "$REPORT" echo "" >> "$REPORT" echo "## Top Countries" >> "$REPORT" grep 'group-title=' playlist.m3u | sed 's/.*group-title="//; s/".*//' | sort | uniq -c | sort -nr | head -10 >> "$REPORT" 2>/dev/null || echo "No country data" >> "$REPORT" echo "" >> "$REPORT" echo "---" >> "$REPORT" echo "*Auto-generated report with smart management*" >> "$REPORT" echo "Created report: $REPORT" else echo "No playlist generated" fi - name: Archive Old Reports run: | echo "Managing archives..." # Archive reports older than 7 days CUTOFF_DATE=$(date -d '7 days ago' +%s 2>/dev/null || date -v-7d +%s 2>/dev/null || echo "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/') mkdir -p "reports/archive/$REPORT_DATE" mv "$report_file" "reports/archive/$REPORT_DATE/" echo "Archived old report: $BASENAME" fi fi done # Show archive status 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 status: $ACTIVE_REPORTS active, $ARCHIVED_REPORTS archived" - 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 ACTIVE_REPORTS=$(ls reports/daily/playlist_report_*.md report_*.md 2>/dev/null | wc -l) git commit -m "📺 Updated playlist: $CHANNELS channels across $COUNTRIES countries - Reports: $ACTIVE_REPORTS/3 active - $(date '+%Y-%m-%d %H:%M')" 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 "Reports: $ACTIVE_REPORTS active, $ARCHIVED_REPORTS archived" echo "Repository is clean and organized with smart report management!"