<commit_before>﻿using System.Collections.Generic;
using System.Linq;
using Wox.Plugin.Spotify.MetadataApi;

namespace Wox.Plugin.Spotify
{
    public class SpotifyPlugin : IPlugin
    {
        private PluginInitContext _context;

        private ApiData data;

        public void Init(PluginInitContext context)
        {
            this._context = context;

            // initialize data, passing it the plugin directory
            data = new ApiData(_context.CurrentPluginMetadata.PluginDirecotry);
        }

        public List<Result> Query(Query query)
        {
            var param = query.GetAllRemainingParameter();

            // check if this is an album or artist search, default to track search
            switch (query.ActionParameters[0])
            {
                case "artist":
                    param = param.Substring("artist ".Length);
                    // Retrieve data and return the first 10 results
                    return data.GetArtists(param).Artists.ToList().GetRange(0, 10).Select(x => new Result()
                        {
                            Title = x.Name,
                            SubTitle = string.Format("Popularity: {0}%", System.Convert.ToDouble(x.Popularity)*100 ),
                            // When selected, open it with the spotify client
                            Action = e => _context.ShellRun(x.Href),
                            IcoPath = "icon.png"
                        }).ToList();
                case "album":
                    param = param.Substring("album ".Length);
                    // Retrieve data and return the first 10 results
                    return data.GetAlbums(param).Albums.ToList().GetRange(0, 10).Select(x => new Result()
                        {
                            Title = x.Name,
                            SubTitle = "Artist: " + string.Join(", ", x.Artists.Select(a => a.Name).ToArray()),
                            // When selected, open it with the spotify client
                            Action = e => _context.ShellRun(x.Href),
                            IcoPath = data.GetArtwork(x.Href)
                        }).ToList();
                default:
                    if (query.ActionParameters[0] == "track")
                        param = param.Substring("track ".Length);
                    // Retrieve data and return the first 20 results
                    return data.GetTracks(param).Tracks.ToList().GetRange(0, 20).Select(x => new Result()
                        {
                            Title = x.Name,
                            SubTitle = "Artist: " + string.Join(", ", x.Artists.Select(a => a.Name).ToArray()),
                            // When selected, open it with the spotify client
                            Action = e => _context.ShellRun(x.Href),
                            IcoPath = "icon.png"
                        }).ToList();
            }
        }
    }
}<commit_msg>notify when no results are found<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using Wox.Plugin.Spotify.MetadataApi;

namespace Wox.Plugin.Spotify
{
    public class SpotifyPlugin : IPlugin
    {
        private PluginInitContext _context;

        private ApiData data;

        public void Init(PluginInitContext context)
        {
            this._context = context;

            // initialize data, passing it the plugin directory
            data = new ApiData(_context.CurrentPluginMetadata.PluginDirecotry);
        }

        public List<Result> Query(Query query)
        {
            var param = query.GetAllRemainingParameter();
            var Results = new List<Result>();

            // check if this is an album or artist search, default to track search
            switch (query.ActionParameters[0])
            {
                case "artist":
                    param = param.Substring("artist ".Length);
                    // Retrieve data and return the first 10 results
                    Results = data.GetArtists(param).Artists.ToList().GetRangeSafe(0, 10).Select(x => new Result()
                        {
                            Title = x.Name,
                            SubTitle = string.Format("Popularity: {0}%", System.Convert.ToDouble(x.Popularity)*100 ),
                            // When selected, open it with the spotify client
                            Action = e => _context.ShellRun(x.Href),
                            IcoPath = "icon.png"
                        }).ToList();
                    break;
                case "album":
                    param = param.Substring("album ".Length);
                    // Retrieve data and return the first 10 results
                    Results = data.GetAlbums(param).Albums.ToList().GetRangeSafe(0, 10).Select(x => new Result()
                        {
                            Title = x.Name,
                            SubTitle = "Artist: " + string.Join(", ", x.Artists.Select(a => a.Name).ToArray()),
                            // When selected, open it with the spotify client
                            Action = e => _context.ShellRun(x.Href),
                            IcoPath = data.GetArtwork(x.Href)
                        }).ToList();
                    break;
                default:
                    if (query.ActionParameters[0] == "track")
                        param = param.Substring("track ".Length);
                    // Retrieve data and return the first 20 results
                    Results = data.GetTracks(param).Tracks.ToList().GetRangeSafe(0, 20).Select(x => new Result()
                        {
                            Title = x.Name,
                            SubTitle = "Artist: " + string.Join(", ", x.Artists.Select(a => a.Name).ToArray()),
                            // When selected, open it with the spotify client
                            Action = e => _context.ShellRun(x.Href),
                            IcoPath = "icon.png"
                        }).ToList();
                    break;
            }
            if (Results.Count > 0)
                return Results;
            else
                return new List<Result>()
                    {
                        new Result() { Title = "No results found on Spotify.", IcoPath = "icon.png" }
                    };
        }        
    }

    public static class Extensions
    {
        /// <summary>
        /// Returns a range of elements in the source List, limiting the number of 
        /// results to the specified maximum and starting from the specified index.
        /// </summary>
        public static List<T> GetRangeSafe<T> (this List<T> source, int index, int maxCount)
        {
            // Index cannot be negative
            if (index < 0)
                throw new ArgumentOutOfRangeException("index");

            // if index, count are not out of bounds, use GetRange
            if (source.Count - index >= maxCount)
                return source.GetRange(index, maxCount);
            
            // if count is greater than the number of items after the specified position (index),
            //  return a list of all items after that position.
            if (source.Count - index >= 0)
                return source.GetRange(index, source.Count - index);
            
            // In any other case, return an empty list
            return new List<T>();
        }
    }
}
{'fraction_non_alphanumeric': 0.09648768671780379, 'fraction_numerical': 0.006055712555510699, 'mean_word_length': 1.271393643031785, 'pattern_counts': {'":': 4, '<': 14, '<?xml version=': 0, '>': 32, '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': '3987807', 'n_tokens_mistral': 1838, 'n_tokens_neox': 1739, 'n_words': 492}