Repository navigation
Expand file tree
/
Copy pathRegionTagHelper.cs
More file actions
225 lines (202 loc) · 9.41 KB
/
Copy pathRegionTagHelper.cs
File metadata and controls
225 lines (202 loc) · 9.41 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
using System.Collections.Generic;
using System.Linq;
using System;
namespace SmartTags;
public static class RegionTagHelper
{
// 语言代码 -> 主要使用该语言的国家 (用于兜底)
private static readonly Dictionary<string, HashSet<string>> LanguageToCountryMap = new()
{
// === 中文系 ===
{ "zh", new HashSet<string> { "CN", "HK", "TW", "SG", "MO" } },
{ "cn", new HashSet<string> { "CN", "HK", "TW", "SG", "MO" } },
{ "bo", new HashSet<string> { "CN" } },
// === 英语系 ===
{ "en", new HashSet<string> { "US", "GB", "CA", "AU", "NZ", "IE", "ZA", "PH", "SG" } },
// === 日韩 ===
{ "ja", new HashSet<string> { "JP" } },
{ "ko", new HashSet<string> { "KR", "KP" } },
// === 欧洲 ===
{ "fr", new HashSet<string> { "FR", "BE", "CA", "CH", "MA" } },
{ "de", new HashSet<string> { "DE", "AT", "CH", "DD" } },
{ "it", new HashSet<string> { "IT", "CH" } },
{ "es", new HashSet<string> { "ES", "MX", "AR", "CL", "CO", "PE", "CU", "VE" } },
{ "pt", new HashSet<string> { "PT", "BR" } },
{ "ru", new HashSet<string> { "RU", "SU", "UA", "KZ", "BY" } },
{ "nl", new HashSet<string> { "NL", "BE" } },
// === 北欧 ===
{ "sv", new HashSet<string> { "SE" } },
{ "no", new HashSet<string> { "NO" } },
{ "da", new HashSet<string> { "DK" } },
{ "fi", new HashSet<string> { "FI" } },
{ "is", new HashSet<string> { "IS" } },
// === 东欧/中欧 ===
{ "pl", new HashSet<string> { "PL" } },
{ "cs", new HashSet<string> { "CZ", "CS", "XC" } },
{ "hu", new HashSet<string> { "HU" } },
{ "ro", new HashSet<string> { "RO" } },
{ "bg", new HashSet<string> { "BG" } },
{ "sr", new HashSet<string> { "RS", "YU", "HR", "BA" } },
{ "hr", new HashSet<string> { "HR", "YU" } },
{ "uk", new HashSet<string> { "UA", "SU" } },
// === 亚洲其他 ===
{ "hi", new HashSet<string> { "IN" } },
{ "ta", new HashSet<string> { "IN" } },
{ "th", new HashSet<string> { "TH" } },
{ "vi", new HashSet<string> { "VN" } },
{ "id", new HashSet<string> { "ID" } },
{ "ms", new HashSet<string> { "MY", "SG" } },
{ "tl", new HashSet<string> { "PH" } },
{ "fa", new HashSet<string> { "IR" } },
{ "he", new HashSet<string> { "IL" } },
{ "tr", new HashSet<string> { "TR" } },
// === 阿拉伯 ===
{ "ar", new HashSet<string> { "EG", "SA", "AE", "QA", "MA", "IQ", "LB" } }
};
// 国家代码 -> 中文名映射
private static readonly Dictionary<string, string> CountryCodeToName = new()
{
// === 英语国家 ===
{ "US", "美国" }, { "GB", "英国" }, { "CA", "加拿大" }, { "AU", "澳大利亚" },
{ "NZ", "新西兰" }, { "IE", "爱尔兰" },
// === 中文/东亚 ===
{ "CN", "中国" }, { "HK", "香港" }, { "TW", "台湾" }, { "SG", "新加坡" },
{ "JP", "日本" }, { "KR", "韩国" }, { "KP", "朝鲜" }, { "MO", "澳门" },
// === 历史政权 (老电影常见) ===
{ "SU", "苏联" }, // Soviet Union
{ "CS", "捷克斯洛伐克" }, // Czechoslovakia
{ "YU", "南斯拉夫" }, // Yugoslavia
{ "DD", "东德" }, // East Germany
{ "XC", "捷克斯洛伐克" }, // TMDB 偶尔使用的非标准代码
// === 欧洲主要 ===
{ "FR", "法国" }, { "DE", "德国" }, { "ES", "西班牙" }, { "IT", "意大利" },
{ "RU", "俄罗斯" }, { "PT", "葡萄牙" }, { "NL", "荷兰" }, { "BE", "比利时" },
{ "SE", "瑞典" }, { "NO", "挪威" }, { "DK", "丹麦" }, { "FI", "芬兰" },
{ "IS", "冰岛" }, { "CH", "瑞士" }, { "AT", "奥地利" }, { "GR", "希腊" },
// === 欧洲其他 ===
{ "PL", "波兰" }, { "TR", "土耳其" }, { "UA", "乌克兰" }, { "CZ", "捷克" },
{ "HU", "匈牙利" }, { "RO", "罗马尼亚" }, { "BG", "保加利亚" }, { "RS", "塞尔维亚" },
{ "HR", "克罗地亚" }, { "SK", "斯洛伐克" },
// === 美洲 ===
{ "BR", "巴西" }, { "MX", "墨西哥" }, { "AR", "阿根廷" }, { "CL", "智利" },
{ "CO", "哥伦比亚" }, { "PE", "秘鲁" }, { "CU", "古巴" }, { "VE", "委内瑞拉" },
// === 亚洲其他 ===
{ "IN", "印度" }, { "TH", "泰国" }, { "VN", "越南" }, { "ID", "印尼" },
{ "PH", "菲律宾" }, { "MY", "马来西亚" }, { "PK", "巴基斯坦" }, { "IR", "伊朗" },
{ "IL", "以色列" }, { "SA", "沙特" }, { "AE", "阿联酋" }, { "QA", "卡塔尔" },
{ "KZ", "哈萨克斯坦" },
// === 非洲 ===
{ "EG", "埃及" }, { "ZA", "南非" }, { "MA", "摩洛哥" }, { "NG", "尼日利亚" },
{ "KE", "肯尼亚" }
};
public static string? GetRegionTag(TmdbCacheData data, SmartTagsConfig config)
{
if (data == null) return null;
string lang = data.OriginalLanguage?.ToLower() ?? "";
string? code = null;
// === 逻辑层级 1: 优先分析 OriginCountries ===
if (data.OriginCountries != null && data.OriginCountries.Count > 0)
{
// Case A: 单一产地 (Fast Path - 90%的情况)
// 只要只有一个,直接信任,无需仲裁
if (data.OriginCountries.Count == 1)
{
code = data.OriginCountries[0];
}
// Case B: 多个产地 (需要仲裁)
else
{
bool isChineseLang = lang == "zh" || lang == "cn" || lang == "bo";
// B1: 华语片 - 语言锚定策略
if (isChineseLang)
{
// 粤语 (cn) -> 锚定 HK
if (lang == "cn")
{
code = data.OriginCountries.FirstOrDefault(c => c.Equals("HK", StringComparison.OrdinalIgnoreCase));
}
// 国语 (zh) -> 锚定 TW 或 CN
else if (lang == "zh")
{
code = data.OriginCountries.FirstOrDefault(c =>
c.Equals("TW", StringComparison.OrdinalIgnoreCase) ||
c.Equals("CN", StringComparison.OrdinalIgnoreCase));
}
}
// B2: 非华语片 (或 B1 锚定失败) - 资金方仲裁策略
if (string.IsNullOrEmpty(code))
{
var primaryProducer = data.ProductionCountries?.FirstOrDefault();
if (!string.IsNullOrEmpty(primaryProducer) && data.OriginCountries.Contains(primaryProducer))
{
code = primaryProducer;
}
}
// B3: 所有仲裁都失败,回退到 Origin 的第一个
if (string.IsNullOrEmpty(code))
{
code = data.OriginCountries[0];
}
}
}
// === 逻辑层级 2: 数据缺失兜底 ===
// 如果 Origin 为空,尝试用 ProductionCountries + Language 推断
else if (data.ProductionCountries != null && data.ProductionCountries.Count > 0 && !string.IsNullOrEmpty(lang))
{
if (LanguageToCountryMap.TryGetValue(lang, out var validCountries))
{
code = data.ProductionCountries.FirstOrDefault(c => validCountries.Contains(c));
}
}
// === 格式化输出 ===
if (!string.IsNullOrEmpty(code))
{
return FormatCountryTag(code, config.CountryStyle);
}
// === 逻辑层级 3: 最终兜底 (仅语言) ===
return GetDefaultCountryByLanguage(lang, config.CountryStyle);
}
private static string FormatCountryTag(string code, CountryTagStyle style)
{
string name = CountryCodeToName.TryGetValue(code, out var n) ? n : code;
return style switch
{
CountryTagStyle.NameOnly => name,
CountryTagStyle.CodeOnly => code,
CountryTagStyle.NameAndCode => $"{name} ({code})",
_ => name
};
}
// 语言兖底映射: 语言代码 -> (国家代码, 中文名)
private static readonly Dictionary<string, (string Code, string Name)> LanguageFallbackMap = new()
{
{ "ja", ("JP", "日本") },
{ "ko", ("KR", "韩国") },
{ "zh", ("CN", "华语") },
{ "cn", ("HK", "华语") },
{ "en", ("US", "英语") },
{ "fr", ("FR", "法国") },
{ "de", ("DE", "德国") },
{ "ru", ("RU", "俄罗斯") },
{ "es", ("ES", "西班牙") },
{ "pt", ("PT", "葡萄牙") },
{ "it", ("IT", "意大利") },
{ "th", ("TH", "泰国") },
{ "vi", ("VN", "越南") },
{ "hi", ("IN", "印度") },
{ "pl", ("PL", "波兰") }
};
private static string? GetDefaultCountryByLanguage(string lang, CountryTagStyle style)
{
if (LanguageFallbackMap.TryGetValue(lang, out var fallback))
{
return style switch
{
CountryTagStyle.CodeOnly => fallback.Code,
CountryTagStyle.NameAndCode => $"{fallback.Name} ({fallback.Code})",
_ => fallback.Name // NameOnly (default)
};
}
return null;
}
}