Created
September 19, 2011 09:16
-
-
Save tresni/1226188 to your computer and use it in GitHub Desktop.
Set a random movie barcode as the desktop image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
on GrowlNotify(message) | |
(* And if Growl is installed, send a notification *) | |
try | |
tell application "Growl" | |
register as application "Movie Barcode Random Desktop" all notifications {"Desktop Change"} default notifications {"Desktop Change"} icon of application "Script Editor" | |
notify with name "Desktop Change" title "Desktop Change" description message application name "Movie Barcode Random Desktop" | |
end tell | |
on error | |
-- do nothings | |
end try | |
end GrowlNotify | |
on replaceString(theText, oldString, newString) | |
set AppleScript's text item delimiters to oldString | |
set tempList to every text item of theText | |
set AppleScript's text item delimiters to newString | |
set theText to the tempList as string | |
set AppleScript's text item delimiters to "" | |
return theText | |
end replaceString | |
on Download(thisUrl, localFile) | |
do shell script "curl -s -L " & thisUrl & " -o '" & POSIX path of localFile & "'" | |
end Download | |
(* Movie index cache *) | |
set myFolder to (path to library folder from user domain as string) & "Application Support:Movie Barcoder:" | |
set f to myFolder & "moviebarcode-index" | |
tell application "Finder" | |
if not (exists myFolder) then | |
make new folder at (path to library folder from user domain as string) & "Application Support:" with properties {name:"Movie Barcoder"} | |
end if | |
if exists f then | |
(* If older then 2 days, let's get a new one! *) | |
if (current date) - (modification date of file f) > 48 * 60 * 60 then | |
delete f | |
end if | |
end if | |
(* No movie index file, let's get one! *) | |
if not (exists f) then | |
my Download("http://moviebarcode.tumblr.com/movie-index", f) | |
end if | |
end tell | |
(* gotta get some regex action going on. Split movies onto their own line, then get it in a format we can use *) | |
set cmdString to "grep -oE '(http:\\/\\/moviebarcode.tumblr.com[^\\\"]*)\\\">([^<]*)' \"" & (POSIX path of f) & "\" | sed -nE \"s/&/\\&/g;s/([^\\\"]+)\\\">(.*) \\(([0-9]+)\\)/\\2|\\3|\\1/p\"" | |
set cmdResult to do shell script cmdString | |
(* select a movie *) | |
set movieItems to paragraphs of cmdResult | |
set idx to random number from 1 to length of movieItems | |
set movieInfo to item idx of movieItems | |
set AppleScript's text item delimiters to "|" | |
set movieItems to every text item of movieInfo | |
set AppleScript's text item delimiters to "" | |
(* temporary files *) | |
set basePath to myFolder & "movie-data" & idx | |
set html to basePath & ".html" | |
set jpg to basePath & ".jpg" | |
(* final files *) | |
set movieName to text item 1 of movieItems | |
set movieName to my replaceString(movieName, "\\", "") | |
set movieName to my replaceString(movieName, ":", "") | |
set movieName to my replaceString(movieName, "|", "") | |
set movieName to my replaceString(movieName, "?", "") | |
set movieName to my replaceString(movieName, "%", "") | |
set movieName to my replaceString(movieName, "*", "") | |
set movieName to my replaceString(movieName, "\"", "") | |
set movieName to my replaceString(movieName, "<", "") | |
set movieName to my replaceString(movieName, ">", "") | |
set movieName to my replaceString(movieName, "/", "") | |
set movieName to my replaceString(movieName, ".", "") | |
set finalHtml to myFolder & movieName & ".html" | |
set finalImg to myFolder & movieName & ".jpg" | |
tell application "Finder" | |
(* Don't redownload if we still have the movie file. Index may change, so use movie name *) | |
if not (exists finalHtml) then | |
my Download(text item 3 of movieItems, html) | |
set name of file html to (movieName & ".html") | |
end if | |
(* Some of the titles contain colons which break this, but I'm too lazy/tired to fix atm *) | |
if exists finalHtml then | |
if not (exists finalImg) then | |
(* more regex to get image file *) | |
set cmdString to "grep -oE '<img src=\"http://[0-9]*.media.tumblr.com/[^\"]*' \"" & (POSIX path of finalHtml) & "\" | head -n 1 | grep -oE 'http://[0-9]*.media.tumblr.com/[^\"]*'" | |
set imageFile to do shell script cmdString | |
my Download(imageFile, jpg) | |
set name of file jpg to (movieName & ".jpg") | |
end if | |
end if | |
(* If we have a final jpg file, let's set the desktop background *) | |
if exists finalImg then | |
tell application "System Events" to set picture of desktop 1 to (finalImg as alias) | |
my GrowlNotify((text item 1 of movieItems) & " (" & (text item 2 of movieItems) & ")") | |
else | |
my GrowlNotify("Failed to create image file.") | |
end if | |
end tell |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Apparently this will fail on Lion as URL Access Scripting was removed... Safari or cURL may work, something to look into later.