-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdilbert
More file actions
executable file
·177 lines (151 loc) · 4.36 KB
/
dilbert
File metadata and controls
executable file
·177 lines (151 loc) · 4.36 KB
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
#!/usr/bin/expect
set IMAGE_PATH "~/Downloads/dilberts/"
set BACKGROUND_PATH "~/Downloads/dilbert.png"
##################################################
#
# Main
#
# Main program entry point
# (Please keep this on top. Other procs should be
# sorted alphabetically)
#
##################################################
proc main {args} {
global MONTAGE
global CURL
# Process --merge argument
if {[lsearch $args --merge] > -1} { set MERGE true } else { set MERGE false }
# Find image magick
set MONTAGE [require "montage" "Image Magick"]
# Find curl
set CURL [require "curl"]
# Download the latest comics, and if some downloaded merge is always true
if {[downloadComics 9] > 0} { set MERGE true }
# If we need to merge comics
if {$MERGE == true} {
# Merge the comics
mergeComics 9
}
# Update the desktop background
updateDesktopBackground
}
##################################################
#
# DownloadComics
#
# Download the N latest comics
#
# @param count The number of comics to download
#
##################################################
proc downloadComics {count} {
global CURL
global IMAGE_PATH
# Make sure the image path exists
file mkdir [file nativename $IMAGE_PATH]
# Get the current time
set seconds [clock seconds]
# The number of downloaded items
set n 0
# Download comics for each day
for {set i 0} {$i < $count} {incr i 1} {
# Make a datestamp for this day
set dateStamp [clock format [clock add $seconds -$i day] -format "%Y-%m-%d"]
# Calculate the image path on the file system
set imagePath [string trimright [file nativename $IMAGE_PATH] /]/$dateStamp.gif
# If the file doesn't exist, then download it
if {![file exists $imagePath]} {
# If this isn't the first item, then let the server breathe
if {$n > 0} {
# Let the server rest
puts "Letting the server breathe"
sleep 2
}
puts "Downloading comic for $dateStamp"
# Get the page for the image
set html [exec $CURL -s "http://dilbert.com/strips/comic/$dateStamp/"]
# Find the image url
regexp -- {http://dilbert.com/dyn/str_strip[^"]*} $html imageURL #" <-- This is to close the quote for vim's sake
# Download the image
exec $CURL -so "$imagePath" "$imageURL"
# Increment n
incr n 1
}
}
# Return the number we actually downloaded
return $n
}
##################################################
#
# Fail
#
# Print an error message to STDERR and exit
#
##################################################
proc fail {message} {
puts stderr "$message\n"
exit 1;
}
##################################################
#
# MergeComics
#
# Merge the latest $count comics
#
# @param count the number of comics to merge
#
##################################################
proc mergeComics {count} {
global IMAGE_PATH
global BACKGROUND_PATH
global MONTAGE
# Get the comics from IMAGE_PATH
set files [glob -dir [file nativename $IMAGE_PATH] *]
# Get the last $count files
set files [lrange $files end-[expr $count - 1] end]
# ImageMagick the files!
puts "Merging images"
exec -ignorestderr $MONTAGE {*}$files -geometry +10+10 -shadow -background none -font Times-Roman [file nativename $BACKGROUND_PATH]
}
##################################################
#
# Require
#
# Check to see if a program exists. If it
# doesn't, then throw an error
#
##################################################
proc require {command {name ""}} {
# If the name isn't specified, then set it to the command
if {$name == ""} { set name $command }
# Find the command
if { [catch {set path [exec which $command]} ] } {
fail "Cannot find $name"
}
return $path
}
##################################################
#
# Update Desktop Background
#
# Set the desktop background to the background
# image and refresh it
#
##################################################
proc updateDesktopBackground {} {
global BACKGROUND_PATH
# Get the native path to the image
set backgroundPath [file nativename $BACKGROUND_PATH]
puts "Updating desktop"
# Tell AppleScript tp update the background
exec osascript -e [subst {
tell application "Finder"
set thePath to "$backgroundPath" as POSIX file
--set desktop picture to the first item of folder {"Macintosh HD:Library:Desktop Pictures:Solid Colors"} as alias
set desktop picture to thePath as alias
tell application "Dock" to quit
end tell
}]
}
main {*}$argv
# vim: set ft=expect :