-
Notifications
You must be signed in to change notification settings - Fork 1
/
check.sh
executable file
·44 lines (35 loc) · 1.48 KB
/
check.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
#!/usr/bin/env sh
# Checks all products in the DB for changes
# This script should be set as a cronjob to check the products automatically
# Source the config file
. "$(dirname "$0")/data/config.sh"
# Loop through all products
sqlite3 "$db" 'SELECT id FROM urls ORDER BY title' | while read -r id; do
# Get current date
now="$(date '+%d-%m-%Y %H:%M:%S')"
# Get the last known price
price="$(sqlite3 "$db" 'SELECT price FROM prices WHERE url_id = '$id' ORDER BY id DESC LIMIT 1;')"
# Get the URL
url="$(sqlite3 "$db" 'SELECT url FROM urls WHERE id = '$id';')"
[ -z "$url" ] && exit
# Get the parser
parser="$(echo "$url" | awk -F[/:] '{print $4}' | tr -cd '[:alnum:]')"
# Run parser
result="$(sh "$parsers_folder/$parser".sh "$url")"
# Get the title, new price and the extras
title="$(echo "$result" | grep "title/" | cut -d/ -f 2)"
new_price="$(echo "$result" | grep "price/" | cut -d/ -f 2)"
extras="$(echo "$result" | grep "extras/" | cut -d/ -f 2)"
# If the price is different save the new one and call the change checker script
old="$(echo "$price" | sed -e 's/,/./g' -e 's/[^0-9.]*//g')"
new="$(echo "$new_price" | sed -e 's/,/./g' -e 's/[^0-9.]*//g')"
if [ "$old" != "$new" ]; then
sqlite3 "$db" "INSERT INTO prices (url_id, price, created) VALUES ('$id', '$new_price', '$now');"
sh "$change_checker" "$price" "$new_price" "$title" "$extras" "$url"
fi
done
if [ "$debug" -gt 0 ]; then
path="$logs/$(date +%Y%m%d%H%M)/"
mkdir -p "$path"
mv "$logs/"*html "$path"
fi