property done_foldername : "Processed posts"


-- since file types are optional in Mac OS X, 

-- check the name extension if there is no file type 

-- NOTE: do not use periods (.) with the items in the name extensions list 

-- eg: {"txt", "text", "jpg", "jpeg"}, NOT: {".txt", ".text", ".jpg", ".jpeg"} 

property extension_list : {"txt", "html", "md", "markdown"}







on adding folder items to this_folder after receiving these_items

-- CHECK FOR THE DESTINATION FOLDER WITHIN THE ATTACHED FOLDER

-- IF IT DOESN'T EXIST, THEN CREATE IT

tell application "Finder"

if not (exists folder done_foldername of this_folder) then

make new folder at this_folder with properties {name:done_foldername}

set current view of container window of this_folder to list view

end if

set the target_folder to folder done_foldername of this_folder

end tell

-- PROCESS EACH OF THE ITEMS ADDED TO THE ATTACHED FOLDER

try

repeat with i from 1 to number of items in these_items

set this_item to item i of these_items

set the item_info to the info for this_item

-- CHECK TO SEE IF THE ITEM IS AN IMAGE FILE OF THE ACCEPTED FILE TYPE

if (alias of the item_info is false and the name extension of the item_info is in the extension_list) then

tell application "Finder"

-- LOOK FOR EXISTING MATCHING ITEMS IN THE DESTINATION FOLDER

-- IF THERE ARE MATCHES, THEN RENAME THE CONFLICTING FILES INCREMENTALLY

my resolve_conflicts(this_item, target_folder)

-- MOVE THE ITEM TO THE DESTINATION FOLDER

set the target_file to (move this_item to the target_folder with replacing) as alias

end tell

-- PROCESS THE ITEM

process_item(target_file)

end if

end repeat

on error error_message number error_number

if the error_number is not -128 then

--tell application "Finder"

-- activate

-- display dialog error_message buttons {"Cancel"} default button 1 giving up after 120

--end tell

--NEED to record ERRORS

end if

end try

end adding folder items to


on resolve_conflicts(this_item, target_folder)

tell application "Finder"

set the file_name to the name of this_item

if (exists document file file_name of target_folder) then

set file_extension to the name extension of this_item

if the file_extension is "" then

set the trimmed_name to the file_name

else

set the trimmed_name to text 1 thru -((length of file_extension) + 2) of the file_name

end if

set the name_increment to 1

repeat

set the new_name to (the trimmed_name & "." & (name_increment as string) & "." & file_extension) as string

if not (exists document file new_name of the target_folder) then

-- rename to conflicting file

set the name of document file file_name of the target_folder to the new_name

exit repeat

else

set the name_increment to the name_increment + 1

end if

end repeat

end if

end tell

end resolve_conflicts


-- this sub-routine processes files 

on process_item(this_item)

-- NOTE that the variable this_item is a file reference in alias format 

-- FILE PROCESSING STATEMENTS GOES HERE 

try

set the_page to readFile(this_item) --the_page is the webpage to extract the mp3 file from

set mp3filelist to do shell script "cat /Users/johnjohn/Dropbox/action/nothuffduffer/files/filelist.txt"

set thisdate to RFCdate(current date)

set qChar to ASCII character 34

set RSStop to "<?xml version='1.0' encoding='ISO-8859-1'?><rss xmlns:itunes='http://www.itunes.com/dtds/podcast-1.0.dtd' version='2.0'><channel><description>Not Huffduffer</description><link>http://www.example.com</link>  <title>Not Huffduffer</title><pubDate>" & thisdate & "</pubDate>"

set RSSbottom to "</channel></rss>"

set newfiles to getAudioFromWebPage(the_page)

--tell application "Finder" to display dialog newfiles

repeat with _URL in paragraphs of newfiles

if (offset of "http://" in _URL) is not 0 then

if (offset of _URL in mp3filelist) = 0 then set mp3filelist to _URL & "•" & thisdate & return & mp3filelist

end if

end repeat

do shell script "echo " & mp3filelist & " > /Users/johnjohn/Dropbox/action/nothuffduffer/files/filelist.txt"

set theItems to ""

repeat with mp3AndTABandDate in paragraphs of mp3filelist

set oldDelim to AppleScript's text item delimiters

set AppleScript's text item delimiters to "•"

set theTextItems to text items of mp3AndTABandDate

set mp3path to item 1 of theTextItems

set pubDate to item 2 of theTextItems

set theTitle to fileNameFromURL(mp3path)

set theItems to theItems & "<item><title>" & theTitle & "</title><description></description><link>" & mp3path & "</link><guid>" & mp3path & "</guid><enclosure url='" & mp3path & "'/><pubDate>" & pubDate & " </pubDate></item>"

set AppleScript's text item delimiters to oldDelim

end repeat

set theRSSData to RSStop & theItems & RSSbottom

do shell script "echo " & qChar & theRSSData & qChar & " > /Users/johnjohn/Dropbox/action/nothuffduffer/files/podcast.rss"

end try

end process_item


on getAudioFromWebPage(the_url)

set _URLs to do shell script "/System/Library/Automator/Get\\ Link\\ URLs\\ from\\ Webpages.action/Contents/Resources/links " & the_url

set mp3_URLs to ""

repeat with _URL in paragraphs of _URLs

if _URL contains ".mp3" then set mp3_URLs to mp3_URLs & _URL & (ASCII character 10)

if _URL contains ".m4a" then set mp3_URLs to mp3_URLs & _URL & (ASCII character 10)

end repeat

return mp3_URLs

end getAudioFromWebPage


on fileNameFromURL(theURL)

set oldDelim to AppleScript's text item delimiters

set AppleScript's text item delimiters to "/"

set theTextItems to text items of theURL

set fname to last item of theTextItems

set AppleScript's text item delimiters to oldDelim

return fname

end fileNameFromURL


on readFile(this_file)

set foo to (open for access (this_file))

set txt to (read foo for (get eof foo))

close access foo

return txt

end readFile


on RFCdate(theDate)

-- RFCdate by Joe Maller http://www.joemaller.com

-- I had to truncate the Month to get feed to valadate JJ

set theTimeZone to do shell script "date +%z" -- returns the timezone as ±HHMM

if weekday of theDate = Sunday then set theWeekday to "Sun, " -- included ", " to save a string later on

if weekday of theDate = Monday then set theWeekday to "Mon, "

if weekday of theDate = Tuesday then set theWeekday to "Tue, "

if weekday of theDate = Wednesday then set theWeekday to "Wed, "

if weekday of theDate = Thursday then set theWeekday to "Thu, "

if weekday of theDate = Friday then set theWeekday to "Fri, "

if weekday of theDate = Saturday then set theWeekday to "Sat, "

set theHours to characters -2 thru -1 of ("00" & (time of theDate) div hours) as string

set theMinutes to characters -2 thru -1 of ("00" & ((time of theDate)) div minutes - theHours * minutes) as string

set theSeconds to characters -2 thru -1 of ("00" & (time of theDate) mod 60) as string

set theMonth to characters 1 thru 3 of (month of theDate as string)

set theDay to characters -2 thru -1 of ("0" & day of theDate) -- zeropad

return theWeekday & theDay & " " & theMonth & " " & year of theDate & " " & theHours & ":" & theMinutes & ":" & theSeconds & " " & theTimeZone

end RFCdate