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: Configure Git run: | git config --local user.email "actions@forgejo.plainrock127.xyz" git config --local user.name "IPTV Playlist Bot" - name: Setup Python Environment run: | echo "Setting up Python environment..." python3 --version echo "Setting up directories..." mkdir -p reports/daily reports/logs backups config echo "Setup completed" - name: Check Import File run: | echo "Checking import file..." if [ -f bulk_import.m3u ]; then LINES=$(wc -l < bulk_import.m3u) SIZE=$(du -h bulk_import.m3u | cut -f1) echo "Found bulk_import.m3u with $LINES lines ($SIZE)" # Show first few lines to verify content echo "First 5 lines:" head -n 5 bulk_import.m3u || echo "Could not read file" else echo "Creating empty bulk_import.m3u" echo '#EXTM3U' > bulk_import.m3u fi - name: Verify Scripts Directory run: | echo "Checking scripts directory..." if [ -d scripts ]; then echo "Scripts directory exists" ls -la scripts/ else echo "❌ Scripts directory not found!" exit 1 fi - name: Run Playlist Generation run: | echo "Running playlist generation..." cd scripts if [ -f generate_playlist.py ]; then echo "Found generate_playlist.py, executing..." python3 generate_playlist.py echo "Playlist generation completed with exit code: $?" else echo "❌ Error: generate_playlist.py not found in scripts/" exit 1 fi - name: Verify Generated Files run: | echo "Verifying generated files..." if [ -f playlist.m3u ]; then CHANNELS=$(grep -c "^#EXTINF" playlist.m3u || echo "0") SIZE=$(du -h playlist.m3u | cut -f1) echo "✅ Generated playlist.m3u with $CHANNELS channels ($SIZE)" else echo "⚠️ No playlist.m3u generated" fi if [ -f channels.txt ]; then SIZE=$(du -h channels.txt | cut -f1) echo "✅ Updated channels.txt ($SIZE)" else echo "⚠️ No channels.txt found" fi - name: Create Simple Report run: | echo "Creating report..." DATE=$(date +%Y%m%d_%H%M%S) REPORT="reports/daily/report_$DATE.md" echo "# IPTV Playlist Report" > "$REPORT" echo "Generated: $(date)" >> "$REPORT" echo "" >> "$REPORT" if [ -f playlist.m3u ]; then CHANNELS=$(grep -c "^#EXTINF" playlist.m3u || echo "0") echo "## Summary" >> "$REPORT" echo "- **Channels:** $CHANNELS" >> "$REPORT" echo "- **Generated:** $(date)" >> "$REPORT" echo "- **File size:** $(du -h playlist.m3u | cut -f1)" >> "$REPORT" # Add top 10 groups echo "" >> "$REPORT" echo "## Top Channel Groups" >> "$REPORT" if command -v python3 >/dev/null 2>&1; then python3 -c " import re try: with open('playlist.m3u', 'r') as f: content = f.read() groups = {} for line in content.split('\n'): if 'group-title=' in line: match = re.search(r'group-title=\"([^\"]*)', line) if match: group = match.group(1) groups[group] = groups.get(group, 0) + 1 for group, count in sorted(groups.items(), key=lambda x: x[1], reverse=True)[:10]: print(f'- **{group}:** {count} channels') except Exception as e: print(f'- Error analyzing groups: {e}') " >> "$REPORT" fi echo "Report created: $REPORT" else echo "**Status:** No playlist generated" >> "$REPORT" echo "⚠️ No playlist found for report" fi - name: Clean Old Reports run: | echo "Cleaning old reports..." cd reports/daily if ls *.md >/dev/null 2>&1; then COUNT=$(ls *.md | wc -l) echo "Found $COUNT reports" if [ "$COUNT" -gt 5 ]; then echo "Removing excess reports (keeping 5 most recent)..." ls -t *.md | tail -n +6 | xargs rm -f echo "Cleanup done" fi else echo "No reports to clean" fi cd ../.. - name: Reset Import File run: | echo "Resetting import file..." echo '#EXTM3U' > bulk_import.m3u echo "# Import file cleared after processing" >> bulk_import.m3u echo "# Add your M3U content here for next run" >> bulk_import.m3u echo "Import file reset and ready for next import" - name: Commit Changes run: | echo "Committing changes..." git add . if git diff --staged --quiet; then echo "No changes to commit" else CHANNELS="0" if [ -f playlist.m3u ]; then CHANNELS=$(grep -c "^#EXTINF" playlist.m3u || echo "0") fi # Create detailed commit message COMMIT_MSG="📺 Updated playlist with $CHANNELS channels - $(date '+%Y-%m-%d %H:%M')" if [ -f reports/daily/report_*.md ]; then LATEST_REPORT=$(ls -t reports/daily/report_*.md | head -n 1) if [ -f "$LATEST_REPORT" ]; then COMMIT_MSG="$COMMIT_MSG 📊 Latest Report: $(basename "$LATEST_REPORT") 🔄 Auto-processed via workflow" fi fi git commit -m "$COMMIT_MSG" git push echo "✅ Changes committed and pushed" fi - name: Summary run: | echo "=== WORKFLOW COMPLETE ===" if [ -f playlist.m3u ]; then CHANNELS=$(grep -c "^#EXTINF" playlist.m3u || echo "0") echo "✅ Playlist has $CHANNELS channels" # Show file sizes echo "📊 File sizes:" [ -f playlist.m3u ] && echo " - playlist.m3u: $(du -h playlist.m3u | cut -f1)" [ -f channels.txt ] && echo " - channels.txt: $(du -h channels.txt | cut -f1)" else echo "⚠️ No playlist generated" fi echo "✅ Reports updated" echo "✅ Repository cleaned" echo "✅ Ready for next import"