<commit_before>﻿// 
// 	XboxUnity.cs
// 	AuroraAssetEditor
// 
// 	Created by Swizzy on 10/05/2015
// 	Copyright (c) 2015 Swizzy. All rights reserved.

namespace AuroraAssetEditor.Classes {
    using System;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Json;
    using System.Web;

    internal static class XboxUnity {
        private static readonly DataContractJsonSerializer Serializer = new DataContractJsonSerializer(typeof(UnityResponse[]));
        private static readonly DataContractJsonSerializer CacheSerializer = new DataContractJsonSerializer(typeof(XboxUnityTitle[]));

        private static string GetUnityUrl(string searchTerm) { return string.Format("http://xboxunity.net/api/Covers/{0}", HttpUtility.UrlEncode(searchTerm)); }

        public static XboxUnityAsset[] GetUnityCoverInfo(int titleId) {
            using(var wc = new WebClient()) {
                var stream = wc.OpenRead(GetUnityUrl(titleId.ToString("X8")));
                return stream != null ? ((UnityResponse[])Serializer.ReadObject(stream)).Select(t => new XboxUnityAsset(t)).ToArray() : new XboxUnityAsset[0];
            }
        }

        public static XboxUnityAsset[] GetUnityCoverInfo(string searchTerm) {
            using(var wc = new WebClient()) {
                try {
                    var stream = wc.OpenRead(GetUnityUrl(searchTerm));
                    return stream != null ? ((UnityResponse[])Serializer.ReadObject(stream)).Select(t => new XboxUnityAsset(t)).ToArray() : new XboxUnityAsset[0];
                }
                catch(Exception ex) {
                    MainWindow.SaveError(ex);
                    return new XboxUnityAsset[0];
                }
            }
        }

        public static XboxUnityTitle[] GetSavedTitleCache() {
            var path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            try {
                path = !string.IsNullOrWhiteSpace(path) ? Path.Combine(path, "AuroraAssetEditor", "titles.cache") : "titles.cache";
                using(var stream = File.OpenRead(path))
                    return (XboxUnityTitle[])CacheSerializer.ReadObject(stream);
            }
            catch {
                return new XboxUnityTitle[0];
            }
        }

        public static XboxUnityTitle[] UpdateTitleCache() {
            var path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            try {
                path = !string.IsNullOrWhiteSpace(path) ? Path.Combine(path, "AuroraAssetEditor", "titles.cache") : "titles.cache";
                var wc = new WebClient();
                wc.DownloadFile("http://xboxunity.net/api/gettitle", path + ".dl");
                if(!File.Exists(path + ".dl") || new FileInfo(path + ".dl").Length <= 10)
                    return GetSavedTitleCache();
                File.Copy(path + ".dl", path);
                File.Delete(path + ".dl");
                return GetSavedTitleCache();
            }
            catch {
                return GetSavedTitleCache();
            }
        }

        [DataContract] internal class UnityResponse {
            [DataMember(Name = "titleid")] public string TitleId { get; set; }

            [DataMember(Name = "name")] public string Name { get; set; }

            [DataMember(Name = "official")] public bool Official { get; set; }

            [DataMember(Name = "filesize")] public string FileSize { get; set; }<commit_msg>Overwrite old cache upon updating<commit_after>﻿// 
// 	XboxUnity.cs
// 	AuroraAssetEditor
// 
// 	Created by Swizzy on 10/05/2015
// 	Copyright (c) 2015 Swizzy. All rights reserved.

namespace AuroraAssetEditor.Classes {
    using System;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Json;
    using System.Web;

    internal static class XboxUnity {
        private static readonly DataContractJsonSerializer Serializer = new DataContractJsonSerializer(typeof(UnityResponse[]));
        private static readonly DataContractJsonSerializer CacheSerializer = new DataContractJsonSerializer(typeof(XboxUnityTitle[]));

        private static string GetUnityUrl(string searchTerm) { return string.Format("http://xboxunity.net/api/Covers/{0}", HttpUtility.UrlEncode(searchTerm)); }

        public static XboxUnityAsset[] GetUnityCoverInfo(int titleId) {
            using(var wc = new WebClient()) {
                var stream = wc.OpenRead(GetUnityUrl(titleId.ToString("X8")));
                return stream != null ? ((UnityResponse[])Serializer.ReadObject(stream)).Select(t => new XboxUnityAsset(t)).ToArray() : new XboxUnityAsset[0];
            }
        }

        public static XboxUnityAsset[] GetUnityCoverInfo(string searchTerm) {
            using(var wc = new WebClient()) {
                try {
                    var stream = wc.OpenRead(GetUnityUrl(searchTerm));
                    return stream != null ? ((UnityResponse[])Serializer.ReadObject(stream)).Select(t => new XboxUnityAsset(t)).ToArray() : new XboxUnityAsset[0];
                }
                catch(Exception ex) {
                    MainWindow.SaveError(ex);
                    return new XboxUnityAsset[0];
                }
            }
        }

        public static XboxUnityTitle[] GetSavedTitleCache() {
            var path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            try {
                path = !string.IsNullOrWhiteSpace(path) ? Path.Combine(path, "AuroraAssetEditor", "titles.cache") : "titles.cache";
                using(var stream = File.OpenRead(path))
                    return (XboxUnityTitle[])CacheSerializer.ReadObject(stream);
            }
            catch {
                return new XboxUnityTitle[0];
            }
        }

        public static XboxUnityTitle[] UpdateTitleCache() {
            var path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            try {
                path = !string.IsNullOrWhiteSpace(path) ? Path.Combine(path, "AuroraAssetEditor", "titles.cache") : "titles.cache";
                var wc = new WebClient();
                wc.DownloadFile("http://xboxunity.net/api/gettitle", path + ".dl");
                if(!File.Exists(path + ".dl") || new FileInfo(path + ".dl").Length <= 10)
                    return GetSavedTitleCache();
                File.Copy(path + ".dl", path, true);
                File.Delete(path + ".dl");
                return GetSavedTitleCache();
            }
            catch {
                return GetSavedTitleCache();
            }
        }

        [DataContract] internal class UnityResponse {
            [DataMember(Name = "titleid")] public string TitleId { get; set; }

            [DataMember(Name = "name")] public string Name { get; set; }

            [DataMember(Name = "official")] public bool Official { get; set; }

            [DataMember(Name = "filesize")] public string FileSize { get; set; }
{'fraction_non_alphanumeric': 0.11227628635346756, 'fraction_numerical': 0.005592841163310962, 'mean_word_length': 2.2133872416891283, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2349874', 'n_tokens_mistral': 1956, 'n_tokens_neox': 1823, 'n_words': 405}