<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using IF.Lastfm.Core.Api.Helpers;
using Newtonsoft.Json.Linq;

namespace IF.Lastfm.Core.Objects
{
    public class LastAlbum : ILastFmObject
    {
        #region Properties

        public string Name { get; set; }
        public IEnumerable<LastTrack> Tracks { get; set; }
        
        public string ArtistName { get; set; }
        public string ArtistId { get; set; }
        
        public DateTime ReleaseDateUtc { get; set; }

        public int ListenerCount { get; set; }
        public int TotalPlayCount { get; set; }

        public string Mbid { get; set; }

        public IEnumerable<Tag> TopTags { get; set; }

        public Uri Url { get; set; }

        public LastImageSet Images { get; set; }
        
        #endregion

        /// <summary>
        /// TODO datetime parsing
        /// TODO images
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        internal static LastAlbum ParseJToken(JToken token)
        {
            var a = new LastAlbum();

            try
            {
                a.ArtistName = token.Value<string>("artist");
                a.ArtistId = token.Value<string>("id");

                if (token.Contains("tracks"))
                {
                    var tracksToken = token.SelectToken("tracks").SelectToken("track");
                    if (tracksToken != null)
                    {
                        a.Tracks = tracksToken.Children().Select(trackToken => LastTrack.ParseJToken(trackToken, a.Name));
                    }

                    var tagsToken = token.SelectToken("toptags").SelectToken("tag");
                    if (tagsToken != null)
                    {
                        a.TopTags = tagsToken.Children().Select(Tag.ParseJToken);
                    }
                }
            }
            catch
            {
                //artist is not a string but a FmArtist object
                var artist = token.SelectToken("artist").ToObject<LastArtist>();
                a.ArtistName = artist.Name;
                a.ArtistId = artist.Mbid;
            }

            a.ListenerCount = token.Value<int>("listeners");
            a.Mbid = token.Value<string>("mbid");
            a.Name = token.Value<string>("name");
            a.TotalPlayCount = token.Value<int>("playcount");

            var images = token.SelectToken("image");
            if (images != null)
            {
                var imageCollection = LastImageSet.ParseJToken(images);
                a.Images = imageCollection;
            }
            
            a.Url = new Uri(token.Value<string>("url"), UriKind.Absolute);

            return a;
        }

        internal static string GetNameFromJToken(JToken albumToken)
        {
            var name = albumToken.Value<string>("name");

            if (string.IsNullOrEmpty(name))
            {
                name = albumToken.Value<string>("#text");
            }
<commit_msg>Fix conflicts with ceb16a9<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using IF.Lastfm.Core.Api.Helpers;
using Newtonsoft.Json.Linq;

namespace IF.Lastfm.Core.Objects
{
    public class LastAlbum : ILastFmObject
    {
        #region Properties

        public string Name { get; set; }
        public IEnumerable<LastTrack> Tracks { get; set; }
        
        public string ArtistName { get; set; }
        public string ArtistId { get; set; }
        
        public DateTime ReleaseDateUtc { get; set; }

        public int ListenerCount { get; set; }
        public int TotalPlayCount { get; set; }

        public string Mbid { get; set; }

        public IEnumerable<Tag> TopTags { get; set; }

        public Uri Url { get; set; }

        public LastImageSet Images { get; set; }
        
        #endregion

        /// <summary>
        /// TODO datetime parsing
        /// TODO images
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        internal static LastAlbum ParseJToken(JToken token)
        {
            var a = new LastAlbum();

            var artistToken = token["artist"];
            switch (artistToken.Type)
            {
                case JTokenType.String:
                    a.ArtistName = token.Value<string>("artist");
                    a.ArtistId = token.Value<string>("id");
                    break;
                case JTokenType.Object:
                    a.ArtistName = artistToken.Value<string>("name");
                    a.ArtistId = artistToken.Value<string>("mbid");
                    break;
            }

            var tracksToken = token.SelectToken("tracks");
            if (tracksToken != null)
            {
                var trackToken = tracksToken.SelectToken("track");
                if (trackToken != null)
                    a.Tracks = tracksToken.Children().Select(t => LastTrack.ParseJToken(t, a.Name));
            }

            var tagsToken = token.SelectToken("toptags");
            if (tagsToken != null)
            {
                var tagToken = tagsToken.SelectToken("tag");
                a.TopTags = tagToken.Children().Select(Tag.ParseJToken);
            }
    
            a.ListenerCount = token.Value<int>("listeners");
            a.Mbid = token.Value<string>("mbid");
            a.Name = token.Value<string>("name");
            a.TotalPlayCount = token.Value<int>("playcount");

            var images = token.SelectToken("image");
            if (images != null)
            {
                var imageCollection = LastImageSet.ParseJToken(images);
                a.Images = imageCollection;
            }
            
            a.Url = new Uri(token.Value<string>("url"), UriKind.Absolute);

            return a;
        }

        internal static string GetNameFromJToken(JToken albumToken)
        {
            var name = albumToken.Value<string>("name");

            if (string.IsNullOrEmpty(name))
            {
                name = albumToken.Value<string>("#text");
            }

{'fraction_non_alphanumeric': 0.11254281520143533, 'fraction_numerical': 0.0004893165878323275, 'mean_word_length': 1.5328376703841389, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 42, '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': '7645816', 'n_tokens_mistral': 1641, 'n_tokens_neox': 1581, 'n_words': 351}