-
Notifications
You must be signed in to change notification settings - Fork 0
/
feedy.sh
executable file
·80 lines (63 loc) · 1.65 KB
/
feedy.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
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
feed=$1
print_usage_err() {
echo 'Usage: ./feedy.sh <feed_name>.atom | Creates the feed'
echo 'Usage: ./feedy.sh <feed_name>.atom <entry_title> <entry_link> | Creates a new entry for the feed'
exit 1
}
get_date() {
date +%FT%R:00Z
}
get_id() {
echo urn:uuid:$(uuidgen)
}
get_basic_feed() {
cat << EOF
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>Title of your feed</title>
<subtitle>Subtitle of your feed</subtitle>
<link href="http://domain.com"/>
<link rel="self" href="/feed.atom"/>
EOF
echo "<updated>$(get_date)</updated>"
echo "<id>$(get_id)</id>"
cat << EOF
<author>
<name>Your Name</name>
<email>[email protected]</email>
</author>
</feed>
EOF
}
get_entry() {
title=$1
id=$2
link=$3
echo "<entry>"
echo "<title>$title</title>"
echo "<id>$id</id>"
if [ "$link" != "" ] ; then echo "<link href=\"$link\" rel=\"alternate\"/>" ; fi
echo "<updated>$(get_date)</updated>"
echo "<published>$(get_date)</published>"
echo "</entry>"
}
if [[ $feed != *.atom ]] ; then print_usage_err; fi
if [ ! -f $feed ] ; then
get_basic_feed > $feed
echo The feed was just created. Please manually fill in the feed details.
fi
entry_title="$2"
entry_link="$3"
if [ "$entry_title" == "" ] || [ "$entry_link" == "" ] ; then
print_usage_err
fi
# remove closing feed for adding new entry
sed -i '$d' $feed
# add entry
entry_id=$(get_id)
get_entry "$entry_title" "$entry_id" "$entry_link" >> $feed
# update last updated date
sed -i "0,/^.*updated.*$/s//<updated>$(get_date)<\/updated>/" $feed
# close feed
echo '</feed>' >> $feed
echo Added new feed entry with ID $entry_id