001/* 002 * $Source: $ 003 * $Name: $ 004 * 005 * $Revision: 563 $ 006 * $Date: 2012-11-03 19:28:37 -0700 (Sat, 03 Nov 2012) $ 007 * $Locker: $ 008 * 009 * 010 * Written by Tom Gutwin - WebARTS Design. 011 * Copyright (C) 2007 WebARTS Design, North Vancouver Canada 012 * http://www.webarts.bc.ca 013 * 014 * This program is free software; you can redistribute it and/or modify 015 * it under the terms of the GNU General Public License version 2 as 016 * published by the Free Software Foundation. 017 * 018 * This program is distributed in the hope that it will be useful, 019 * but WITHOUT ANY WARRANTY; without even the implied warranty of 020 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 021 * GNU General Public License for more details. 022 * 023 * You should have received a copy of the GNU General Public License 024 * along with this program; if not, write to the Free Software 025 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 026 */ 027 028package ca.bc.webarts.tools; 029 030import ca.bc.webarts.widgets.Util; 031import ca.bc.webarts.widgets.random.RandomSample; 032import java.io.File; 033import java.io.IOException; 034import java.util.Vector; 035import java.util.Random; 036 037class RandomFileSelector 038{ 039 /** The users home ditrectory. */ 040 public static String USERHOME = System.getProperty("user.home"); 041 042 /** The users pwd ditrectory. */ 043 public static String USERDIR = System.getProperty("user.dir"); 044 045 /** The file extension (type) to load **/ 046 public static String FILETYPE = "ogg"; 047 048 public static boolean testing = false; 049 public static boolean createSubDirs = true; 050 public static boolean doClear = false; 051 public static Vector<File> songs = new Vector(); 052 public static Random rand = new Random(); 053 public static int maxFiles = 250; 054 public static boolean maxOut = false; 055 056 057 public static void main (String [] args) 058 { 059 String fileType = FILETYPE; 060 if (args.length >0) 061 { 062 if (args.length >1 && args[0].equals("-testing")) testing = true; 063 if (args.length >1 && args[0].equals("-clear")) doClear = true; 064 if (args.length >1 && args[0].equals("-type")) fileType = args[1]; 065 if (args.length >1 && args[0].equals("-fill")) maxOut = true; 066 067 if (args.length >2 && args[1].equals("-type")) fileType = args[2]; 068 if (args.length >2 && args[1].equals("-testing")) testing = true; 069 if (args.length >2 && args[1].equals("-clear")) doClear = true; 070 if (args.length >2 && args[1].equals("-fill")) maxOut = true; 071 072 if (args.length >3 && args[2].equals("-type")) fileType = args[3]; 073 if (args.length >3 && args[2].equals("-testing")) testing = true; 074 if (args.length >3 && args[2].equals("-clear")) doClear = true; 075 if (args.length >3 && args[2].equals("-fill")) maxOut = true; 076 077 if (args.length >4 && args[3].equals("-type")) fileType = args[4]; 078 if (args.length >4 && args[3].equals("-testing")) testing = true; 079 if (args.length >4 && args[3].equals("-clear")) doClear = true; 080 if (args.length >4 && args[3].equals("-fill")) maxOut = true; 081 082 if (args.length >5 && args[4].equals("-type")) fileType = args[5]; 083 if (args.length >5 && args[4].equals("-testing")) testing = true; 084 if (args.length >5 && args[4].equals("-clear")) doClear = true; 085 if (args.length >5 && args[4].equals("-fill")) maxOut = true; 086 087 if (args.length >6 && args[5].equals("-type")) fileType = args[6]; 088 if (args.length >6 && args[5].equals("-testing")) testing = true; 089 if (args.length >6 && args[5].equals("-clear")) doClear = true; 090 if (args.length >6 && args[5].equals("-fill")) maxOut = true; 091 092 String songDir = args[args.length-2]; 093 String deviceDir = args[args.length-1]; 094 File songDirFile = new File(songDir); 095 File deviceDirFile = new File(deviceDir); 096 if (testing) System.out.println(" TESTING ONLY "); 097 if (doClear) System.out.println(" Clearing output directory "+ deviceDir +" before re-load."); 098 if (songDirFile != null && 099 songDirFile.isDirectory() && 100 songDirFile.canRead() && 101 deviceDirFile != null && 102 deviceDirFile.isDirectory() && 103 deviceDirFile.canWrite()) 104 { 105 if (doClear) 106 { 107 // clear the device dir 108 if (!testing) 109 { 110 System.out.println(" Clearing output directory "); 111 Util.removeDir(deviceDir); 112 // recreate the empty dir 113 Util.ensureFolderExists(deviceDirFile); 114 } 115 } 116 117 // Load the songs into the vector 118 System.out.println("Adding "+fileType+ " Songs From "+songDir); 119 songs.addAll((Vector <File>) getDirFiles(songDirFile, fileType, true)); 120 System.out.println(" Found " + songs.size() + " songs."); 121 122 if (true) 123 { 124 String toDirPath = deviceDirFile.getAbsolutePath(); 125 // Transfer the song files 126 //for (int i=0;i< songs.size();i++) 127 int sampleSize = songs.size(); 128 String fromSong = ""; 129 String toSong = ""; 130 String toSubDir = ""; 131 int randNum = 0; 132 int fileCount = 0; 133 boolean dirIsFull = false; 134 135 // set this to some max if you want 136 // else the while loop will copy 'til the device is FULL and throws an exception 137 if (maxOut) maxFiles = songs.size(); 138 while (!dirIsFull && fileCount++ < maxFiles) 139 { 140 randNum = rand.nextInt(sampleSize); 141 fromSong = ((File)songs.get(randNum)).getAbsolutePath(); 142 int c = fromSong.lastIndexOf(File.separator); 143 int b = fromSong.substring(0,c).lastIndexOf(File.separator); 144 int a = fromSong.substring(0,b).lastIndexOf(File.separator); 145 toSubDir = fromSong.substring(a,c); 146 //System.out.println(" toSubDir="+toSubDir); 147 148 // this kludges together the artist/album dir to copy to 149 toSong = toDirPath; 150 if (createSubDirs) toSong += toSubDir; 151 Util.ensureFolderExists(toSong); 152 153 toSong += File.separator+((File)songs.get(randNum)).getName(); 154 try 155 { 156 // check that it doesn't already exist 157 if (!(new File(toSong)).exists()) 158 { 159 System.out.println(fileCount +") Transfering "+randNum+" "+fromSong+" to "+ toSong); 160 if (!testing) Util.copyFile(fromSong, toSong); 161 } 162 } 163 catch(IOException ioEx) 164 { 165 System.out.println("Sorry, Could Not transfer "+fromSong+" file."); 166 System.out.println(ioEx.getMessage()); 167 if( ioEx.getMessage().toLowerCase().indexOf("no space")!=-1 ) 168 dirIsFull = true; 169 } 170 } 171 System.out.println("\nTransfered "+fileCount +" files."); 172 } 173 } 174 else 175 { 176 System.out.println(" Directory Error."); 177 if (args.length >1) System.out.println(" songDir=" + args[1]); 178 if (args.length >2) System.out.println(" deviceDir=" + args[2]); 179 } 180 } 181 else 182 { 183 System.out.println("SYNTAX: java ca.bc.webarts.tools.RandomFileSelector [-clear | -testing] [-fill] [-type fileExtension] musicDirPath transferToDirPath"); 184 System.out.println(" You *must* supply the directories on the commandline, other options are optional.\n"); 185 System.out.println(" Optional argument can be specified\n '-testing' to allow no actual writes"); 186 System.out.println(" '-clear' it will contents of the device dir first"); 187 System.out.println(" '-fill' will run until the device is full ELSE it loads 250 songs at a time"); 188 System.out.println(" '-type' takkes the next argument as the file extension to copy (defaults to 'ogg')"); 189 System.out.println("\n EXAMPLE: java ca.bc.webarts.tools.RandomFileSelector -clear /var/media/snd/ogg /mnt/usb-storage/music"); 190 } 191 } 192 193 194 private static Vector<File> getDirOggs(File dirToParse, boolean recureSubdirs ) 195 { 196 return getDirFiles( dirToParse, "ogg", recureSubdirs ); 197 } 198 199 200 private static Vector<File> getDirFiles(File dirToParse, String fileType, boolean recurseSubdirs ) 201 { 202 Vector<File> songs = (Vector <File>) new Vector(); 203 File subDirFile = null; 204 205 String [] subFilenames = dirToParse.list(); 206 if (dirToParse != null) 207 { 208 for (int i=0;i< subFilenames.length;i++) 209 { 210 subDirFile = new File(dirToParse.getAbsolutePath()+File.separator+subFilenames[i]); 211 if (subDirFile != null && subDirFile.isDirectory()) 212 { 213 // recurse 214 //System.out.println(" Recursing... "+dirToParse.getAbsolutePath()+File.separator+subFilenames[i]); 215 songs.addAll((Vector <File>) getDirFiles(subDirFile, fileType, recurseSubdirs)); 216 } 217 else 218 { 219 //System.out.println(" "+dirToParse.getAbsolutePath()+File.separator+subFilenames[i]); 220 // add files to retVal songs 221 if (subDirFile.getName().endsWith(fileType)) 222 songs.add((File) subDirFile); 223 } 224 } 225 } 226 return songs; 227 } 228}