diff options
Diffstat (limited to 'fdroids.sh')
-rwxr-xr-x | fdroids.sh | 122 |
1 files changed, 87 insertions, 35 deletions
@@ -1,49 +1,101 @@ | |||
1 | #!/bin/bash | 1 | #!/bin/bash |
2 | set -e | 2 | set -e |
3 | quiet() { "$@" >/dev/null 2>&1; } | 3 | shopt -s lastpipe |
4 | quiet dpkg-query -W html-xml-utils || sudo apt install html-xml-utils | 4 | |
5 | stale() { [ $(( $(date +%s) - $(stat -c%Y "$1") )) -gt $(( 60*60*12 )) ]; } | 5 | baseurl=https://f-droid.org |
6 | stale_hours=12 | ||
7 | |||
8 | quiet() | ||
9 | { | ||
10 | "$@" >/dev/null 2>&1 | ||
11 | } | ||
12 | |||
13 | stale() | ||
14 | { | ||
15 | [ ! -e "$1" ] || | ||
16 | [ $(( $(date +%s) - $(stat -c%Y "$1") )) -gt $(( 60*60*stale_hours )) ]; | ||
17 | } | ||
18 | |||
6 | wget_with_log() | 19 | wget_with_log() |
7 | { | 20 | { |
8 | touch "$1" | 21 | set -- wget-log.txt "$@" |
22 | [ -e "$1" ] || touch "$1" | ||
9 | flock "$1" wget -a "$1" "${@:2}" | 23 | flock "$1" wget -a "$1" "${@:2}" |
10 | } | 24 | } |
25 | |||
26 | list_existing() | ||
27 | { | ||
28 | find repo/ -maxdepth 1 -type d | | ||
29 | xargs bash -c 'printf "%s\n" "${@#repo/}"' bash | ||
30 | } | ||
31 | |||
32 | parse_pkgname() | ||
33 | { | ||
34 | # Set package on succ | ||
35 | set -- "${1%/}" | ||
36 | case "$1" in | ||
37 | '' | $baseurl/??/packages/*/* | $baseurl/packages/*/* ) | ||
38 | false ;; | ||
39 | $baseurl/??/packages/* | $baseurl/packages/* ) | ||
40 | echo "${1#*/packages/}" ;; | ||
41 | */* ) | ||
42 | false ;; | ||
43 | * ) | ||
44 | echo "$1" | ||
45 | ;; | ||
46 | esac | ||
47 | } | ||
48 | |||
49 | fetch_pkg() | ||
50 | { | ||
51 | local pkg="$1" | ||
52 | url=$baseurl/en/packages/$url | ||
53 | if wget_with_log -c -O index.html "$url" | ||
54 | then | ||
55 | # Wget might not touch file if it is up-to-date | ||
56 | # with server | ||
57 | touch "$pkgdir"/index.html | ||
58 | else | ||
59 | echo "failed to fetch url ${url@Q}" >&2 | ||
60 | return | ||
61 | fi | ||
62 | |||
63 | if hxwls "$pkgdir"/index.html | | ||
64 | egrep "^$baseurl/repo/${pkg}_[0-9]+\.apk\$" | | ||
65 | sort -n | | ||
66 | tail -n1 | | ||
67 | read link && | ||
68 | [ "$link" ] | ||
69 | then | ||
70 | (cd "$pkgdir" && | ||
71 | wget_with_log -c "$link" "$link.asc") | ||
72 | else | ||
73 | echo "failed to fetch package ${pkg@Q}" >&2 | ||
74 | fi | ||
75 | } | ||
76 | |||
77 | quiet dpkg-query -W html-xml-utils || sudo apt install html-xml-utils | ||
78 | |||
11 | if [ -t 0 ] | 79 | if [ -t 0 ] |
12 | then | 80 | then |
13 | exec < fdroids.txt | 81 | exec < <(list_existing) |
14 | fi | 82 | fi |
15 | while read url | 83 | while read |
16 | do | 84 | do |
17 | baseurl=https://f-droid.org | 85 | [ "$REPLY" ] || continue |
18 | url=${url%/} | 86 | if ! parse_pkgname "$REPLY" | read pkg || ! [ "$pkg" ] |
19 | case "$url" in | 87 | then |
20 | $baseurl/??/packages/*/* | $baseurl/packages/*/* ) | 88 | echo "Skipping invalid URL: ${REPLY@Q}" >&2 |
21 | continue ;; | 89 | fi |
22 | $baseurl/??/packages/* | $baseurl/packages/* ) | 90 | pkgdir=repo/$pkg |
23 | pkg=${url#*/packages/} ;; | 91 | mkdir -p "$pkgdir" |
24 | * ) continue ;; | 92 | if stale "$pkgdir"/index.html |
25 | esac | ||
26 | html=repo/$pkg/index.html | ||
27 | if [ ! -e "$html" ] || stale "$html" | ||
28 | then | 93 | then |
29 | ( | ||
30 | echo "$pkg" >&2 | 94 | echo "$pkg" >&2 |
31 | mkdir -p "${html%/*}" | 95 | ( |
32 | cd "${html%/*}" || continue | 96 | cd "$pkgdir" |
33 | if wget_with_log wget-log.txt -c -O "$html" "$url" | 97 | fetch_pkg "$pkg" |
34 | then | 98 | ) |
35 | touch "$html" | 99 | break |
36 | else | ||
37 | continue | ||
38 | fi | ||
39 | link=$(hxwls "$html" | egrep "/repo/${pkg}_[0-9]+\.apk\$" | sort -n | tail -n1) | ||
40 | if [ "$link" ] && | ||
41 | wget_with_log wget-log.txt -c "$link" "$link.asc" | ||
42 | then | ||
43 | continue | ||
44 | else | ||
45 | echo "failed to fetch $pkg" >&2 | ||
46 | fi | ||
47 | ) | ||
48 | fi | 100 | fi |
49 | done | 101 | done |