• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 import java.io.IOException;
2 import java.util.Random;
3 import java.io.FileOutputStream;
4 
5 /*
6  * This class will generate a json file containing 200 songs by default.
7  * Defaults:
8  * 1.) No. of songs - 200
9  * 2.) Seed - 200
10  * 3.) File name - "hundres_songs.json"
11  *
12  * If you want to change any of the default values pass them as arguments in the following:
13  * arg #1 No. of songs (Integer)
14  * arg #2 Seed (Integer)
15  * arg #3 File name (String) e.g <some-name>.json
16  */
17 
18 public class GenerateSongsJson {
19 
20   private static final StringBuilder sb = new StringBuilder();
21 
main(String []args)22   public static void main(String []args) throws IOException {
23     int numOfSongs = 200;
24     int seed = 200;
25     String fileName = "hundres_songs.json";
26 
27     if (args.length != 0) {
28       try {
29         numOfSongs = Integer.parseInt(args[0]);
30         seed = Integer.parseInt(args[1]);
31         fileName = args[2];
32       } catch (NumberFormatException e) {
33         System.out.println("Please enter the args as mentioned in the comment above.");
34         throw (e);
35       }
36     }
37 
38     Random randomNum = new Random(seed);
39     FileOutputStream outputStream = new FileOutputStream(fileName);
40 
41     sb.append("{ \n");
42     sb.append("  \"FLAGS\": \"browsable\", \n\n");
43     sb.append("  \"METADATA\": { \n");
44     sb.append("	\"MEDIA_ID\": \"hundreds_songs\", \n");
45     sb.append("    \"DISPLAY_TITLE\": \"More songs\" \n");
46     sb.append("  },\n\n");
47     sb.append("  \"CHILDREN\": [ \n");
48 
49     for (int i = 1; i <= numOfSongs; i++) {
50       int num1 = randomNum.nextInt(10000);
51       int num2 = num1/1000;
52       sb.append("    { \n  ");
53       sb.append("    \"FLAGS\": \"playable\",\n");
54       sb.append("      \"METADATA\": { \n");
55       sb.append("     	\"MEDIA_ID\": \"hundreds_songs normal " + num2 + "s song" + i +"\",\n");
56       sb.append(" 		\"DISPLAY_TITLE\": \"Normal " + num2 + "s song" + i + "\",\n");
57       sb.append(" 		\"DURATION\": " + num1 + "\n");
58       sb.append("      } \n");
59       sb.append("    },\n");
60     }
61     sb.deleteCharAt(sb.length() - 2);
62     sb.append("  ]\n");
63     sb.append("}");
64     byte[] strToBytes = sb.toString().getBytes();
65     outputStream.write(strToBytes);
66     outputStream.close();
67   }
68 }
69