-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
goodreads-to-obsidian.sh
executable file
·197 lines (158 loc) · 6.19 KB
/
goodreads-to-obsidian.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
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
#!/bin/sh
# Enter urls to your goodreads rss feed below.
# You can find it by navigating to one of your goodreads shelves and
# clicking the "RSS" button at the bottom of the page.
# url for "Currently reading":
url="https://www.goodreads.com/url-to-your-rss-feed-shelf=currently-reading"
# url for "Read":
readurl="https://www.goodreads.com/url-to-your-rss-feed-shelf=read"
# Enter the path to your Vault
vaultpath="Path/to/your/vault"
# Assign times to variables
year=$(date +%Y)
nummonth=$(date +%m)
month=$(date +%B)
# This grabs the data from the currently reading rss feed and formats it
IFS=$'\n' feed=$(curl --silent "$url" | grep -E '(title>|book_large_image_url>|author_name>|book_published>|book_id>)' | \
sed -e 's/<!\[CDATA\[//' -e 's/\]\]>//' \
-e 's/Joschua.s bookshelf: currently-reading//' \
-e 's/<book_large_image_url>//' -e 's/<\/book_large_image_url>/ | /' \
-e 's/<title>//' -e 's/<\/title>/ | /' \
-e 's/<author_name>//' -e 's/<\/author_name>/ | /' \
-e 's/<book_published>//' -e 's/<\/book_published>/ | /' \
-e 's/<book_id>//' -e 's/<\/book_id>/ | /' \
-e 's/^[ \t]*//' -e 's/[ \t]*$//' | \
tail +3 | \
fmt
)
# Grab the bookid from READ data from the url and format it
IFS=$'\n' readfeed=$(curl --silent "$readurl" | grep -E '(book_id>)' | \
sed -e 's/<book_id>//' -e 's/<\/book_id>/ | /' \
-e 's/^[ \t]*//' -e 's/[ \t]*$//' | \
fmt
)
# Turn the data into an array
arr=($(echo $feed | tr "|" "\n")) # CURRENTLY-READING
readarr=($(echo $readfeed | tr "|" "\n")) # READ
# Remove whitespace on each element: CURRENTLY-READING
for (( i = 0 ; i < ${#arr[@]} ; i++ ))
do
arr[$i]=$(echo "${arr[$i]}" | sed -e 's/^[ \t]*//' -e 's/[ \t]*$//')
done
# Remove whitespace on each element: READ
for (( i = 0 ; i < ${#readarr[@]} ; i++ ))
do
readarr[$i]=$(echo "${readarr[$i]}" | sed -e 's/^[ \t]*//' -e 's/[ \t]*$//')
done
# Get the amount of books by dividing array by 5
bookamount=$( expr "${#arr[@]}" / 5)
for (( i = 0 ; i < ${bookamount} ; i++ ))
do
# Create a temporary counter to loop through books
counter=$( expr "$i" \* 5)
# Set variables
bookid=${arr[$( expr "$counter" + 1)]}
# Check if book already exists in note by bookid
if grep -q "${bookid}" -r "${vaultpath}"
then
# code if found
unset arr["$counter"]
unset arr[$( expr "$counter" + 1)]
unset arr[$( expr "$counter" + 2)]
unset arr[$( expr "$counter" + 3)]
unset arr[$( expr "$counter" + 4)]
# code if not found
fi
done
# Reindex array to take away gaps
for i in "${!arr[@]}"; do
new_array+=( "${arr[i]}" )
done
arr=("${new_array[@]}")
unset new_array
# Get the amount of books by dividing array by 5
bookamount=$( expr "${#arr[@]}" / 5)
if (( "$bookamount" == 0 )); then
osascript -e "display notification \"No new books found.\" with title \"Currently-reading: No update\""
fi
# Start the loop for each book
for (( i = 0 ; i < ${bookamount} ; i++ ))
do
counter=$( expr "$i" \* 5)
# Set variables
title=${arr["$counter"]}
bookid=${arr[$( expr "$counter" + 1)]}
imglink=${arr[$( expr "$counter" + 2)]}
author=${arr[$( expr "$counter" + 3)]}
pub=${arr[$( expr "$counter" + 4)]}
# Delete illegal (':' and '/') and unwanted ('#') characters
cleantitle=$(echo "${title}" | sed -e 's/\///' -e 's/:/ –/' -e 's/#//')
# Write the contents for the book file
if [[ "$cleantitle" == "" ]];
then
osascript -e "display notification \"Failed to create note due to empty array.\" with title \"Error!\""
else
echo "---
bookid: ${bookid}
---
links: [[Books MOC]]
#currently-reading
# ${title}
![b|150](${imglink})
* Type: #book/
* Universe/Series: ADD SERIES
* Author: [[${author}]]
* Year published: [[${pub}]]" >> "${vaultpath}/${cleantitle}.md"
# Display a notification when creating the file
osascript -e "display notification \"Booknote created!\" with title \"${cleantitle//\"/\\\"}\""
fi
done
ifbookid=$(find "${vaultpath}" -type f -print0 | xargs -0 grep -li "${cbookid}")
ifcurrread=$(find "${vaultpath}" -type f -print0 | xargs -0 grep -li "#currently-reading")
if find "${vaultpath}" -type f -print0 | xargs -0 grep -li "${cbookid}"
then
# Code if found: update read books
fname=$(find "${vaultpath}" -type f -print0 | xargs -0 grep -li "${cbookid}")
sed -i '' "/Year published: \[\[[0-9][0-9][0-9][0-9]\]\]/ a\\
\* Year read: #read${year}" "$fname"
sed -i '' "/Year read: #read${year}/ a\\
\* Month read: [[${year}-${nummonth}-${month}|${month} ${year}]]" "$fname"
sed -i '' -e 's/#currently-reading/#read/' "$fname"
# Grab the name of the changed book
fname=$(echo ${fname} | sed 's/^.*\///' | sed 's/\.[^.]*$//')
osascript -e "display notification \"${fname}\" with title \"Updated read books\""
else
# code if not found: No new books
osascript -e "display notification \"No new read books.\" with title \"Read: No update\""
fi
for (( i = 0 ; i < ${#readarr[@]} ; i++ ))
do
#circle through bookid array
cbookid=${readarr["$i"]}
# If in the path to the vault, there is a file with the current id, then …
if find "${vaultpath}" -not -path "*/\.*" -type f \( -iname "*.md" \) -print0 | xargs -0 grep -li "${cbookid}"
then
# … set variable fname to that file
fname=$(find "${vaultpath}" -not -path "*/\.*" -type f \( -iname "*.md" \) -print0 | xargs -0 grep -li "${cbookid}")
# Check if it has tag "#currently-reading"
if grep "#currently-reading" "${fname}"
then
# If yes, change the formatting, delete the "#currently-reading" tag
sed -i '' "/Year published: \[\[[0-9][0-9][0-9][0-9]\]\]/ a\\
\* Year read: #read${year}" "$fname"
sed -i '' "/Year read: #read${year}/ a\\
\* Month read: [[${year}-${nummonth}-${month}|${month} ${year}]]" "$fname"
sed -i '' -e 's/#currently-reading/#outline \/ #welcome/' "$fname"
# Grab the name of the changed book
declare -i updatedbooks; updatedbooks+=1
fname=$(echo ${fname} | sed 's/^.*\///' | sed 's/\.[^.]*$//')
# Show notification
osascript -e "display notification \"${fname}\" with title \"Updated read books\""
fi
fi
done
# code if not found: No new books
if [[ ${updatedbooks} = "" ]]
then
osascript -e "display notification \"No new read books.\" with title \"Read: No update\""
fi