-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrssh
executable file
·83 lines (72 loc) · 1.85 KB
/
rssh
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
#!/bin/bash
max_item_count=10
xml_executable="xml"
function get_exit_code {
type $1 >/dev/null 2>&1
local return_=$?
return $return_
}
function check_for_xmlstarlet {
local return_=1
if get_exit_code xmlstarlet || get_exit_code xml; then
local return_=0
fi
return $return_
}
if get_exit_code xmlstarlet; then
xml_executable="xmlstarlet"
fi
if ! check_for_xmlstarlet; then
echo "XMLStarlet not installed."
exit 1
fi
XDG_CONFIG_HOME=${XDG_CONFIG_HOME:-${HOME}/.config}
config_folder="${XDG_CONFIG_HOME}/rssh"
# Serialize items to be served
# $1 - Channel title
# $2 - Item link
# $3 - item title
serialize() {
echo -e "[$1] \e]8;;$2\e\\$3\e]8;;\e\\"
}
parse_xml() {
title="$($xml_executable sel -t -v "//rss/channel/title" <<< "$1")"
item_count=$($xml_executable sel -t -c "count(//rss/channel/item)" <<< "$1")
if (( $item_count>$max_item_count )); then
item_count=$max_item_count
fi
for (( i=1; i<=$item_count; i++ ))
do
item_title="$($xml_executable sel -t -v "//rss/channel/item[$i]/title" <<< "$1")"
item_link="$($xml_executable sel -t -v "//rss/channel/item[$i]/link" <<< "$1")"
serialize "$title" "$item_link" "$item_title"
done
}
read_and_serve_feed_file() {
title=$(sed -n 1p $1)
printf "### $title\n"
declare -a entries=($(awk '(NR>1)' "$1"))
for entry in "${entries[@]}"
do
parse_xml "$(curl --silent $entry)"
done
printf '\n'
}
check_feed_files() {
if [[ ! -d $config_folder ]]; then
# No config folder, create it.
mkdir -p ${config_folder}
touch ${config_folder}/default.feed
sed -i "1i# This is your default feed." ${config_folder}/default.feed
fi
cd $config_folder
}
read_config_folder() {
check_feed_files
declare -a files=($(ls $config_folder | grep .feed))
for file in "${files[@]}"
do
read_and_serve_feed_file $file
done
}
read_config_folder