forked from decipher3114/Revancify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch_patches.sh
156 lines (146 loc) · 6.29 KB
/
fetch_patches.sh
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
#!/bin/bash
source="$1"
storagePath="$3"
patchesJson=$(jq '.' "$source"-patches-*.json)
savedJson=$(jq '.' "$storagePath/$source-patches.json" 2>/dev/null || jq -n '[]')
if ! jq -n --argjson savedJson "$savedJson" '$savedJson' > /dev/null 2>&1; then
savedJson=$(jq -n '[]')
fi
allPackages=$(echo "$patchesJson" | jq '[.[].compatiblePackages | if . != null then .[].name else empty end]')
pkgs=$(jq -n --argjson allPackages "$allPackages" '[$allPackages | to_entries[] | .value as $pkg | map($allPackages | to_entries[] | select(.value == $pkg).key)[0] as $index | if $index == .key then $pkg else empty end]')
generatedJson=$(jq --null-input --argjson pkgs "$pkgs" --argjson savedJson "$savedJson" --slurpfile patchesFile "$source"-patches-*.json '
[
$patchesFile[] | . as $patchesJson |
$pkgs[] | . as $pkgName |
{
"pkgName": .,
"appName": (
if (($savedJson | length) != 0) then
($savedJson[] | select(.pkgName == $pkgName) | .appName) // null
else
null
end
),
"apkmirrorAppName": (
if (($savedJson | length) != 0) then
($savedJson[] | select(.pkgName == $pkgName) | .apkmirrorAppName) // null
else
null
end
),
"developerName": (
if (($savedJson | length) != 0) then
($savedJson[] | select(.pkgName == $pkgName) | .developerName) // null
else
null
end
),
"versions": (
[
$patchesJson[] |
.compatiblePackages |
if . != null then
if ((map(.name) | index($pkgName)) != null) then
.[(map(.name) | index($pkgName))].versions | if . != null then .[] else empty end
else
empty
end
else
empty
end] |
unique
),
"includedPatches": (
if (($savedJson | length) != 0) then
[
(
($savedJson[] | select(.pkgName == $pkgName)) |
if ((.includedPatches | length) == 0) then
(
$patchesJson[] |
.name as $patchName |
.use as $use |
.excluded as $excluded |
.compatiblePackages |
if . != null then
if ((((map(.name) | index($pkgName)) != null) or (length == 0)) and (($use == true) or ($excluded == false))) then
$patchName
else
empty
end
else
empty
end
)
else
.includedPatches[]
end) //
(
$patchesJson[] |
.name as $patchName |
.use as $use |
.excluded as $excluded |
.compatiblePackages |
if . != null then
if ((((map(.name) | index($pkgName)) != null) or (length == 0)) and (($use == true) or ($excluded == false))) then
$patchName
else
empty
end
else
"hi"
end
)
]
else
[(
$patchesJson[] |
.name as $patchName |
.use as $use |
.excluded as $excluded |
.compatiblePackages |
if . != null then
if ((((map(.name) | index($pkgName)) != null) or (length == 0)) and (($use == true) or ($excluded == false))) then
$patchName
else
empty
end
else
empty
end
)]
end
)
}
]')
if [ "$2" == "online" ]; then
response=$(curl --fail-early --connect-timeout 2 --max-time 5 -s 'https://www.apkmirror.com/wp-json/apkm/v1/app_exists/' \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
-H 'Authorization: Basic YXBpLXRvb2xib3gtZm9yLWdvb2dsZS1wbGF5OkNiVVcgQVVMZyBNRVJXIHU4M3IgS0s0SCBEbmJL' \
-H 'User-Agent: Mozilla/5.0 (Linux; Android 10) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.5414.86 Mobile Safari/537.36' \
-d "$(jq -n --argjson pkgs "$pkgs" '{"pnames": $pkgs}')")
if ! responseJson=$(
echo "$response" | jq '[.data[] |
if .exists then
({
"key": (.pname),
"value": {
"appName": (.app.name | sub("( -)|( &)|:"; ""; "g") | sub("[()\\|]"; ""; "g") | sub(" *[-, ] *"; "-"; "g") | sub("-Wear-OS"; ""; "g")),
"apkmirrorAppName": (.app.link | sub("-wear-os"; "") | match("(?<=\\/)(((?!\\/).)*)(?=\\/$)").string),
"developerName": (.app.link | match("(?<=apk\\/).*?(?=\\/)").string)
}
})
else
empty
end] | from_entries' 2>/dev/null
); then
echo error
exit 1
fi
generatedJson=$(
jq -n --argjson generatedJson "$generatedJson" --argjson responseJson "$responseJson" '[
$generatedJson[] | .pkgName as $pkgName | (.appName = ($responseJson[$pkgName].appName)) | (.apkmirrorAppName = ($responseJson[$pkgName].apkmirrorAppName)) | (.developerName = ($responseJson[$pkgName].developerName))]'
2>/dev/null || { echo error && exit 1 ;})
fi
echo "$generatedJson" | jq '.' >"$storagePath/$source-patches.json"