-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathet
executable file
·753 lines (665 loc) · 19 KB
/
et
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
#!/usr/bin/env bash
#
# Copyright © 2014, David McIntosh <[email protected]>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided this permission
# notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.
typeset -i ID_DIGITS=12
typeset -i SUCCESS=0
typeset -i FAIL=1
shopt -s extglob
typeset -A cookie_file_globs=(
[firefox]="${HOME}/.mozilla/firefox/*.default/cookies.sqlite"
[chromium]="${HOME}/.config/chromium/Default/Cookies"
[chrome]="${HOME}/.config/google-chrome/Default/Cookies"
[firefox_mac]="${HOME}/Library/Application Support/Firefox/Profiles/*.default/cookies.sqlite"
[chromium_mac]="${HOME}/Library/Application Support/Chromium/Default/Cookies"
[chrome_mac]="${HOME}/Library/Application Support/Google/Chrome/Default/Cookies"
)
typeset -A cookie_queries=(
[firef]="SELECT name, value FROM moz_cookies WHERE baseDomain = '%s'"
[chrom]="SELECT name, value FROM cookies WHERE hostkey LIKE '%%%s'"
)
# Return success if the given value is a valid Ello id (padded or unpadded)
# is_id candidate
function is_id
{
local cand=$1
local exp="^0*[0-9]{1,${ID_DIGITS}}$"
[[ "${cand}" =~ ${exp} ]]
}
# If the given parameter is a valid id, identify the corresponding file and
# echo that filename to standard out. Else, echo to standard output unchanged.
# If the file does not exist, or no file is found for the id, return failure,
# else return success.
# filename_or_id id|filename
function filename_or_id
{
local -i id
local glob
local filename
if is_id "$1"; then
id=${1##+(0)}
glob=post-$(printf "%0${ID_DIGITS}d*(-+([a-zA-Z0-9_])).md" ${id})
# note: glob should be unquoted
filename=$(echo ${glob})
else
filename=$1
fi
echo "${filename}"
[[ -f "${filename}" ]] || return ${FAIL}
return ${SUCCESS}
}
# Returns success if the current directory is an initialized VCS repository
function has_repo
{
if [[ -d './.hg' ]]; then
echo 'hg'
return ${SUCCESS}
elif [[ -d './.git' ]]; then
echo 'git'
return ${SUCCESS}
fi
return ${FAIL}
}
# Returns success if the current directory has uncommitted version control
# changes
# has_uncommitted_changes hg | git
function has_uncommitted_changes
{
local repo_type=$1
local -i count
if [[ "${repo_type}" == 'hg' ]]; then
count=$(hg status | wc -l)
elif [[ "${repo_type}" == 'git' ]]; then
count=$(git status -s | wc -l)
fi
(( count ))
return
}
# A simple confirmation notice, requiring the user to type 'yes' in upper or
# lower case if -e is specified, a non-confirming response will exit
# confirm [-e] message
function confirm
{
local -l input
local -i do_exit
if [[ $1 == '-e' ]]; then
do_exit=1
shift
else
do_exit=0
fi
echo $*
read input
while [[ "${input}" != 'yes' ]]; do
if (( do_exit )); then
exit ${SUCCESS}
fi
read input
done
}
# Retreieve a key value pair from a conf file
# get_conf filename key
function get_conf
{
local conf_file=$1
local key=$2
local value
local pair
if [[ ! -f "${conf_file}" ]]; then
return ${FAIL}
fi
pair=$(grep -E "^${key}\\s*=" "${conf_file}")
if (( $? != ${SUCCESS} )); then
return ${FAIL}
fi
value=${pair#*=}
echo "${value## }"
return ${SUCCESS}
}
# Write a key value pair to a conf file
# write_conf filename key value
function write_conf
{
local conf_file=$1
local key=$2
local value=$3
local temp_conf_file=$1.$$
if [[ -f "${conf_file}" ]]; then
grep -v -E "^${key}\\s*=" "${conf_file}" > "${temp_conf_file}"
fi
echo "${key} = ${value}" >> "${temp_conf_file}"
mv "${temp_conf_file}" "${conf_file}"
return ${SUCCESS}
}
# Print the file corresponding to the given post id to the stdout
# get_filename id
function get_filename
{
local id=$1
local patt="%0${ID_DIGITS}d"
for file in post-$(printf "${patt}" "${id}")*(-+([0-9A-Za-z_])).md
do
[[ -f "${file}" ]] || continue
echo "${file}"
return ${SUCCESS}
done
return ${FAIL}
}
# Format an individual post by removing leading and trailing quotes and
# replacing <br> and \n with newlines. The formatted file will be written to
# stdout
# format_post filename
function format_post
{
awk '\
/^["]/ {
innerstr = substr($0, 2, length($0) - 2);
gsub("<br>", "\n", innerstr);
gsub(/\\n/, "\n", innerstr);
gsub(/\\["]/, "\"", innerstr);
print innerstr
}
/^[^"]/ { print $0; }' < $1
}
# Write a post to disc, naming it for the Ello API post id
# The post should be provided via stdin
function write_post
{
local id
local filename=''
local patt
local -i first=1
local err
local tmp
filename=$(jq '.id,.created_at,.body[].data' | while IFS='' read -r line
do
if (( first )); then
id=${line}
filename=$(get_filename ${id})
if (( $? )); then
# if no existing file, select new filename
patt="%0${ID_DIGITS}d"
filename="post-$(printf "${patt}" "${id}").md"
fi
exec 8> "${filename}"
first=0
# echo "Parsing post ${id}" >&2
echo "${filename}"
else
echo "$line" >&8
fi
done)
err=${PIPESTATUS[0]}
exec 8>&-
if (( err )); then
return $err
fi
tmp=${filename}.$$
format_post "${filename}" > "${tmp}"
mv "${tmp}" "${filename}"
}
# Perform an action on each post in an Ello JSON dump
# The JSON dump should be provided via stdin
# each_post action
function each_post
{
local line
local post
while IFS='' read -r line
do
if [[ "${line:0:1}" == "{" ]]; then
post=${line}
else
post=${post}${line}
fi
if [[ "${line:0:1}" == "}" ]]; then
echo "${post}" | $1
if (( $? )); then
echo "${post}"
fi
fi
done
}
# Exit if the unprefixed id format is in use in the current directory
function old_ids
{
local filename
for filename in post-+([1-9][0-9])*(-+([a-zA-Z0-9_])).md
do
[[ -f "${filename}" ]] || continue
echo "You appear to have a repository using the old unprefixed filename format. Please run '$0 migrate'"
exit 1
done
}
# Pull the posts from a given Ello username into the current directory
# pull [-f filename.json] [-a] [-p] [-b browser] [username]
function pull
{
local infile
local -i with_assets
local -i private_acct
local browser
local username
local src
local curl_cmd
local cookie_string
local repo_type
local -i count
local -i rc
old_ids
while getopts 'i:apb:' arg;
do
case "${arg}" in
i) infile=${OPTARG}
;;
a) with_assets=1
;;
p) private_acct=1
;;
b) browser=${OPTARG}
;;
esac
done
shift $((OPTIND-1))
# If a username provided, write it to the config file
if (( $# > 0 )); then
username=$1
src="https://ello.co/${username}.json"
write_conf './.et' 'source' "${src}"
# Else, if no input file provided, require that the config file contain a
# username
elif ! [[ -v infile ]]; then
src=$(get_conf './.et' 'source')
if (( $? )); then
exit ${FAIL};
fi
fi
curl_cmd=( curl )
if [[ -v private_acct ]]; then
if [[ ! -v browser ]]; then
browser=$(get_conf "./.et" "browser")
if (( $? )); then
echo "No browser specified in configuration file" >&2
exit 8
fi
fi
cookie_string=$(get_cookie_string "${browser}" "ello.co")
rc=$?
(( rc )) && exit ${rc}
if [[ -n "${cookie_string}" ]]; then
curl_cmd=( "${curl_cmd[@]}" -b "${cookie_string}" )
write_conf "./.et" "browser" "${browser}"
else
exit 8
fi
fi
# Count the number of markdown files
count=0
for i in post-*.md
do
[[ -f "${i}" ]] && (( count++ ))
done
repo_type=$(has_repo)
rc=$?
if (( count )); then
if (( rc )); then
confirm -e "This is a destructive activity and will overwrite all previously retrieved posts. You do not appear to have a version control system active in this directory. Are you sure you wish to do this? (Type 'yes' to confirm)"
elif has_uncommitted_changes "${repo_type}"; then
confirm -e "This is a destructive activity and will overwrite all previously retrieved posts. You appear to have uncommitted changed to posts in this directory (using version control system: ${repo_type}). Are you sure you wish to do this? (Type 'yes' to confirm)"
fi
fi
# If an infile specified, use instead of curl
if [[ -v infile ]]; then
cat "${infile}"
else
"${curl_cmd[@]}" "${src}"
fi | jq '.posts[] | {id: .id, created_at: .created_at, body: .body}' | \
each_post write_post
if [[ -v with_assets ]]; then
pull_assets
fi
}
# Get the appropriate rename command for the repo format in use
function get_rename_cmd
{
local -a rename_cmd
local repo_type=$(has_repo)
case "${repo_type}" in
hg)
rename_cmd=(hg rename)
;;
git)
rename_cmd=(git mv)
;;
*)
rename_cmd=(mv)
;;
esac
echo "${rename_cmd[@]}"
}
# If there exists files using an unpadded id, migrate these to the padded
# format
function migrate
{
local -a rename_cmd=( $(get_rename_cmd) )
local filename_exp='post-([0-9]+)(-[a-zA-Z0-9_]+)*\.md'
local src
local id
local tags
local patt
local dest
for src in post-+([0-9])*(-+([a-zA-Z0-9_])).md
do
if [[ ${src} =~ ${filename_exp} ]]; then
id=${BASH_REMATCH[1]}
tags=${src##post-+([0-9])}
patt="post-%0${ID_DIGITS}d%s"
dest=$(printf "${patt}" "${id}" "${tags}")
"${rename_cmd[@]}" "${src}" "${dest}"
fi
done
}
# Return success if the first argument is equal to one of the remaining arguments
# in_array needle arg2 [arg3] ...
function in_array
{
local needle=$1
shift
while (( $# > 0 ));
do
[[ "${needle}" == "$1" ]] && return ${SUCCESS}
shift
done
return ${FAIL}
}
# Using the first argument as a joining string, echo the remaining arguments
# concatenated with that joining string
# join_array glue arg2 [arg3] ...
function join_array
{
local glue=$1
shift
while (( $# > 0 ));
do
echo -n "$1"
shift
(( $# > 0 )) && echo -n "${glue}"
done
}
# Apply tags to a given file
# tag filename tag1 [tag2] ...
function tag
{
old_ids
if (( $# < 2 )); then
exit
fi
local -a rename_cmd=( $(get_rename_cmd) )
local filename
local new_filename
local -i rc
local -a new_tags
local -a tags
local stem
local filename_exp
local id
local tags_str
local -i ind
filename=$(filename_or_id $1)
rc=$?
if (( rc )); then
case ${rc} in
1) echo "Supplied id is not present"
exit ${FAIL}
;;
2) echo "Supplied filename is not present"
exit ${FAIL}
;;
*) exit ${rc}
;;
esac
fi
shift
new_tags=("$@")
stem=${filename%.md}
filename_exp='post-([0-9]+)(-[a-zA-Z0-9_]+)*\.md'
if [[ ${filename} =~ ${filename_exp} ]]; then
id=${BASH_REMATCH[1]}
if [[ -n "${BASH_REMATCH[2]}" ]]; then
tags_str=${stem#post-${id}-}
if (( ${#tags_str} )); then
IFS=- tags=(${tags_str})
fi
fi
if (( ${#new_tags[@]} )); then
for tag in "${new_tags[@]}"
do
if [[ -v tags ]]; then
ind=${#tags[@]}
else
ind=0
fi
if (( ind )) && in_array "${tag}" "${tags[@]}"; then
continue
fi
tags[${ind}]=${tag}
done
fi
new_filename=$(printf \
post-%s-%s.md "${id}" "$(join_array '-' "${tags[@]}")")
"${rename_cmd[@]}" "${filename}" "${new_filename}"
fi
}
# Return the first item in a given glob
# first_in_glob glob
function first_in_glob
{
local glob=$1
eval "for i in ${glob};
do
[[ -f \"\$i\" ]] && echo \$i && return ${SUCCESS};
done"
return ${FAIL}
}
# Retrieve the cookies for a given browser and domain
# get_cookie_table browser domain
function get_cookie_table
{
local browser=$1
local filename=$(first_in_glob "${cookie_file_globs[${browser}]}")
local format=${cookie_queries[${browser:0:5}]}
local domain=$2
if [[ ! -f "${filename}" ]]; then
echo "Specified browser cookie file not available" >&2
return 8
fi
sqlite3 "${filename}" "$(printf "${format}" "${domain}")"
}
# Retrieve the cookie string to be passed to curl for a given browser and
# domain
# get_cookie_string browser domain
function get_cookie_string
{
local browser=$1
local domain=$2
# Use _mac suffix if running on Mac OS X
if [[ $(uname -s) == "Darwin" ]]; then
browser="${browser%_mac}_mac"
fi
if [[ ! -v cookie_file_globs[$browser] ]]; then
echo "Unknown browser '$browser'" >&2
exit 8
fi
local str=$(get_cookie_table "$browser" "$domain" |
while IFS="|" read -a fields;
do
echo -n "${fields[0]}=${fields[1]};"
done)
if [[ -n "${str}" ]]; then
echo "${str:0:$(( ${#str} - 1 ))}"
fi
}
# Read a file and print tuples describing the assets contained within
# file_assets filename
function file_assets
{
awk '\
BEGIN { in_asset=0; }
/^\{$/ {
in_asset=1; has_url=0; has_alt=0; has_id=0;
}
/\"url\":/ {
has_url=1; split($0, parts, "\""); url=parts[4];
}
/\"alt\":/ {
has_alt=1; split($0, parts, "\""); alt=parts[4];
}
/\"asset_id\":/ {
has_id=1; split($0, parts, "\""); id=substr(parts[3], 3);
}
/^}$/ {
if (in_asset && has_url && has_id) {
if (!has_alt) {
alt = "optimized.jpg";
}
print id "|" url "|" alt
}
in_asset=0
}' < "$1"
}
# Download the specified asset if it does not already exist
# retrieve_assets post_id asset_id url filename_suffix
function retrieve_asset
{
local post_id=$1
local asset_id=$2
local url=$3
local filename_suffix=$4
local padded_addet_id
local path
local -i rc
if [[ "${url:0:2}" == '//' ]]; then
url="https:${url}"
fi
padded_addet_id=$(printf "%0${ID_DIGITS}d" "${asset_id}")
path="assets/${post_id}/${padded_addet_id}-${filename_suffix}"
mkdir -p "${path%/*}"
if ! [[ -f "${path}" ]]; then
curl -o "${path}" "${url}"
rc=$?
if (( rc )); then
echo "\
Warning: could not retrieve asset ${asset_id} for post ${post_id}" >&2
return ${rc}
fi
fi
}
# Perform the specified command on each asset in turn as read from the standard
# input, an asset being defined as a pipe delimited tuple of asset_id, url and
# filename_suffix
# each_asset command < asset_list
function each_asset
{
(( $# )) || return
local cmd=( "${@}" )
while IFS="|" read asset_id url filename_suffix remainder
do
"${cmd[@]}" "${asset_id}" "${url}" "${filename_suffix}"
done
}
# Retrieve all assets associated with retrieved posts
function pull_assets
{
old_ids
local filename_exp
local post_id
for src in post-+([0-9])*(-+([a-zA-Z0-9_])).md
do
filename_exp='post-([0-9]+)(-[a-zA-Z0-9_]+)*\.md'
if [[ "${src}" =~ ${filename_exp} ]]; then
post_id=${BASH_REMATCH[1]}
file_assets "${src}" |
each_asset retrieve_asset "${post_id}"
fi
done
}
# Print usage
function usage
{
local cmd=${0##*/}
echo "USAGE: ${cmd} command [options ...]
Where command is one of:
pull Retrieve the latest published articles from a given user:
${cmd} pull [username]
Specify a username to retrieve posts from
subsequently, this username may be omitted
as it will be written to the .et conf file
in the current directory
This is intended to be used in conjunction with a version
control tool such as git or mercurial to allow you to more
easily track the changes you have made to your Ello posts
and comments.
Options:
-i filename.json
Pull changes from a json file retrieved manually.
-a
Following successful pull, retrieve post assets.
-p
Attempt to retrieve the data using browser credentials.
-b browser
Specify the browser to use for retreiving credentials.
If not supplied, the value in the .et conf file will
be used. If supplied, the conf file value will be
overwritten.
tag Apply tag(s) to a given file:
${cmd} tag filename tag1 tag2 ...
${cmd} tag id tag1 tag2 ...
Any tags already present in the filename will be ignored.
If the current directory is in version control, the VCS
will be used to rename the file. Else, the OS's mv
command will be used.
pull_assets
Retrieve all assets associated with previously retrieved posts
Assets are downloaded to:
./assets/<post_id>/<asset_id>-<asset_filename>"
}
# if file was run as executable (as opposed to sourced)
if [[ "$0" == "${BASH_SOURCE}" ]]; then
set -u
# Parse command line
if (( $# < 1 )); then
usage
exit ${FAIL}
fi
command=$1
shift
case "${command}" in
pull)
pull "$@"
;;
migrate)
migrate "$@"
;;
tag)
tag "$@"
;;
pull_assets)
pull_assets "$@"
;;
*)
usage
exit ${FAIL}
;;
esac
fi