1 /* 2 * File : MakeTorrent.java 3 * Created : 16 nov. 2003 4 * By : Olivier 5 * 6 * Azureus - a Java Bittorrent client 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License as published by 10 * the Free Software Foundation; either version 2 of the License. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details ( see the LICENSE file ). 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; if not, write to the Free Software 19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20 */ 21 22 package org.gudy.azureus2.ui.console; 23 24 import java.io.File; 25 import java.net.URL; 26 import java.util.ArrayList; 27 import java.util.HashMap; 28 import java.util.List; 29 import java.util.Map; 30 import java.util.StringTokenizer; 31 32 import org.gudy.azureus2.core3.util.TorrentUtils; 33 import org.gudy.azureus2.core3.torrent.TOTorrent; 34 import org.gudy.azureus2.core3.torrent.TOTorrentCreator; 35 import org.gudy.azureus2.core3.torrent.TOTorrentFactory; 36 import org.gudy.azureus2.core3.torrent.TOTorrentProgressListener; 37 38 /** 39 * @author Olivier 40 * 41 */ 42 public class MakeTorrent implements TOTorrentProgressListener { 43 44 private boolean verbose; 45 46 private static final String[] validKeys = {"comment","announce-list","target","force_piece_size_pow2","verbose"}; 47 reportCurrentTask(String task_description)48 public void reportCurrentTask(String task_description) { 49 if(verbose) { 50 System.out.println(task_description); 51 } 52 } 53 reportProgress(int percent_complete)54 public void reportProgress(int percent_complete) { 55 if(verbose) { 56 System.out.print("\r" + percent_complete + "% "); 57 } 58 } 59 MakeTorrent(String file,URL url,Map parameters)60 public MakeTorrent(String file,URL url,Map parameters) { 61 File fSrc = new File(file); 62 63 String torrentName = (String) parameters.get("target"); 64 if(torrentName == null) 65 torrentName = file + ".torrent"; 66 File fDst = new File(torrentName); 67 68 if(parameters.get("verbose") != null) 69 verbose = true; 70 71 TOTorrent torrent = null; 72 String pieceSizeStr = (String) parameters.get("force_piece_size_pow2"); 73 if(pieceSizeStr != null) { 74 try { 75 long pieceSize = 1l << Integer.parseInt(pieceSizeStr); 76 TOTorrentCreator creator = TOTorrentFactory.createFromFileOrDirWithFixedPieceLength(fSrc,url,pieceSize); 77 creator.addListener( this ); 78 torrent = creator.create(); 79 }catch(Exception e) { 80 e.printStackTrace(); 81 return; 82 } 83 } else { 84 try { 85 TOTorrentCreator creator = TOTorrentFactory.createFromFileOrDirWithComputedPieceLength(fSrc,url); 86 creator.addListener( this ); 87 torrent = creator.create(); 88 } catch(Exception e) { 89 e.printStackTrace(); 90 return; 91 } 92 } 93 94 String comment = (String) parameters.get("comment"); 95 if(comment != null) { 96 torrent.setComment(comment); 97 } 98 99 String announceList = (String) parameters.get("announce-list"); 100 if(announceList != null) { 101 StringTokenizer st = new StringTokenizer(announceList,"|"); 102 List list = new ArrayList(); 103 List urls = new ArrayList(); 104 while(st.hasMoreTokens()) { 105 String _url = st.nextToken(); 106 urls.add(_url); 107 } 108 list.add(urls); 109 110 TorrentUtils.listToAnnounceGroups(list, torrent); 111 } 112 113 try { 114 torrent.serialiseToBEncodedFile(fDst); 115 } catch(Exception e) { 116 e.printStackTrace(); 117 } 118 119 } 120 main(String args[])121 public static void main(String args[]) { 122 if(args.length < 2) { 123 usage(); 124 System.exit(0); 125 } 126 Map parameters = new HashMap(); 127 for(int i = 2 ; i < args.length ; i++) { 128 boolean ok = parseParameter(args[i],parameters); 129 if(!ok) System.exit(-1); 130 } 131 File f = new File(args[1]); 132 if(!f.exists()) { 133 System.out.println(args[1] + " is not a valid file / directory"); 134 System.exit(-1); 135 } 136 URL url = null; 137 try { 138 url = new URL(args[0]); 139 } catch(Exception e) { 140 System.out.println(args[0] + " is not a valid url"); 141 System.exit(-1); 142 } 143 new MakeTorrent(args[1],url,parameters); 144 } 145 usage()146 public static void usage() { 147 System.out.println("Usage :"); 148 System.out.println("MakeTorrent <trackerurl> <file|dir> [options]"); 149 System.out.println("Options :"); 150 System.out.println("--comment=<comment> Adds a comment to the torrent"); 151 System.out.println("--force_piece_size_pow2=<pow2> Specifies the piece size to use"); 152 System.out.println("--target=<target file> Specifies a target torrent file"); 153 System.out.println("--verbose Verbose"); 154 System.out.println("--announce-list=url1[|url2|...] Use a list of trackers"); 155 } 156 parseParameter(String parameter,Map parameters)157 public static boolean parseParameter(String parameter,Map parameters) { 158 if(parameter == null) 159 return false; 160 if(parameter.equalsIgnoreCase("--v") || parameter.equalsIgnoreCase("--verbose")) { 161 parameters.put("verbose",new Integer(1)); 162 } 163 if(parameter.startsWith("--")) { 164 try { 165 StringTokenizer st = new StringTokenizer(parameter.substring(2),"="); 166 String key = st.nextToken(); 167 String value = ""; 168 String sep = ""; 169 while(st.hasMoreTokens()) { 170 value += sep + st.nextElement(); 171 sep = "="; 172 } 173 boolean valid = false; 174 for(int i = 0 ; i < validKeys.length ;i++) { 175 if(validKeys[i].equalsIgnoreCase(key)) { 176 valid = true; 177 break; 178 } 179 } 180 if(!valid) { 181 System.out.println("Invalid parameter : " + key); 182 return false; 183 } 184 parameters.put(key,value); 185 return true; 186 } catch(Exception e) { 187 System.out.println("Cannot parse " + parameter); 188 return false; 189 } 190 } 191 System.out.println("Cannot parse " + parameter); 192 return false; 193 } 194 195 } 196