package com.skeptomai;

import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.javanet.NetHttpTransport;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.concurrent.Callable;
import java.util.logging.Logger;

public class MP3Fetcher implements Callable<String> {
    private static final String outputDir = System.getProperty("user.dir");
    private static final Logger log = Logger.getLogger(MP3Fetcher.class.getName());

    private final NetHttpTransport netHttpTransport;

    private String urlToFetch;

    public MP3Fetcher(String url) {
        netHttpTransport = new NetHttpTransport();
        urlToFetch = url;
    }

    public String call() throws IOException, IllegalArgumentException {
        if (urlToFetch == null) {
            throw new IllegalArgumentException("Need a URL to fetch");
        }

        log.info("urlToFetch: " + urlToFetch);
        String[] filenameParts = urlToFetch.split("/");
        String basename = filenameParts[filenameParts.length - 1];
        String absoluteFilePath = outputDir + File.separator + basename;
        File finalMP3 = new File(absoluteFilePath);
        Path pathFinalMP3 = finalMP3.toPath();

        if (finalMP3.exists()) {
            log.info("Looks like " + basename + " is already there.. Skipping");
        } else {
            GenericUrl url = new GenericUrl(urlToFetch);
            HttpRequestFactory requestFactory =
                    netHttpTransport.createRequestFactory();

            HttpRequest request = requestFactory.buildGetRequest(url);
            InputStream is = request.execute().getContent();
            File f = File.createTempFile("mp3fetcher", ".mp3");
            log.info(f.getAbsolutePath());
            //f.deleteOnExit();

            FileOutputStream outputStream = new FileOutputStream(f);
            byte[] buffer = new byte[4096];
            int len;
            while ((len = is.read(buffer)) > 0) {
                outputStream.write(buffer, 0, len);
            }
            outputStream.close();
            Path pathTempMP3 = f.toPath();


            log.info("Moving " + pathTempMP3.toString() + " to " + pathFinalMP3.toString());
            Files.move(pathTempMP3, pathFinalMP3);
        }
        return pathFinalMP3.toString();
    }


}
@@ -223,16 +223,63 @@
 ansport;
+%0Aimport com.google.api.client.util.ByteStreams;
 %0A%0Aimport
@@ -736,16 +736,23 @@
 private 
+static 
 final Ne
@@ -782,16 +782,155 @@
 ransport
+ = new NetHttpTransport();%0A    private static final HttpRequestFactory requestFactory =%0A            netHttpTransport.createRequestFactory()
 ;%0A%0A    p
@@ -996,59 +996,8 @@
 ) %7B%0A
-        netHttpTransport = new NetHttpTransport();%0A
     
@@ -1747,117 +1747,8 @@
 ch);
-%0A            HttpRequestFactory requestFactory =%0A                    netHttpTransport.createRequestFactory();
 %0A%0A  
@@ -1820,68 +1820,8 @@
 l);%0A
-            InputStream is = request.execute().getContent();
 %0A   
@@ -1920,24 +1920,25 @@
 utePath());%0A
+%0A
             
@@ -1941,29 +1941,65 @@
     
-//f.deleteOnExi
+try(InputStream is = request.execute().getConten
 t();%0A
-%0A
+    
     
@@ -2065,202 +2065,84 @@
 m(f)
-;%0A            byte%5B%5D buffer = new byte%5B4096%5D;%0A            int len;%0A            while ((len = is.read(buffer)) %3E 0) %7B%0A                outputStream.write(buffer, 0, len);%0A            %7D%0A           
+) %7B%0A                //f.deleteOnExit();%0A                ByteStreams.copy(is,
  out
@@ -2154,18 +2154,16 @@
 ream
-.close();%0A
+);%0A%0A    
     
@@ -2201,18 +2201,20 @@
 Path();%0A
-%0A%0A
+    
         
@@ -2306,24 +2306,28 @@
             
+    
 Files.move(p
@@ -2361,17 +2361,34 @@
         
+ 
-%7D
+   %7D%0A%0A%0A        %7D%0A
 %0A       

make transport and request static use try-with-resources