forked from bookfere/Calibre-News-Delivery
-
Notifications
You must be signed in to change notification settings - Fork 0
149 lines (146 loc) · 5.43 KB
/
calibre-news.yml
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
name: Calibre News Delivery
run-name: 'Calibre News Delivery: ${{ github.ref_name }}'
on:
workflow_dispatch:
schedule:
- cron: '0 12 * * *'
# push:
# branches: ['master']
permissions:
contents: read
jobs:
worker:
runs-on: ubuntu-latest
environment: calibre-news
env:
output: converted_ebooks
publisher: bookfere.com
author: Calibre News Delivery
relay: ${{ secrets.SMTP }}
port: ${{ secrets.PORT }}
encrypt: ${{ secrets.ENCRYPT }}
secret: ${{ secrets.SECRET }}
from: ${{ secrets.FROM }}
to: ${{ secrets.TO }}
ext: ${{ secrets.FORMAT || 'epub' }}
size: ${{ secrets.SIZE || 25 }}
steps:
- name: Checking Variable
run: |
declare -A variables
declare -a absences
variables=(
'SMTP' "$relay" 'PORT' "$port" 'ENCRYPT' "$encrypt"
'SECRET' "$secret" 'FROM' "$from" 'TO' "$to")
for key in ${!variables[@]}; do
variable=${variables[$key]}
[ -n "$variable" ] || absences+=($key)
done
if [ ${#absences[@]} -gt 0 ]; then
variable_keys=$(echo ${absences[@]/%/,})
echo "Missing variable(s): ${variable_keys/%,/}"; exit 1
fi
- name: Set up Python
if: ${{ success() }}
uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Install Calibre
if: ${{ success() }}
run: |
sudo apt-get update
sudo apt-get install libegl1 libopengl0 libxcb-cursor0
url=https://download.calibre-ebook.com/linux-installer.sh
sudo -v && wget -nv -O- $url | sudo sh /dev/stdin
- name: Retrieving Recipe
if: ${{ success() }}
uses: actions/checkout@v4
- name: Checking Recipe
if: ${{ success() }}
run: |
declare -A recipe_paths
add_recipe() {
local name="$(basename "$recipe_path")"
local key=$(echo "$name" | md5sum | cut -d ' ' -f 1)
recipe_paths[$key]="$recipe_path"
}
if [ -f 'recipe_list.txt' ]; then
while read recipe_path || [ -n "$recipe_path" ]; do
if [ ! -n "$recipe_path" ]; then continue; fi
title="${recipe_path%.recipe}"
result="$(ebook-convert --list-recipes | grep "$title" || true)"
if [ ! -n "$result" ]; then
echo "Recipe \"$recipe_path\" does not exists"; continue
fi
if [ "${recipe_path##*.}" != 'recipe' ]; then
recipe_path="$recipe_path".recipe
fi
add_recipe
done < recipe_list.txt
fi
while read -d '' recipe_path; do
add_recipe
done < <(find . -maxdepth 1 -type f -name '*.recipe' -print0)
for key in ${!recipe_paths[@]}; do
recipe_path="${recipe_paths[$key]}"
echo "Recipe path: $recipe_path"
echo "$recipe_path" >> temp_recipe_list.txt
done
count=${#recipe_paths[@]}
echo "Recipe count: $count"
if [ $count -eq 0 ]; then
echo 'No recipe needs processing'; exit 1
fi
- name: Converting Ebook
if: ${{ success() }}
run: |
mkdir $output
convert_ebook() {
ebook_name="${recipe_path%.*}"
ebook_path="$ebook_name.${ext,,}"
echo "Converting \"$recipe_path\" > \"$ebook_path\"..."
arguments="--authors=\"$author\" --publisher=\"$publisher\""
cover="covers/${ebook_name}.png"
if [ -f "$cover" ]; then arguments+=" --cover=\"$cover\""; fi
style="styles/${ebook_name}.css"
if [ -f "$style" ]; then arguments+=" --extra-css=\"$style\""; fi
echo $arguments | xargs ebook-convert "$recipe_path" "$ebook_path"
title=$(ebook-meta "${ebook_path}" | grep '^Title *:' |
sed 's/^Title *: *\(.*\)/\1/')
new_ebook_path="${output}/${title}.${ext,,}"
echo "Renaming \"$ebook_path\" > \"$new_ebook_path\""
mv "$ebook_path" "$new_ebook_path"
}
while read recipe_path || [ -n "$recipe_path" ]; do
convert_ebook &
done < temp_recipe_list.txt
wait
count=$(find "$output" -type f -name "*.${ext,,}" | wc -l)
echo "Ebook count: $count"
if [ $count -eq 0 ]; then
echo 'No ebook needs send'; exit 1
fi
- name: Sending Ebook
if: ${{ success() }}
run: |
while read -d '' ebook_path; do
filename="$(basename "$ebook_path")"
title="${filename%.*}"
file_size=$(du -m "$ebook_path" | cut -f 1)
if [ $file_size -ge $size ]; then
echo "Size exceeds the limitation: $filename (${file_size}MB)"
continue
fi
echo "Sending: \"$filename\""
calibre-smtp -a "$ebook_path" -r "$relay" --port="$port" \
-e "${encrypt^^}" -u "$from" -p "$secret" -s "$title" \
"$from" "$to" "Deliver \"${title}\""
done < <(find "$output" -type f -name "*.${ext,,}" -print0)
echo "All jobs done"
- name: Storing Ebook
if: ${{ success() }}
uses: actions/upload-artifact@v4
with:
name: Calibre-News-Delivery
path: ${{ env.output }}
retention-days: ${{ secrets.DAYS || 90 }}