Advertisement
Guest User

update from mysql

a guest
Nov 27th, 2015
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (*
  2. *)
  3.  
  4. property themonths : {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}
  5. property themonths_index : {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"}
  6.  
  7. property thedays : {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}
  8. property thedays_index : {"1", "2", "3", "4", "5", "6", "7"}
  9.  
  10. property themonthsfr : {"janvier", "fevrier", "mars", "avril", "mai", "juin", "juillet", "août", "septembre", "octobre", "novembre", "decembre"}
  11. property thedaysfr : {"lundi", "mardi", "mercredi", "jeudi", "vendredi", "samedi", "dimanche"}
  12.  
  13. set badlyrics to {"NULL", "missing value"}
  14. set audiobookgenres to {"Audiobook", "Languages"}
  15.  
  16. tell application "iTunes"
  17.     get selection of front browser window
  18.     if (selection of front browser window) is not {} then
  19.         set sel to selection
  20.         repeat with thisTrack in sel
  21.             set updateOk to "yes"
  22.            
  23.             if comment of thisTrack is "" then set updateOk to "no"
  24.             if (disc number of thisTrack as string) is "0" then set updateOk to "no"
  25.             if (disc count of thisTrack as string) is "0" then set updateOk to "no"
  26.             if (track number of thisTrack as string) is "0" then set updateOk to "no"
  27.             if (track count of thisTrack as string) is "0" then set updateOk to "no"
  28.             set newRating to 0
  29.             if updateOk is equal to "yes" then
  30.                 try
  31.                     set sqlResults to my findTrack(track count of thisTrack, album of thisTrack, track number of thisTrack, ¬
  32.                         comment of thisTrack, disc number of thisTrack, disc count of thisTrack)
  33.                     if sqlResults is not equal to "" then
  34.                         set newName to item 1 of sqlResults
  35.                         set newPlayedCount to item 2 of sqlResults
  36.                         set newPlayedDate to item 3 of sqlResults
  37.                         set newYear to item 4 of sqlResults
  38.                         set newRating to item 5 of sqlResults
  39.                         set newArtist to item 6 of sqlResults
  40.                         set newComment to item 7 of sqlResults
  41.                         set newGrouping to item 8 of sqlResults
  42.                         set newComposer to item 9 of sqlResults
  43.                         try
  44.                             set discnumber to item 10 of sqlResults
  45.                         end try
  46.                         try
  47.                             set disccount to item 11 of sqlResults
  48.                         end try
  49.                         try
  50.                             set newbpm to item 12 of sqlResults
  51.                         end try
  52.                        
  53.                         try
  54.                             set trackstart to item 13 of sqlResults
  55.                         end try
  56.                         try
  57.                             set trackfinish to item 14 of sqlResults
  58.                         end try
  59.                         try
  60.                             set dateadded to item 15 of sqlResults
  61.                         end try
  62.                         try
  63.                             set tracklyrics to item 16 of sqlResults
  64.                         end try
  65.                        
  66.                         if genre of thisTrack is in audiobookgenres then
  67.                             if trackstart is not in badlyrics then
  68.                                 set thisTrack's start to trackstart
  69.                             end if
  70.                             if (trackfinish is not in badlyrics) and (thisTrack's duration is greater than trackfinish) then
  71.                                 set thisTrack's finish to trackfinish
  72.                             end if
  73.                         end if
  74.                        
  75.                        
  76.                         considering case
  77.                             if newName is not equal to name of thisTrack then
  78.                                 set name of thisTrack to newName
  79.                             end if
  80.                             if newArtist is not equal to artist of thisTrack then
  81.                                 set artist of thisTrack to newArtist
  82.                             end if
  83.                             if (newComposer is not "") and (newComposer is not equal to composer of thisTrack) then
  84.                                 if (newComposer is not "NULL") then
  85.                                     set composer of thisTrack to newComposer
  86.                                 end if
  87.                             end if
  88.                         end considering
  89.                         if (newYear is not "") and (newYear is not equal to year of thisTrack) then
  90.                             set year of thisTrack to newYear
  91.                         end if
  92.                        
  93.                        
  94.                         -- *********** Lyrics
  95.                         set ChR to ASCII character 13
  96.                         set newlyrics to my replace_chars(tracklyrics, "%return%", ChR)
  97.                         try
  98.                             if (thisTrack's lyrics is not equal to newlyrics) then
  99.                                 set thisTrack's lyrics to newlyrics
  100.                             end if
  101.                             if thisTrack's lyrics is in badlyrics then set thisTrack's lyrics to ""
  102.                         end try
  103.                        
  104.                         --                      if (newbpm is not "") and (newbpm is not equal to bpm of thisTrack) then
  105.                         --                          try
  106.                         --                              set bpm of thisTrack to newbpm
  107.                         --                          end try
  108.                         --                      end if
  109.                         try
  110.                             if newGrouping is not equal to "NULL" and grouping of thisTrack is not equal to newGrouping then
  111.                                 set grouping of thisTrack to newGrouping
  112.                             end if
  113.                         end try
  114.                        
  115.                         --                      set comment of thisTrack to newComment
  116.                        
  117.                         --                      if thisTrack's grouping is in badlyrics then set thisTrack's grouping to ""
  118.                        
  119.                         try
  120.                             if (newPlayedCount as integer) is greater than or equal to (played count of thisTrack) then
  121.                                 set played count of thisTrack to newPlayedCount
  122.                                 try
  123.                                     set newPlayedDate to my convertdate(newPlayedDate)
  124.                                     set played date of thisTrack to date newPlayedDate
  125.                                 end try
  126.                             end if
  127.                         end try
  128.                         try
  129.                             --                          set date added of thisTrack to date dateadded
  130.                             --                          set date added of thisTrack to my convertdate(dateadded)
  131.                         end try
  132.                        
  133.                     end if
  134.                 end try
  135.                 if newRating as integer is greater than 0 then
  136.                     set rating of thisTrack to newRating
  137.                 else
  138.                     --my growl(rating of thisTrack as string)
  139.                     if rating of thisTrack is equal to 0 then
  140.                         set rating of thisTrack to 40
  141.                     end if
  142.                 end if
  143.                
  144.             end if
  145.         end repeat
  146.     end if
  147.     my growl("Updated tracks from MySQL")
  148. end tell
  149.  
  150. on findTrack(trackCount, trackAlbum, track_number, trackComment, discnumber, disccount)
  151.     set trackAlbum to my escapeString(trackAlbum)
  152.     set trackComment to my escapeString(trackComment)
  153.    
  154.     if trackComment starts with "http" then
  155.         set oldDelim to AppleScript's text item delimiters
  156.         set AppleScript's text item delimiters to {" "}
  157.         set thestring to text items of trackComment
  158.         set AppleScript's text item delimiters to oldDelim
  159.         set commentBegin to item 1 of thestring
  160.     else
  161.         if trackComment = "" then
  162.             set commentBegin to "unknown"
  163.         else
  164.             set commentBegin to word 1 of trackComment
  165.         end if
  166.     end if
  167.    
  168.     set theQuery to "WHERE track_number ='" & (track_number as string) & "' && track_count='" & (trackCount as string) & "' "
  169.    
  170.     if the length of commentBegin is 10 then
  171.         set theQuery to theQuery & "&& comment LIKE '%" & commentBegin & "%' "
  172.         set cdEnd to true
  173.     else
  174.         set theQuery to theQuery & "&& comment LIKE '" & commentBegin & "%' "
  175.         set cdEnd to true
  176.     end if
  177.    
  178.     -- set newAlbum to getAlbumName(trackAlbum)
  179.     --  set theQuery to theQuery & "&& album like '" & newAlbum & "%' "
  180.     set theQuery to theQuery & "&& discnumber ='" & discnumber & "' "
  181.     set theQuery to theQuery & "&& disccount ='" & disccount & "' "
  182.     -- delimiters should be set back
  183.     set text item delimiters to {"  "}
  184.     return the text items of paragraph 2 of sqlSelect("name, played_count, played_date, year, rating, artist, comment, grouping, composer, discnumber, disccount, bpm, starttime, finishtime, date_added, lyrics", theQuery)
  185. end findTrack
  186.  
  187. on sqlSelect(fieldsToGet, myString)
  188.     set theQuery to "/usr/local/bin/mysql -u root -D 'itunes'  -e \"SELECT " & fieldsToGet & " FROM itunes " & myString & "\""
  189.     --set the clipboard to myString
  190.     return do shell script theQuery
  191. end sqlSelect
  192.  
  193. on escapeString(newString)
  194.     set newString to replace_chars(newString, "\"", "\\\"")
  195.     set newString to replace_chars(newString, "'", "\\'")
  196.     return newString
  197. end escapeString
  198.  
  199.  
  200. on getAlbumName(theAlbum)
  201.     set AppleScript's text item delimiters to " "
  202.     set the item_list to every text item of theAlbum
  203.     if item -1 of item_list starts with "CD" then
  204.         set newAlbum to items 1 thru -2 of item_list as string
  205.     else
  206.         set newAlbum to the item_list as string
  207.     end if
  208.     set AppleScript's text item delimiters to ""
  209.     return newAlbum
  210. end getAlbumName
  211.  
  212. on replace_chars(this_text, search_string, replacement_string)
  213.     set AppleScript's text item delimiters to the search_string
  214.     set the item_list to every text item of this_text
  215.     set AppleScript's text item delimiters to the replacement_string
  216.     set this_text to the item_list as string
  217.     set AppleScript's text item delimiters to ""
  218.     return this_text
  219. end replace_chars
  220.  
  221. on convertdate(oldDate)
  222.     -- french to english so far
  223.     set tid to AppleScript's text item delimiters
  224.     set AppleScript's text item delimiters to " "
  225.     set testEnglish to text item 1 of oldDate as string
  226.     if testEnglish is in thedaysfr then
  227.         set AppleScript's text item delimiters to " "
  228.         set datedayofWeek to text item 1 of oldDate
  229.         set dateTemp to text item 2 of oldDate
  230.         set dateMonth to text item 3 of oldDate
  231.         set dateYear to text item 4 of oldDate
  232.         set dateDay to text item 2 of oldDate
  233.        
  234.        
  235.         repeat with x from 1 to 12
  236.             if dateMonth = (item x of themonthsfr) then
  237.                 set dateMonth to x as string
  238.                 exit repeat
  239.             end if
  240.         end repeat
  241.        
  242.        
  243.         repeat with x from 1 to 7
  244.             if datedayofWeek = (item x of thedaysfr) then
  245.                 set datedayofWeek to x as integer
  246.                 exit repeat
  247.             end if
  248.         end repeat
  249.         set newDate to (item datedayofWeek of thedays as string) & ", "
  250.         set newDate to newDate & (item dateMonth of themonths as string) & ", " & dateDay
  251.        
  252.         set datedayofWeek to item datedayofWeek of thedaysfr
  253.         set newDate to newDate & ", " & text item 4 of oldDate & " " & text item 5 of oldDate
  254.     else
  255.         set newDate to oldDate
  256.        
  257.     end if
  258.     set AppleScript's text item delimiters to tid
  259.    
  260.     return newDate
  261. end convertdate
  262.  
  263. on growl(myString)
  264.     tell application "Growl"
  265.         notify with name ¬
  266.             "AppleScript" title "iTunes" description myString application name ¬
  267.             "AppleScript" icon of file "file:///Applications/iTunes.app"
  268.     end tell
  269.     return true
  270. end growl
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement