<commit_before>﻿using Fizzler.Systems.HtmlAgilityPack;
using HtmlAgilityPack;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web;

namespace VSMarketplaceBadge.Models
{
    public static class VsMarketplace
    {
        private static readonly Uri marketplaceUri = new Uri("https://marketplace.visualstudio.com/");
        private static readonly Uri marketplaceItemUri = new Uri(marketplaceUri, "items");
        private static readonly string itemQuery = "itemName";

        public static async Task<string> Load(string itemName, BadgeType type)
        {
            switch (type)
            {
                case BadgeType.Version:
                case BadgeType.VersionShort:
                    return await LoadVersion(itemName);
                case BadgeType.Installs:
                    return await LoadInstalls(itemName);
                case BadgeType.Rating:
                    return await LoadRating(itemName);
                case BadgeType.RatingShort:
                    return await LoadRating(itemName, true);
                default:
                    throw new ArgumentException();
            }
        }
        
        private static async Task<string> LoadRating(string itemName, bool isShort = false)
        {
            var json = await LoadVssItemData(itemName);
            var average = json["statistics"].FirstOrDefault(x => (string)x["statisticName"] == "averagerating").Value<double>("value");
            var count = json["statistics"].FirstOrDefault(x => (string)x["statisticName"] == "ratingcount").Value<int>("value");
            return isShort
                ? $"{Math.Round(average, 2)}/5 ({count})"
                : $"average: {Math.Round(average, 2)}/5 ({count} ratings)";
        }

        private static async Task<string> LoadVersion(string itemName)
        {
            var json = await LoadVssItemData(itemName);
            return $"v{(string)json["versions"].Max(x => x["version"])}";
        }

        private static async Task<string> LoadInstalls(string itemName)
        {
            var json = await LoadVssItemData(itemName);
            return (string)json["statistics"].FirstOrDefault(x => (string)x["statisticName"] == "install")["value"];
        }

        private static async Task<JObject> LoadVssItemData(string itemName)
        {
            var html = new HtmlDocument();
            using (var client = new HttpClient())
            {
                var builder = CreateItemUri(itemName);
                html.LoadHtml(await client.GetStringAsync(builder.ToString()));
            }<commit_msg>Mod 0 rating badge.<commit_after>﻿using Fizzler.Systems.HtmlAgilityPack;
using HtmlAgilityPack;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web;

namespace VSMarketplaceBadge.Models
{
    public static class VsMarketplace
    {
        private static readonly Uri marketplaceUri = new Uri("https://marketplace.visualstudio.com/");
        private static readonly Uri marketplaceItemUri = new Uri(marketplaceUri, "items");
        private static readonly string itemQuery = "itemName";

        public static async Task<string> Load(string itemName, BadgeType type)
        {
            switch (type)
            {
                case BadgeType.Version:
                case BadgeType.VersionShort:
                    return await LoadVersion(itemName);
                case BadgeType.Installs:
                    return await LoadInstalls(itemName);
                case BadgeType.Rating:
                    return await LoadRating(itemName);
                case BadgeType.RatingShort:
                    return await LoadRating(itemName, true);
                default:
                    throw new ArgumentException();
            }
        }

        private static async Task<string> LoadRating(string itemName, bool isShort = false)
        {
            var json = await LoadVssItemData(itemName);
            var average = json["statistics"].FirstOrDefault(x => (string)x["statisticName"] == "averagerating")?.Value<double>("value");
            var count = json["statistics"].FirstOrDefault(x => (string)x["statisticName"] == "ratingcount")?.Value<int>("value");
            return isShort
                ? $"{Math.Round(average ?? 0, 2)}/5 ({count ?? 0})"
                : $"average: {Math.Round(average ?? 0, 2)}/5 ({count ?? 0} ratings)";
        }

        private static async Task<string> LoadVersion(string itemName)
        {
            var json = await LoadVssItemData(itemName);
            return $"v{(string)json["versions"].Max(x => x["version"])}";
        }

        private static async Task<string> LoadInstalls(string itemName)
        {
            var json = await LoadVssItemData(itemName);
            return (string)json["statistics"].FirstOrDefault(x => (string)x["statisticName"] == "install")["value"];
        }

        private static async Task<JObject> LoadVssItemData(string itemName)
        {
            var html = new HtmlDocument();
            using (var client = new HttpClient())
            {
                var builder = CreateItemUri(itemName);
                html.LoadHtml(await client.GetStringAsync(builder.ToString()));
            }
{'fraction_non_alphanumeric': 0.10768378278537126, 'fraction_numerical': 0.0024011821204285187, 'mean_word_length': 2.2483503299340133, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 25, 'https://': 2, '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': '1447507', 'n_tokens_mistral': 1451, 'n_tokens_neox': 1382, 'n_words': 331}