<commit_before>﻿using System.Net.Mime;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Channels;
using MediaBrowser.Controller.Drawing;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Channels;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Web;

namespace MediaBrowser.Plugins.SoundCloud
{
    public class SoundCloudChannel : IChannel
    {
        private readonly IHttpClient _httpClient;
        private readonly ILogger _logger;
        private readonly IJsonSerializer _jsonSerializer;

        public SoundCloudChannel(IHttpClient httpClient, IJsonSerializer jsonSerializer, ILogManager logManager)
        {
            _httpClient = httpClient;
            _logger = logManager.GetLogger(GetType().Name);
            _jsonSerializer = jsonSerializer;
        }

        public string DataVersion
        {
            get
            {
                // Increment as needed to invalidate all caches
                return "4";
            }
        }

        public bool IsEnabledFor(Controller.Entities.User user)
        {
            return true;
        }

        public async Task<IEnumerable<ChannelItemInfo>> Search(ChannelSearchInfo searchInfo, Controller.Entities.User user, CancellationToken cancellationToken)
        {
            return null;
        }

        public async Task<ChannelItemResult> GetChannelItems(InternalChannelItemQuery query, CancellationToken cancellationToken)
        {
            IEnumerable<ChannelItemInfo> items;

            if (query.FolderId == null)
            {
                items = await GetChannels(cancellationToken).ConfigureAwait(false);
            }
            else
            {
                var catSplit = query.FolderId.Split('_');
                query.FolderId = catSplit[1];
                items = await GetTracks(query, cancellationToken).ConfigureAwait(false);
            }

            return new ChannelItemResult
            {
                Items = items.ToList(),
                CacheLength = TimeSpan.FromDays(3)
            };
        }

        private async Task<IEnumerable<ChannelItemInfo>> GetChannels(CancellationToken cancellationToken)
        {
            return new List<ChannelItemInfo>
            {
                new ChannelItemInfo
                {
                    Name = "Hot",
                    Id = "cat_hot",
                    Type = ChannelItemType.Folder
                },
                new ChannelItemInfo
                {
                    Name = "Latest",
                    Id = "cat_latest",
                    Type = ChannelItemType.Folder
                }
            };
        }

        private async Task<IEnumerable<ChannelItemInfo>> GetTracks(InternalChannelItemQuery query, CancellationToken cancellationToken)
        {
            var downloader = new SoundCloudListingDownloader(_logger, _jsonSerializer, _httpClient);
            var songs = await downloader.GetTrackList(query, cancellationToken)
                .ConfigureAwait(false);

            return songs.Select(i => new ChannelItemInfo
            {
                ContentType = ChannelMediaContentType.Song,
                ImageUrl = i.artwork_url,
                IsInfiniteStream = false,
                MediaType = ChannelMediaType.Audio,
                Name = i.title,
                Type = ChannelItemType.Media,
                Id = i.id.ToString(),
                RunTimeTicks = TimeSpan.FromMilliseconds(i.duration).Ticks,

                MediaSources = new List<ChannelMediaInfo>
                {
                    new ChannelMediaInfo
                    {
                        Path = i.stream_url + "?client_id=78fd88dde7ebf8fdcad08106f6d56ab6"
                    }
                }
            });
        }

        public Task<DynamicImageResponse> GetChannelImage(ImageType type, CancellationToken cancellationToken)
        {
            switch (type)
            {
                case ImageType.Thumb:
                case ImageType.Backdrop:
                    {
                        var path = GetType().Namespace + ".Images." + type.ToString().ToLower() + ".png";

                        return Task.FromResult(new DynamicImageResponse
                        {
                            Format = ImageFormat.Png,
                            HasImage = true,
                            Stream = GetType().Assembly.GetManifestResourceStream(path)
                        });
                    }
                default:
                    throw new ArgumentException("Unsupported image type: " + type);
            }
        }

        public IEnumerable<ImageType> GetSupportedChannelImages()
        {
            return new List<ImageType>
            {
                ImageType.Thumb,
                ImageType.Primary,
                ImageType.Backdrop
            };
        }

        public string Name
        {
            get { return "SoundCloud"; }
        }



        public ChannelFeatures GetChannelFeatures()
        {
            return new ChannelFeatures
            {
                CanSearch = false,

                ContentTypes = new List<ChannelMediaContentType>
                 {
                     ChannelMediaContentType.Song
                 },

                MediaTypes = new List<ChannelMediaType>
                  {
                       ChannelMediaType.Audio
                  },
            };
        }

        public Task<ChannelItemResult> GetAllMedia(InternalAllChannelItemsQuery query, CancellationToken cancellationToken)
        {
            // Unsupported by this channel
            throw new NotImplementedException();
        }

        public string HomePageUrl
        {
            get { return "http://www.soundcloud.com/"; }
        }<commit_msg>Add Max page size for soundcloud<commit_after>﻿using System.Net.Mime;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Channels;
using MediaBrowser.Controller.Drawing;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Channels;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Web;

namespace MediaBrowser.Plugins.SoundCloud
{
    public class SoundCloudChannel : IChannel
    {
        private readonly IHttpClient _httpClient;
        private readonly ILogger _logger;
        private readonly IJsonSerializer _jsonSerializer;

