<commit_before>﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;

namespace TwitchLeecher.Core.Models
{
    public class VodPlaylist : List<VodPlaylistPart>
    {
        #region Static Methods

        public static VodPlaylist Parse(string tempDir, string playlistStr, string urlPrefix)
        {
            VodPlaylist playlist = new VodPlaylist();

            List<string> lines = playlistStr.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries).ToList();

            int partCounter = 0;

            for (int i = 0; i < lines.Count; i++)
            {
                string line = lines[i];

                if (line.StartsWith("#EXTINF", StringComparison.OrdinalIgnoreCase))
                {
                    double length = Math.Max(double.Parse(line.Substring(line.LastIndexOf(":") + 1).TrimEnd(','), NumberStyles.Any, CultureInfo.InvariantCulture), 0);

                    playlist.Add(new VodPlaylistPart(length, urlPrefix + lines[i + 1], Path.Combine(tempDir, partCounter.ToString("D8") + ".ts")));
                    partCounter++;
                    i++;
                }
            }

            return playlist;
        }

        #endregion Static Methods<commit_msg>Issue #514 - support for m3u8 version 4<commit_after>﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;

namespace TwitchLeecher.Core.Models
{
    public class VodPlaylist : List<VodPlaylistPart>
    {
        #region Static Methods

        public static VodPlaylist Parse(string tempDir, string playlistStr, string urlPrefix)
        {
            VodPlaylist playlist = new VodPlaylist();

            List<string> lines = playlistStr.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries).ToList();

            for (int i = 0; i < lines.Count; i++)
            {
                string line = lines[i];

                if (line.StartsWith("#EXT-X-VERSION", StringComparison.OrdinalIgnoreCase))
                {
                    int version = int.Parse(line.Substring(line.LastIndexOf(":") + 1).TrimEnd(), NumberStyles.Integer, CultureInfo.InvariantCulture);

                    if (version == 4)
                    {
                        return ParseV4(tempDir, lines, urlPrefix);
                    }
                    else
                    {
                        return ParseV3(tempDir, lines, urlPrefix);
                    }
                }
            }

            return playlist;
        }

        private static VodPlaylist ParseV3(string tempDir, List<string> lines, string urlPrefix)
        {
            VodPlaylist playlist = new VodPlaylist();

            int partCounter = 0;

            for (int i = 0; i < lines.Count; i++)
            {
                string line = lines[i];

                if (line.StartsWith("#EXTINF", StringComparison.OrdinalIgnoreCase))
                {
                    double length = Math.Max(double.Parse(line.Substring(line.LastIndexOf(":") + 1).TrimEnd(','), NumberStyles.Any, CultureInfo.InvariantCulture), 0);

                    playlist.Add(new VodPlaylistPart(length, urlPrefix + lines[i + 1], Path.Combine(tempDir, partCounter.ToString("D8") + ".ts")));
                    partCounter++;
                    i++;
                }
            }

            return playlist;
        }

        private static VodPlaylist ParseV4(string tempDir, List<string> lines, string urlPrefix)
        {
            VodPlaylist playlist = new VodPlaylist();

            int partCounter = 0;
            double lengthBuffer = 0;
            string currentPartStr = null;

            for (int i = 0; i < lines.Count; i++)
            {
                string line = lines[i];

                if (line.StartsWith("#EXTINF", StringComparison.OrdinalIgnoreCase))
                {
                    string partStr = lines[i + 2];

                    if (string.IsNullOrWhiteSpace(currentPartStr))
                    {
                        currentPartStr = partStr;
                    }

                    if (!currentPartStr.Equals(partStr))
                    {
                        playlist.Add(new VodPlaylistPart(lengthBuffer, urlPrefix + currentPartStr, Path.Combine(tempDir, partCounter.ToString("D8") + ".ts")));
                        currentPartStr = partStr;
                        lengthBuffer = 0;
                        partCounter++;
                    }

                    lengthBuffer += Math.Max(double.Parse(line.Substring(line.LastIndexOf(":") + 1).TrimEnd(','), NumberStyles.Any, CultureInfo.InvariantCulture), 0);

                    i++;
                }
            }

            if (!string.IsNullOrWhiteSpace(currentPartStr) && lengthBuffer > 0)
            {
                playlist.Add(new VodPlaylistPart(lengthBuffer, urlPrefix + currentPartStr, Path.Combine(tempDir, partCounter.ToString("D8") + ".ts")));
            }

            return playlist;
        }

        #endregion Static Methods
{'fraction_non_alphanumeric': 0.1045481393975192, 'fraction_numerical': 0.00689112029927151, 'mean_word_length': 1.6541274817136886, 'pattern_counts': {'":': 4, '<': 13, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7483082', 'n_tokens_mistral': 1395, 'n_tokens_neox': 1293, 'n_words': 280}