~trs-80/hf-price-check

hf-price-check/hf-price-check -rwxr-xr-x 6.3 KiB
08e7fdf0TRS-80 Change name of LICENSE file to COPYING. 3 months ago
                                                                                
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/bin/bash

# Copyright (C) 2020 TRS-80
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# this program. If not, see https://www.gnu.org/licenses.


#
# Project home: https://sr.ht/~trs-80/hf-price-check
#


# Commentary
#
# Extracts a list of lot numbers from a todo.txt file, then searches for each of
# them on hfqpdb.com to see if they have appeared (only posted coupons appear in
# this list).  If a match is found, notification is sent via XMPP to a specified
# address.




# Functions

f_load_config(){

    # N.B.: (From the point of view of writing software, anyway), it's a bad
    # idea to try and put config anywhere else, especially in the same directory
    # as the script.  See also: https://mywiki.wooledge.org/BashFAQ/028
    
    configpath="$HOME/.config/hf-price-check/hfpc.conf"
    
    if [[ -r "$configpath" ]]; then

	echo "Using config file found at $configpath."

	source "$configpath"

    fi
}


f_build_notification(){

    # Function to build up a notification message.  Accepts one argument, the
    # output you want to append to the notification, which will get sent once at
    # the very end of script.  Also echoes each part to STDOUT as we go.
    append="$1"

    echo "$append"

    notification+=("${append}\n")
}


f_parse_todo_txt(){

    # Search through given todo.txt file for keyword and extract all relevant
    # lot numbers, putting them into an array.  Multiple lot numbers per line /
    # item are now supported, but they have to be at the end of the line and
    # space separated.
    
    array_lot_numbers=()
    
    for i in $(cat $todo_txt_file | grep -v "$todo_txt_hidden_tag" | \
		   grep -v '^x ' | \
		   grep -E "${todo_txt_keyword}" | \
		   grep -E -o '([[:digit:]]{4,} ?)+$' | sed -e 's/ /\n/' ); do
		   # old way
		   #sed -E -n "s/^.*${todo_txt_keyword}([[:digit:]]{3,}).*$/\1/p"

	array_lot_numbers+=("$i")
	
    done
}


f_wget_error_check(){

    # We check the exit code from wget to make sure the page downloaded
    # succesfully.  And if not, there is no need to continue.

    if [[ "$1" == "0" ]]; then

	a="Web page downloaded successfully."

	f_build_notification "$a"

    else

	a="Error downloading HFQPDB.com web page!"

	f_build_notification "$a"

    fi
    
}




# Main Program Loop

f_load_config

f_parse_todo_txt

count_lot_numbers=$(echo "${#array_lot_numbers[@]}")

if [[ "$count_lot_numbers" == "0" ]]; then

    # If there are no lot numbers parsed out of todo.txt, we send a notification
    # and exit, without even downloading the HFQPDB web page.
    
    echo "$script_name script ran on $HOSTNAME but found no tagged lot numbers" \
	 "in $todo_txt_file." | sendxmpp -t "$notify_xmpp"

else

    # Add first part to the notification we are building up.
    a="$script_name script running on $HOSTNAME, downloading HFQPDB.com and "
    a+="searching it for $count_lot_numbers lot number(s)..."

    f_build_notification "$a"

    # Only now that we know there are lot numbers to look for, do we download
    # the web page.  And also only if $testing is not set to yes.
    if [[ "$testing" == 'yes' ]]; then

	a="Test run only!  HFQPDB.com will not actually be downloaded!"

	f_build_notification "$a"
	
	web_page_result=$(cat "$test_web_page_result_file" 2>&1 )
	
    else

	echo "We are NOT testing!"
	
	web_page_result=$(wget -qO - "$web_page_url" 2>&1 )

	f_wget_error_check "$?"
	    
    fi
    

    # Again only once we are already downloading the web page, we do a test to
    # make sure our regex still appears to be working.  We should receive
    # messages about regex with lots of results, otherwise probably something
    # broke.  Note -c count returns count of lines with matches (there will be a
    # few matches per line).
    lot_no_regex_test_count=$(echo "$web_page_result" | \
			   grep -c -E -e 'Lot No\. [[:digit:]/]* Valid:')
    
    a="Lot no. regex test returned $lot_no_regex_test_count results (if 0, the "
    a+="website probably changed and broke our regex)."

    f_build_notification "$a"
    

    # Search through downloaded web page for each lot number.
    for lot_no in "${array_lot_numbers[@]}"; do

	# For debugging only
	#printf 'Searching for lot number: %s\n' "$lot_no"
	
	# I had to beef up the grep to eliminate false positives from similar
	# numbers elsewhere in results like coupon codes or picture file names,
	# etc.  It should also allow for zero or more other lot numbers and "/"
	# either before or after, but if there is anything to test for this
	# would probably be it as I suppose this could break over time.  Also,
	# we will need to make sure to manually keep this in sync with the very
	# similar $lot_no_regex_test_count, above).  Then sed just removes
	# spaces at beginning and end of result.
	result=$(echo "$web_page_result" | \
		     grep -E "Lot No\. [[:digit:]/]*$lot_no[[:digit:]/]* Valid:" | \
		     sed -E -e 's/^[ \t]*//' -e 's/[ \t]*$//')

	# Send a different notification whether the result came up with anything
	# or not.  Again, adding everything bit by bit to the notification we
	# are building up (to send once at end).
	if [[ -z "$result" ]]; then

	    # I comment this out because it was just getting to be too much
	    # message, once I started adding enough lot nos.
	    #a="No result for lot number $lot_no."
	    #f_build_notification "$a"

	    # OK I guess we have to do something, or else we get an error.
	    foo=bar
	    #printf 'No result for lot number %s.\n\n' "$lot_no"

	else

	    a="Hit for lot number $lot_no!  http://www.hfqpdb.com/search/$lot_no"
	    
	    f_build_notification "$a"

	    ((hit++))
	fi
	
    done

    
    if [[ -z "$hit" ]]; then

	a="Searched for $count_lot_numbers lot numbers, but no hits."

    else

	a="Hit on $hit results total."

    fi

    f_build_notification "$a"

fi    


# Finally, we send the notification we have been buuilding up.
printf '%b' "${notification[@]}" | sendxmpp -t "$notify_xmpp"