        public SoundCloudChannel(IHttpClient httpClient, IJsonSerializer jsonSerializer, ILogManager logManager)
        {
            _httpClient = httpClient;
            _logger = logManager.GetLogger(GetType().Name);
            _jsonSerializer = jsonSerializer;
        }

        public string DataVersion
        {
            get
            {
                // Increment as needed to invalidate all caches
                return "4";
            }
        }

        public bool IsEnabledFor(Controller.Entities.User user)
        {
            return true;
        }

        public async Task<IEnumerable<ChannelItemInfo>> Search(ChannelSearchInfo searchInfo, Controller.Entities.User user, CancellationToken cancellationToken)
        {
            return null;
        }

        public async Task<ChannelItemResult> GetChannelItems(InternalChannelItemQuery query, CancellationToken cancellationToken)
        {
            IEnumerable<ChannelItemInfo> items;

            if (query.FolderId == null)
            {
                items = await GetChannels(cancellationToken).ConfigureAwait(false);
            }
            else
            {
                var catSplit = query.FolderId.Split('_');
                query.FolderId = catSplit[1];
                items = await GetTracks(query, cancellationToken).ConfigureAwait(false);
            }

            return new ChannelItemResult
            {
                Items = items.ToList(),
                CacheLength = TimeSpan.FromDays(3)
            };
        }

        private async Task<IEnumerable<ChannelItemInfo>> GetChannels(CancellationToken cancellationToken)
        {
            return new List<ChannelItemInfo>
            {
                new ChannelItemInfo
                {
                    Name = "Hot",
                    Id = "cat_hot",
                    Type = ChannelItemType.Folder
                },
                new ChannelItemInfo
                {
                    Name = "Latest",
                    Id = "cat_latest",
                    Type = ChannelItemType.Folder
                }
            };
        }

        private async Task<IEnumerable<ChannelItemInfo>> GetTracks(InternalChannelItemQuery query, CancellationToken cancellationToken)
        {
            var downloader = new SoundCloudListingDownloader(_logger, _jsonSerializer, _httpClient);
            var songs = await downloader.GetTrackList(query, cancellationToken)
                .ConfigureAwait(false);

            return songs.Select(i => new ChannelItemInfo
            {
                ContentType = ChannelMediaContentType.Song,
                ImageUrl = i.artwork_url,
                IsInfiniteStream = false,
                MediaType = ChannelMediaType.Audio,
                Name = i.title,
                Type = ChannelItemType.Media,
                Id = i.id.ToString(),
                RunTimeTicks = TimeSpan.FromMilliseconds(i.duration).Ticks,

                MediaSources = new List<ChannelMediaInfo>
                {
                    new ChannelMediaInfo
                    {
                        Path = i.stream_url + "?client_id=78fd88dde7ebf8fdcad08106f6d56ab6"
                    }
                }
            });
        }

        public Task<DynamicImageResponse> GetChannelImage(ImageType type, CancellationToken cancellationToken)
        {
            switch (type)
            {
                case ImageType.Thumb:
                case ImageType.Backdrop:
                    {
                        var path = GetType().Namespace + ".Images." + type.ToString().ToLower() + ".png";

                        return Task.FromResult(new DynamicImageResponse
                        {
                            Format = ImageFormat.Png,
                            HasImage = true,
                            Stream = GetType().Assembly.GetManifestResourceStream(path)
                        });
                    }
                default:
                    throw new ArgumentException("Unsupported image type: " + type);
            }
        }

        public IEnumerable<ImageType> GetSupportedChannelImages()
        {
            return new List<ImageType>
            {
                ImageType.Thumb,
                ImageType.Primary,
                ImageType.Backdrop
            };
        }

        public string Name
        {
            get { return "SoundCloud"; }
        }

        public ChannelFeatures GetChannelFeatures()
        {
            return new ChannelFeatures
            {
                CanSearch = false,

                ContentTypes = new List<ChannelMediaContentType>
                 {
                     ChannelMediaContentType.Song
                 },

                MediaTypes = new List<ChannelMediaType>
                  {
                       ChannelMediaType.Audio
                  },
                  MaxPageSize = 200
            };
        }

        public Task<ChannelItemResult> GetAllMedia(InternalAllChannelItemsQuery query, CancellationToken cancellationToken)
        {
            // Unsupported by this channel
            throw new NotImplementedException();
        }

        public string HomePageUrl
        {
            get { return "http://www.soundcloud.com/"; }
        }
{'fraction_non_alphanumeric': 0.06768900981119631, 'fraction_numerical': 0.003215434083601286, 'mean_word_length': 1.4851464863757426, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3866062', 'n_tokens_mistral': 2856, 'n_tokens_neox': 2722, 'n_words': 567}