-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathData.cs
171 lines (163 loc) · 4.69 KB
/
Data.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
using System.Collections.Specialized;
using System.Collections.Generic;
using Wox.Infrastructure.Storage;
using System.Threading.Tasks;
using Wox.Infrastructure;
using Wox.Plugin.Logger;
using System.Text.Json;
using System.Net.Http;
using System.Windows;
using System.Linq;
using Wox.Plugin;
using System.IO;
using System;
using System.Windows.Media.Imaging;
using System.Security.Cryptography;
using System.Text;
namespace Community.PowerToys.Run.Plugin.Its_MyPic
{
public class History
{
public int Version { get; set; }
public List<int> Histroy { get; set; }
}
public class SubtitleInfo
{
private static int modifiedCount = 0;
public string Text { get; set; }
public string Episode { get; set; }
public int Frame_start { get; set; }
public int UsedCount { get; set; }
public int Segment_id { get; set; }
public string FileName => $"{Episode}_{Frame_start}.jpg";
public Result ToResult(string search, Data data)
{
var r = StringMatcher.FuzzySearch(search, Text);
var path = $"{Main.PluginDirectory}/Images/{FileName}";
return new Result
{
QueryTextDisplay = search,
IcoPath = path,
Title = Text,
TitleHighlightData = r.MatchData,
Score = UsedCount,
Action = _ =>
{
UsedCount++;
modifiedCount++;
if (modifiedCount > 5)
{
modifiedCount = 0;
data.Save();
Log.Debug("Data Saved", GetType());
}
if (Main.CopyImage)
{
Clipboard.SetImage(new BitmapImage(new Uri(path)));
}
else
{
Clipboard.SetFileDropList([path]);
}
return true;
},
ContextData = this,
};
}
}
public class Data
{
private static readonly int DATA_VERSION = BitConverter.ToInt32(SHA256.HashData(Encoding.UTF8.GetBytes("我全都不會彈喔")), 0);
private readonly HttpClient client = new();
readonly JsonSerializerOptions options = new() { PropertyNameCaseInsensitive = true, };
public static string PluginDirectory => Main.PluginDirectory;
private readonly List<SubtitleInfo> subtitleDatas = [];
private readonly PluginJsonStorage<History> storage;
private readonly History history;
public Data()
{
if (!Directory.Exists($"{PluginDirectory}/Images"))
{
Directory.CreateDirectory($"{PluginDirectory}/Images");
}
subtitleDatas = JsonSerializer.Deserialize<List<SubtitleInfo>>(File.ReadAllText($"{PluginDirectory}/data/data.json"), options);
foreach (var data in subtitleDatas)
{
data.Text = data.Text.Replace("妳", "你").ToLower();
}
Log.Info($"Loaded {subtitleDatas.Count} Subtitle Data", GetType());
storage = new();
history = storage.Load();
Log.Debug($"{history.Version} {DATA_VERSION}", GetType());
if (history.Version != DATA_VERSION)
{
Log.Info("History is outdated, updateing", GetType());
history.Version = DATA_VERSION;
history.Histroy = null;
}
if (history.Histroy is null)
{
Log.Info("History is empty, Initializing", GetType());
history.Histroy = new List<int>(new int[subtitleDatas.Count]);
storage.Save();
}
for (int i = 0; i < history.Histroy.Count; i++)
{
subtitleDatas[i].UsedCount = history.Histroy[i];
}
}
public List<Result> GetMatchedSubtitleDatas(string SEARCH, bool waitable = true)
{
var search = SEARCH.ToLower();
var ret = subtitleDatas
.Where(data => data.Text.Contains(search.Replace("妳", "你"), StringComparison.CurrentCultureIgnoreCase))
.OrderByDescending(e => e.UsedCount)
.ThenByDescending(e => e.UsedCount)
.Take(25);
if (waitable)
{
PrepareImage(ret).Wait();
}
else
{
ret = ret.Where(e => File.Exists($"{PluginDirectory}/Images/{e.FileName}"));
}
return ret.Select(e => e.ToResult(SEARCH, this)).ToList();
}
private async Task PrepareImage(IEnumerable<SubtitleInfo> filteredDatas)
{
IEnumerable<Task> tasks = filteredDatas.Select(d => DownloadImage(d));
await Task.WhenAll(tasks);
}
private async Task DownloadImage(SubtitleInfo subtitleData)
{
var file_name = subtitleData.FileName;
var file_path = $"{PluginDirectory}/Images/{file_name}";
if (File.Exists(file_path))
{
return;
}
var url = $"https://mygodata.0m0.uk/images/{file_name}";
using HttpResponseMessage message = await client.GetAsync(url);
var stream = await message.Content.ReadAsByteArrayAsync();
for (int i = 0; i < 3; i++)
{
try
{
await File.WriteAllBytesAsync(file_path, stream);
return;
}
catch (Exception e)
{
Log.Warn($"Failed to download {file_name} retrying {i + 1} {e}", GetType());
}
}
Log.Error($"Failed to download {file_name}", GetType());
}
public void Save()
{
history.Histroy = subtitleDatas.Select(e => e.UsedCount).ToList();
storage.Save();
}
}
}