set viddlerUser to "PUT-USER-HERE" set viddlerPassword to "PUT-PASSWORD-HERE" property apiKey : "PUT-API-KEY-HERE" property apiURL : "http://api.viddler.com/rest/v1/" (* #################################### Viddler API AppleScript Wrapper Christopher Masto Public Domain; No rights reserved. Version 0.1 #################################### *) property lf : (ASCII character 10) global sessionID global tempFile set tempFile to "" (* ################ Example Usage ################ *) --user_authenticate(viddlerUser, viddlerPassword) set videos to videos_listfeatured("", "", "") tell application "System Events" set featVideo to first XML element of videos whose name is "video" set videoID to value of XML element "id" of featVideo set desc to value of XML element "description" of featVideo set vidTitle to value of XML element "title" of featVideo set author to value of XML element "author" of featVideo set vidURL to value of XML element "url" of featVideo end tell set answer to display dialog "Today's featured video is '" & vidTitle & "' by " & author & lf & lf & desc  with title "Featured Video" buttons {"Cancel", "Open in Browser"} default button "Open in Browser" cancel button "Cancel" if answer's button returned is "Open in Browser" then open location "http://" & vidURL clean_up() (* ################ API functions ################ *) on get_viddler_xml for method given session:session, resultNode:resultNode, params:params set methodURL to apiURL & "?api_key=" & apiKey & "&method=" & method if session then if sessionID = "" then error "Login required for " & method set methodURL to methodURL & "&sessionid=" & sessionID end if if length of params > 0 then set methodURL to methodURL & "&" & encode_params(params) tell application "URL Access Scripting" if tempFile = "" then set tempFile to download (methodURL)  to (path to temporary items as string) & "viddler.xml" else download (methodURL) to tempFile replacing yes end if end tell tell application "System Events" try set tempXML to XML file (tempFile as string) set resultNodes to every XML element of tempXML whose name is resultNode -- Got nothing, maybe it's an error? if length of resultNodes is 0 then  set resultNodes to every XML element of tempXML whose name is "error" on error set resultNodes to {} end try if length of resultNodes is 1 then copy first item of resultNodes to resultXML else --set fileNum to open for access tempFile --error "Unexpected result: " & (read tempFile) error "Couldn't understand response from Viddler" end if end tell -- Comment out if callers handle their own errors tell application "System Events" to  if resultXML's name is "error" then error (method & " got error " & resultXML's value) return resultXML end get_viddler_xml on clean_up() if tempFile ­ "" then tell application "System Events" to delete file (tempFile as string) end clean_up on user_authenticate(username, password) set result to get_viddler_xml for "user.authenticate" without session given resultNode:"sessionid", params:{"user", username, "password", password} tell application "System Events" to  if result's name is "sessionid" then set sessionID to result's value return sessionID end user_authenticate on user_profile(username) return get_viddler_xml for "user.profile" without session given resultNode:"user_profile", params:{"user", username} end user_profile on video_status(videoID) return get_viddler_xml for "video.status" with session given resultNode:"upload_status", params:{"video_id", videoID} end video_status on videos_listfeatured(tag, page, per_page) return get_viddler_xml for "videos.list_featured" without session given resultNode:"video_list", params:{"tag", tag, "page", page, "per_page", per_page} end videos_listfeatured on video_upload from filename given title:title, tags:tags, description:desc, category:category -- XXX - error if no sessionid set uploadURL to apiURL & "?api_key=" & apiKey & "&method=video.upload" & "&sessionid=" & sessionID -- no go here - can't do HTTP POST from AppleScript :-( end video_upload (* ################ Helper functions ################ *) on encode_params(params) set encoded to "" repeat with p from 1 to count of params by 2 set n to item p of params set v to item (p + 1) of params if p > 1 then set encoded to encoded & "&" set encoded to encoded & n & "=" & encode_text(v, true, true) end repeat return encoded end encode_params on encode_char(this_char) set the ASCII_num to (the ASCII number this_char) set the hex_list to {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"} set x to item ((ASCII_num div 16) + 1) of the hex_list set y to item ((ASCII_num mod 16) + 1) of the hex_list return ("%" & x & y) as string end encode_char -- this sub-routine is used to encode text on encode_text(this_text, encode_URL_A, encode_URL_B) set the standard_characters to "abcdefghijklmnopqrstuvwxyz0123456789" set the URL_A_chars to "$+!'/?;&@=#%><{}[]\"~`^\\|*" set the URL_B_chars to ".-_:" set the acceptable_characters to the standard_characters if encode_URL_A is false then set the acceptable_characters to the acceptable_characters & the URL_A_chars if encode_URL_B is false then set the acceptable_characters to the acceptable_characters & the URL_B_chars set the encoded_text to "" repeat with this_char in this_text if this_char is in the acceptable_characters then set the encoded_text to (the encoded_text & this_char) else set the encoded_text to (the encoded_text & encode_char(this_char)) as string end if end repeat return the encoded_text end encode_text