Skip to content

Commit

Permalink
Revert "feat: modifyPegasusQueryContext"
Browse files Browse the repository at this point in the history
This reverts commit a48a4fd.
  • Loading branch information
VirgilClyne committed Dec 17, 2024
1 parent a48a4fd commit f25d0d5
Show file tree
Hide file tree
Showing 2 changed files with 184 additions and 144 deletions.
180 changes: 39 additions & 141 deletions src/function/modifyPegasusQueryContext.mjs
Original file line number Diff line number Diff line change
@@ -1,143 +1,41 @@
import { Console } from "@nsnanocat/util";

export default function modifyPegasusQueryContext(queryContext, Settings, Query) {
Console.log("☑️ modify PegasusQueryContext");
const Locale = queryContext.locale;
const [Language, CountryCode] = Locale?.split("_") ?? [];
Console.info(`Locale: ${Locale}`, `Language: ${Language}`, `CountryCode: ${CountryCode}`);
switch (Settings.CountryCode) {
case "AUTO":
queryContext.CountryCode = CountryCode;
break;
default:
queryContext.countryCode = Settings.CountryCode;
break;
}
switch (Settings.Region) {
case "AUTO":
queryContext.region = CountryCode;
break;
default:
queryContext.region = Settings.Region;
break;
}
switch (Settings.SiriLocale) {
case "AUTO":
queryContext.siriLocale = Locale;
break;
default:
queryContext.siriLocale = Settings.SiriLocale;
break;
}
switch (Settings.SiriResponseLanguageVariant) {
case "AUTO":
queryContext.siriResponseLanguageVariant = Locale;
break;
default:
queryContext.siriResponseLanguageVariant = Settings.SiriResponseLanguageVariant;
break;
}
if (queryContext?.skuRegion === "CH") queryContext.skuRegion = "LL";
if (Query) {
Query = Query.toLowerCase();
switch (true) {
case Query.includes("什么是") || Query.includes("是什么") || Query.includes("what's ") || Query.includes("what is ") || Query.includes("what does ") || Query.includes("what do "):
case Query.includes("怎么样") || Query.includes("怎样") || Query.includes("如何") || Query.includes("how's ") || Query.includes("how is ") || Query.includes("how does ") || Query.includes("how do "):
case Query.includes("为什么") || Query.includes("why ") || Query.includes("why is ") || Query.includes("why does "):
case Query.includes("搜索") || Query.includes("search "):
case Query.includes(" mean") ||
Query.includes("meaning") ||
Query.includes("explain") ||
Query.includes("look up ") ||
Query.includes("translat") ||
(Query.includes(" in ") && !Query.includes(" here")) ||
Query.includes("web") ||
Query.includes(" internet") ||
Query.includes("defin") ||
Query.includes(" wikipedia") ||
Query.includes("解释") ||
Query.includes("翻译") ||
Query.includes("怎么说") ||
Query.includes("意思"):
delete queryContext.location;
break;
/*
case Query.includes("何时") || Query.includes("几时") || Query.includes("when "):
case Query.includes("什么时") || Query.includes("几点") || Query.includes("what time "):
case Query.includes("哪里") || Query.includes("哪儿") || Query.includes("何处") || Query.includes("where "):
case Query.includes("哪个") || Query.includes("哪一个") || Query.includes("which "):
// 反例:"When was the first plane invented?", "Where was Steve Jobs born?", etc.
break;
*/
case Query.includes("天气") || Query.includes("weather"):
case Query.includes("气压") || Query.includes("air pressure") || Query.includes("barometric pressure") || Query.includes("atmospheric pressure") || Query.includes("atmosphere pressure"):
case Query.includes("湿度") || Query.includes("humidity"):
case Query.includes("温度") || Query.includes("temperature"):
case Query.includes("风速") || Query.includes("wind speed"):
case Query.includes("风向") || Query.includes("wind direction"):
case Query.includes("空气质量") || Query.includes("air quality"):
case Query.includes("月相") || (Query.includes("moon") && Query.includes("phase")):
case Query.includes("紫外线") || Query.includes("uv index") || Query.includes("ultraviolet index outside"):
case Query.includes("能见度") || Query.includes("visibility"):
case Query.includes("日出") || Query.includes("sunrise"):
case Query.includes("日落") || Query.includes("sunset"):
case (Query.includes("sun ") && (Query.includes("rise") || Query.includes("set") || Query.includes("fall"))) || Query.includes("sunrise") || Query.includes("sunset"):
case (Query.includes("太阳") && (Query.includes("升") || Query.includes("落") || Query.includes("下山"))) || Query.includes("日出") || Query.includes("日落"):
case (Query.includes("is it") || Query.includes("it is") || Query.includes("it's")) &&
(Query.includes("humid") ||
Query.includes("rain") ||
Query.includes("shin") ||
Query.includes("sunny") ||
Query.includes("hot") ||
Query.includes("cold") ||
Query.includes("freez") ||
Query.includes("warm") ||
Query.includes("wind") ||
Query.includes("fog") ||
Query.includes("haz") ||
Query.includes("thunder") ||
Query.includes("storm") ||
Query.includes("lightn") ||
Query.includes("snow") ||
Query.includes("hail") ||
Query.includes("clear") ||
Query.includes("tornado") ||
Query.includes("hurricane")):
case (Query.includes("有") || Query.includes("会") || Query.includes("吗")) &&
(Query.includes("下雨") ||
Query.includes("晒") ||
Query.includes("晴") ||
Query.includes("热") ||
Query.includes("冷") ||
Query.includes("暖") ||
Query.includes("风") ||
Query.includes("雾") ||
Query.includes("霾") ||
Query.includes("雷") ||
Query.includes("风暴") ||
Query.includes("电") ||
Query.includes("雪") ||
Query.includes("雹")):
case Query.includes("precipitation") || Query.includes("forecast") || ((Query.includes("chance") || Query.includes("possibilit")) && (Query.includes(" rain") || Query.includes(" snow"))):
case Query.includes("降雪") || Query.includes("降水") || Query.includes("预报") || ((Query.includes("概率") || Query.includes("几率")) && (Query.includes("降水") || Query.includes("降雪"))):
break;
case Query.includes("附近") || Query.includes(" nearby"):
case Query.includes("周围") || Query.includes(" around me") || Query.includes(" around here"):
case Query.includes("导航") || Query.includes("navigat"):
case Query.includes("方向") || Query.includes(" direction"):
case Query.includes("指引") || (Query.includes("direct ") && Query.includes(" to ")):
case Query.includes("指引") || (Query.includes("guide ") && Query.includes(" to ")):
case Query.includes("带我去") || Query.includes("take me to "):
case Query.includes("路线") || Query.includes("route "):
case Query.includes("路径") || Query.includes("path to "):
case (Query.includes("怎样") || Query.includes("如何") || Query.includes("怎么")) && (Query.includes("到") || Query.includes("去") || Query.includes("抵达") || Query.includes("走")):
case Query.includes("how ") && (Query.includes(" get ") || Query.includes(" go ") || Query.includes(" arrive ") || Query.includes(" reach ")):
break;
default:
delete queryContext.location;
break;
}
}
Console.log("✅ modify PegasusQueryContext");
return queryContext;
}
export default function modifyPegasusQueryContext(queryContext, Settings) {
Console.log("☑️ modify PegasusQueryContext");
const Locale = queryContext.locale;
const [Language, CountryCode] = Locale?.split("_") ?? [];
Console.info(`Locale: ${Locale}`, `Language: ${Language}`, `CountryCode: ${CountryCode}`);
switch (Settings.CountryCode) {
// biome-ignore lint/suspicious/noFallthroughSwitchClause: <explanation>
case "AUTO":
Settings.CountryCode = CountryCode;
default:
queryContext.countryCode = Settings.CountryCode;

break;
};
switch (Settings.Region) {
case "AUTO":
break;
default:
queryContext.region = Settings.CountryCode;
break;
};
switch (Settings.SiriLocale) {
case "AUTO":
break;
default:
queryContext.siriLocale = Settings.SiriLocale;
break;
};
switch (Settings.SiriResponseLanguageVariant) {
case "AUTO":
break;
default:
queryContext.siriResponseLanguageVariant = Settings.SiriResponseLanguageVariant;
break;
};
if (queryContext?.skuRegion === "CH") queryContext.skuRegion = "LL";
Console.log("✅ modify PegasusQueryContext");
return queryContext;
};
148 changes: 145 additions & 3 deletions src/request.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,149 @@ Console.info(`PLATFORM: ${PLATFORM}`);
// Siri搜索
body = SiriPegasusRequest.fromBinary(rawBody);
Console.debug(`body: ${JSON.stringify(body, null, 2)}`);
body.queryContext = modifyPegasusQueryContext(body.queryContext, Settings, body.queries?.[0]?.utterance);
body.queryContext = modifyPegasusQueryContext(body.queryContext, Settings);
let fixLocation = true;
const utterance = (body?.queries?.[0]?.utterance ?? "").toLowerCase();
switch (true) {
case utterance.includes("什么是") || utterance.includes("是什么") || utterance.includes("what's ") || utterance.includes("what is ") || utterance.includes("what does ") || utterance.includes("what do "):
case utterance.includes("怎么样") || utterance.includes("怎样") || utterance.includes("如何") || utterance.includes("how's ") || utterance.includes("how is ") || utterance.includes("how does ") || utterance.includes("how do "):
case utterance.includes("为什么") || utterance.includes("why ") || utterance.includes("why is ") || utterance.includes("why does "):
case utterance.includes("搜索") || utterance.includes("search "):
case utterance.includes(" mean") ||
utterance.includes("meaning") ||
utterance.includes("explain") ||
utterance.includes("look up ") ||
utterance.includes("translat") ||
(utterance.includes(" in ") && !utterance.includes(" here")) ||
utterance.includes("web") ||
utterance.includes(" internet") ||
utterance.includes("defin") ||
utterance.includes(" wikipedia") ||
utterance.includes("解释") ||
utterance.includes("翻译") ||
utterance.includes("怎么说") ||
utterance.includes("意思"):
fixLocation = true;
break;
/*
case utterance.includes("何时") || utterance.includes("几时") || utterance.includes("when "):
case utterance.includes("什么时") || utterance.includes("几点") || utterance.includes("what time "):
case utterance.includes("哪里") || utterance.includes("哪儿") || utterance.includes("何处") || utterance.includes("where "):
case utterance.includes("哪个") || utterance.includes("哪一个") || utterance.includes("which "):
// 反例:"When was the first plane invented?", "Where was Steve Jobs born?", etc.
fixLocation = false;
break;
*/
case utterance.includes("天气") || utterance.includes("weather"):
case utterance.includes("气压") || utterance.includes("air pressure") || utterance.includes("barometric pressure") || utterance.includes("atmospheric pressure") || utterance.includes("atmosphere pressure"):
case utterance.includes("湿度") || utterance.includes("humidity"):
case utterance.includes("温度") || utterance.includes("temperature"):
case utterance.includes("风速") || utterance.includes("wind speed"):
case utterance.includes("风向") || utterance.includes("wind direction"):
case utterance.includes("空气质量") || utterance.includes("air quality"):
case utterance.includes("月相") || (utterance.includes("moon") && utterance.includes("phase")):
case utterance.includes("紫外线") || utterance.includes("uv index") || utterance.includes("ultraviolet index outside"):
case utterance.includes("能见度") || utterance.includes("visibility"):
case utterance.includes("日出") || utterance.includes("sunrise"):
case utterance.includes("日落") || utterance.includes("sunset"):
case (utterance.includes("sun ") && (utterance.includes("rise") || utterance.includes("set") || utterance.includes("fall"))) || utterance.includes("sunrise") || utterance.includes("sunset"):
case (utterance.includes("太阳") && (utterance.includes("升") || utterance.includes("落") || utterance.includes("下山"))) || utterance.includes("日出") || utterance.includes("日落"):
case (utterance.includes("is it") || utterance.includes("it is") || utterance.includes("it's")) &&
(utterance.includes("humid") ||
utterance.includes("rain") ||
utterance.includes("shin") ||
utterance.includes("sunny") ||
utterance.includes("hot") ||
utterance.includes("cold") ||
utterance.includes("freez") ||
utterance.includes("warm") ||
utterance.includes("wind") ||
utterance.includes("fog") ||
utterance.includes("haz") ||
utterance.includes("thunder") ||
utterance.includes("storm") ||
utterance.includes("lightn") ||
utterance.includes("snow") ||
utterance.includes("hail") ||
utterance.includes("clear") ||
utterance.includes("tornado") ||
utterance.includes("hurricane")):
case (utterance.includes("有") || utterance.includes("会") || utterance.includes("吗")) &&
(utterance.includes("下雨") ||
utterance.includes("晒") ||
utterance.includes("晴") ||
utterance.includes("热") ||
utterance.includes("冷") ||
utterance.includes("暖") ||
utterance.includes("风") ||
utterance.includes("雾") ||
utterance.includes("霾") ||
utterance.includes("雷") ||
utterance.includes("风暴") ||
utterance.includes("电") ||
utterance.includes("雪") ||
utterance.includes("雹")):
case utterance.includes("precipitation") || utterance.includes("forecast") || ((utterance.includes("chance") || utterance.includes("possibilit")) && (utterance.includes(" rain") || utterance.includes(" snow"))):
case utterance.includes("降雪") || utterance.includes("降水") || utterance.includes("预报") || ((utterance.includes("概率") || utterance.includes("几率")) && (utterance.includes("降水") || utterance.includes("降雪"))):
fixLocation = false;
break;
case utterance.includes("附近") || utterance.includes(" nearby"):
case utterance.includes("周围") || utterance.includes(" around me") || utterance.includes(" around here"):
case utterance.includes("导航") || utterance.includes("navigat"):
case utterance.includes("方向") || utterance.includes(" direction"):
case utterance.includes("指引") || (utterance.includes("direct ") && utterance.includes(" to ")):
case utterance.includes("指引") || (utterance.includes("guide ") && utterance.includes(" to ")):
case utterance.includes("带我去") || utterance.includes("take me to "):
case utterance.includes("路线") || utterance.includes("route "):
case utterance.includes("路径") || utterance.includes("path to "):
case (utterance.includes("怎样") || utterance.includes("如何") || utterance.includes("怎么")) && (utterance.includes("到") || utterance.includes("去") || utterance.includes("抵达") || utterance.includes("走")):
case utterance.includes("how ") && (utterance.includes(" get ") || utterance.includes(" go ") || utterance.includes(" arrive ") || utterance.includes(" reach ")):
fixLocation = false;
break;
}
body?.queries?.[0]?.profileSlices.forEach(profileSlice => {
switch (profileSlice?.values?.[0]?.value?.typeUrl) {
case "type.googleapis.com/apple.parsec.siri.v2alpha.AppInfo": {
/****************** initialization start *******************/
class ApplicationInfomationRequest$Type extends MessageType {
constructor() {
super("ApplicationInfomationRequest", [
{ no: 2, name: "bundleID", kind: "scalar", T: 9 /*ScalarType.STRING*/ },
{ no: 4, name: "launchIntent", kind: "scalar", T: 9 /*ScalarType.STRING*/ },
]);
}
}
const ApplicationInfomationRequest = new ApplicationInfomationRequest$Type();
/****************** initialization finish *******************/
const AppInfo = ApplicationInfomationRequest.fromBinary(profileSlice?.values?.[0]?.value?.value);
Console.debug(`AppInfo: ${JSON.stringify(AppInfo)}`);
switch (AppInfo?.bundleID) {
case "com.apple.weather":
case "com.heweather.weatherapp":
fixLocation = false;
break;
case "com.apple.store.Jolly":
fixLocation = false;
break;
case "com.apple.Music":
case "com.apple.AppStore":
fixLocation = false;
break;
default:
break;
}
break;
}
case "type.googleapis.com/apple.parsec.siri.v2alpha.SiriKitAppInfo":
break;
case "type.googleapis.com/apple.parsec.siri.v2alpha.AmpUserState":
break;
case "type.googleapis.com/apple.parsec.siri.v2alpha.AudioQueueStateInfo":
break;
}
});
Console.debug(`fixLocation: ${fixLocation}`);
if (fixLocation) delete body?.queryContext?.location;
Console.debug(`body: ${JSON.stringify(body, null, 2)}`);
rawBody = SiriPegasusRequest.toBinary(body);
break;
Expand All @@ -128,15 +270,15 @@ Console.info(`PLATFORM: ${PLATFORM}`);
if ($request.headers["user-agent"]?.includes("grpc-node-js")) $request.headers["user-agent"] = "PegasusKit/1 (Mac16,1; macOS 15.2 24C101) safari/1";
body = SafariSearchRequest.fromBinary(rawBody);
Console.debug(`body: ${JSON.stringify(body, null, 2)}`);
body.queryContext = modifyPegasusQueryContext(body.queryContext, Settings, body.searchQuery?.query);
body.queryContext = modifyPegasusQueryContext(body.queryContext, Settings);
Console.debug(`body: ${JSON.stringify(body, null, 2)}`);
rawBody = SafariSearchRequest.toBinary(body);
break;
case "/apple.parsec.lookup.v1alpha.LookupSearch/LookupSearch": // 查询搜索
if ($request.headers["user-agent"]?.includes("grpc-node-js")) $request.headers["user-agent"] = "PegasusKit/1 (Mac16,1; macOS 15.2 24C101) lookup/1";
body = LookupSearchRequest.fromBinary(rawBody);
Console.debug(`body: ${JSON.stringify(body, null, 2)}`);
body.queryContext = modifyPegasusQueryContext(body.queryContext, Settings, body.searchQuery?.query);
body.queryContext = modifyPegasusQueryContext(body.queryContext, Settings);
Console.debug(`body: ${JSON.stringify(body, null, 2)}`);
rawBody = LookupSearchRequest.toBinary(body);
break;
Expand Down

0 comments on commit f25d0d5

Please sign in to comment.