Skip to content

Commit 4441edd

Browse files
committed
account for lots of packages in ytoxt
1 parent 3915479 commit 4441edd

File tree

1 file changed

+58
-32
lines changed

1 file changed

+58
-32
lines changed

src/lib/util.sh

Lines changed: 58 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,9 @@ check_limit() {
186186
remaining_time=$((3600 * (hours_passed + 1) - rate_limit_diff))
187187
((remaining_time < BKG_MAX_LEN - script_limit_diff)) || save_and_exit
188188
(($? != 3)) || return 3
189-
echo "Sleeping for $remaining_time seconds..."
189+
start=$(date -u +'%Y-%m-%dT%H:%M:%SZ')
190+
end=$(date -u -d "+${remaining_time} seconds" +'%Y-%m-%dT%H:%M:%SZ')
191+
echo "Sleeping for $remaining_time seconds from $start to $end..."
190192
sleep $remaining_time
191193
echo "Resuming!"
192194
set_BKG BKG_RATE_LIMIT_START "$(date -u +%s)"
@@ -205,7 +207,9 @@ check_limit() {
205207
remaining_time=$((60 * (min_passed + 1) - sec_limit_diff))
206208
((remaining_time < BKG_MAX_LEN - script_limit_diff)) || save_and_exit
207209
(($? != 3)) || return 3
208-
echo "Sleeping for $remaining_time seconds..."
210+
start=$(date -u +'%Y-%m-%dT%H:%M:%SZ')
211+
end=$(date -u -d "+${remaining_time} seconds" +'%Y-%m-%dT%H:%M:%SZ')
212+
echo "Sleeping for $remaining_time seconds from $start to $end..."
209213
sleep $remaining_time
210214
echo "Resuming!"
211215
set_BKG BKG_MIN_RATE_LIMIT_START "$(date -u +%s)"
@@ -442,36 +446,58 @@ ytox() {
442446
ytoxt() {
443447
# ytox + trim if the json or xml is over 50MB, remove oldest versions
444448
while [ -f "$1" ] && [[ "$(ytox "$1")" -ge 50000000 || "$(stat -c %s "$1")" -ge 50000000 ]]; do
445-
jq -e '
446-
if type == "array" then
447-
reduce .[] as $pkg (false; . or (($pkg.version // []) | length > 0))
448-
else
449-
(.version // []) | length > 0
450-
end
451-
' "$1" || break
452-
jq -c '
453-
def trim_versions:
454-
if (.version // []) | length > 0 then
455-
.version |= (
456-
sort_by(
457-
.id | (if type == "string" and test("^-?[0-9]+$") then tonumber else . end)
458-
)
459-
| del(.[0])
460-
)
461-
else
462-
.
463-
end;
464-
def version_count:
465-
(if has("raw_versions") then .raw_versions else (.versions // 0) end)
466-
| (if type == "string" then (if test("^-?[0-9]+$") then tonumber else 0 end) else . end);
467-
if type == "array" then
468-
sort_by(version_count) | reverse
469-
| map(select((.version // []) | length > 0))
470-
| map(trim_versions)
471-
else
472-
trim_versions
473-
end
474-
' "$1" >"$1".tmp
449+
if jq -e '
450+
if type == "array" then
451+
any(.[]; (.version // []) | length > 0)
452+
else
453+
(.version // []) | length > 0
454+
end
455+
' "$1" >/dev/null; then
456+
jq -c '
457+
def trim_versions:
458+
if (.version // []) | length > 0 then
459+
.version |= (
460+
sort_by(.id | (if type == "string" and test("^-?[0-9]+$") then tonumber else . end))
461+
| del(.[0])
462+
)
463+
else
464+
.
465+
end;
466+
if type == "array" then
467+
(to_entries
468+
| (max_by((.value.version // []) | length) // empty) as $max
469+
| map(
470+
if .key == $max.key and ((.value.version // []) | length > 0)
471+
then (.value |= trim_versions)
472+
else .
473+
end
474+
)
475+
| map(.value))
476+
else
477+
trim_versions
478+
end
479+
' "$1" >"$1".tmp
480+
else
481+
jq -c '
482+
if type == "array" then
483+
(
484+
def to_num:
485+
if type == "number" then .
486+
elif type == "string" then tonumber? // 0
487+
else 0 end;
488+
to_entries
489+
| (min_by([ (.value.raw_downloads // 0 | to_num), (.value.date // "") ]) // null) as $target
490+
| if $target == null then
491+
map(.value)
492+
else
493+
[ .[] | select(.key != $target.key) | .value ]
494+
end
495+
)
496+
else
497+
.
498+
end
499+
' "$1" >"$1".tmp
500+
fi
475501
mv "$1".tmp "$1"
476502
done
477503
}

0 commit comments

Comments
 (0)