#!/bin/bash
function fetch_blogs () {
cd -- "/var/www/www.cozynet.org/html/"
id='blog_cats'
cd blogs/
parse_blogs && echo "Completed."
}
function parse_blogs () {
for file in *_blog.html;
do
[[ -f "$file" ]] || continue
blog_cats=$(cat "${file}" | head -n13 | grep "\' | sed 's/, /,/g')
blog_title=$(cat "${file}" | head -n13 | grep \
| sed 's| ||g' | sed 's|||g')
# Using a pipe "|" for a delimiter since colons and semi-colons are already being used. The pipe will be converted to a new line later.
blog_index+=$(echo "${file}:${blog_cats}:${blog_title}|")
done
parse_cats
}
function parse_cats {
contents=("${blog_index[0]}")
all_cats=$(echo "${contents}" | tr '|' '\n' | cut -d':' -f'2' | tr ',' '\n' | sed '/^$/d' | sort)
uniq_cats=$(echo "${all_cats}" | uniq)
declare -a catsArray
while read -r line
do
catsArray+=("${line}")
done <<< "${uniq_cats}"
for line in "${catsArray[@]}"
do
cat_count=$(echo "${all_cats}" | grep -w "${line}" | wc -l)
cat_stat+=("${line} (${cat_count});")
links=$(echo "${contents}" | tr '|' '\n' | cut -d':' -f'1,2' | grep -w "${line[@]}" | cut -d':' -f'1' | tr '\n' ',')
for link_line in `echo "${links}"`
do
parse_links=$(echo "${link_line}" | tr ',' '\n')
for index_link in `echo "${parse_links}"`
do
raw_link+=$(echo "${contents}" | tr '|' '\n' | cut -d':' -f'1,3' | grep -w "${index_link}" | sed 's/&/\&/g' | sed 's/$/†/')
done
raw_links+=$(echo "${raw_link};" | sed 's/†;/;/g')
raw_link=''
done
cat_links+=$(echo "${links};" | sed 's/,;/;/g')
done
update_db
}
function update_db () {
touch "/var/www/www.cozynet.org/db/${id}_index.db"
# Clear the file
:> "/var/www/www.cozynet.org/db/${id}_index.db"
echo "${cat_stat[*]}" | sed 's/;$//g' >> "/var/www/www.cozynet.org/db/${id}_index.db"
echo "${cat_links[*]}" | sed 's/;$//g' >> "/var/www/www.cozynet.org/db/${id}_index.db"
echo "${raw_links[*]}" >> "/var/www/www.cozynet.org/db/${id}_index.db"
}
fetch_blogs