package feedmeyoutubecrawler;

/* Copyright (C) 2015 Robert Lehmann <lehmrob@gmail.com>
 *
 * This software may be modified and distributed under the terms
 * of the MIT license.  See the LICENSE file for details.
 */

import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.auth.oauth2.StoredCredential;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.util.store.DataStore;
import com.google.api.client.util.store.FileDataStoreFactory;
import org.aeonbits.owner.ConfigFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Performing the authentification on youtube
 *
 * @author Robert Lehmann
 *
 * @since 1.0
 */
public class Auth
{
    private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
    private static final JsonFactory JSON_FACTORY = new JacksonFactory();
    private static final String CREDENTIALS_DIRECTORY = ".oauth-credentials";
    private static final CrawlerConfig config = ConfigFactory.create(CrawlerConfig.class);
    private static final Pattern HOME_DIR = Pattern.compile("~");
    private static final Logger LOG = LoggerFactory.getLogger(Auth.class);

    /**
     * Perform the authorisation for the youtube account
     * @param scopes {@linkplain List} of scopes to perform authorization
     * @param credentailDataStore name of the credential datastore
     *
     * @return {@linkplain Credential} object which is used for Requests
     * @throws IOException an error occurs during the authorisation.
     *
     * @since 1.0
     */
    public static Credential authorize (List<String> scopes, String credentailDataStore)
            throws IOException {
        final Reader reader = new InputStreamReader(Auth.class.getResourceAsStream("/youtube.json"));
        final GoogleClientSecrets secrets = GoogleClientSecrets.load(JSON_FACTORY, reader);
        final FileDataStoreFactory dataStoreFactory = new FileDataStoreFactory(Paths.get(System
                .getProperty("user.home") + "/" + CREDENTIALS_DIRECTORY).toFile());
        final DataStore<StoredCredential> dataStore = dataStoreFactory.getDataStore(credentailDataStore);
        final GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY,
                secrets, scopes).setCredentialDataStore(dataStore).build();
        final LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8080).build();

        return new AuthorizationCodeInstalledApp(flow, receiver).authorize(config.userId());
    }


    /**
     * Gets the instance of {@linkplain HttpTransport}
     * @return instance of {@linkplain HttpTransport}
     *
     * @since 1.0
     */
    public static HttpTransport getHttpTransport() {
        return HTTP_TRANSPORT;
    }

    /**
     * Gets the instance of {@linkplain JsonFactory}
     * @return instance of {@linkplain JsonFactory}
     *
     * @since 1.0
     */
    public static JsonFactory getJsonFactory() {
        return JSON_FACTORY;
    }
}

@@ -211,17 +211,16 @@
 ls.%0A */%0A
-%0A
 import c
@@ -965,32 +965,68 @@
 taStoreFactory;%0A
+import feedmeyoutubecore.AppConfig;%0A
 import org.aeonb
@@ -1151,164 +1151,36 @@
 ile.
-Files;%0Aimport java.nio.file.Path;%0Aimport java.nio.file.Paths;%0Aimport java.nio.file.StandardOpenOption;%0Aimport java.util.List;%0Aimport java.util.regex.Matcher
+Paths;%0Aimport java.util.List
 ;%0Aim
@@ -1331,10 +1331,11 @@
 Auth
+ %7B
 %0A
-%7B
 %0A   
@@ -1588,23 +1588,19 @@
 c final 
-Crawler
+App
 Config c
@@ -1625,22 +1625,31 @@
 ory.
-create(Crawler
+%0A            create(App
 Conf
@@ -1866,16 +1866,23 @@
 account%0A
+     *%0A
      * @
@@ -1894,16 +1894,29 @@
  scopes 
+             
 %7B@linkpl
@@ -1945,16 +1945,50 @@
  perform
+%0A     *                           
  authori
@@ -2141,16 +2141,23 @@
 equests%0A
+     *%0A
      * @
@@ -2288,17 +2288,16 @@
 uthorize
- 
 (List%3CSt
@@ -2309,16 +2309,28 @@
  scopes,
+%0A           
  String 
@@ -2446,16 +2446,33 @@
 h.class.
+%0A                
 getResou
@@ -2571,16 +2571,33 @@
 ts.load(
+%0A                
 JSON_FAC
@@ -2690,16 +2690,33 @@
 Factory(
+%0A                
 Paths.ge
@@ -2724,16 +2724,24 @@
 (System%0A
+        
         
@@ -2805,16 +2805,33 @@
 ECTORY).
+%0A                
 toFile()
@@ -2904,16 +2904,33 @@
 Factory.
+%0A                
 getDataS
@@ -3002,16 +3002,32 @@
 low flow
+%0A               
  = new G
@@ -3076,16 +3076,40 @@
 ANSPORT,
+%0A                       
  JSON_FA
@@ -3115,16 +3115,24 @@
 ACTORY,%0A
+        
         
@@ -3190,16 +3190,33 @@
 aStore).
+%0A                
 build();
@@ -3295,16 +3295,33 @@
 ilder().
+%0A                
 setPort(
@@ -3411,16 +3411,33 @@
 thorize(
+%0A                
 config.u
@@ -3453,17 +3453,16 @@
 %0A    %7D%0A%0A
-%0A
     /**%0A
@@ -3512,24 +3512,31 @@
 pTransport%7D%0A
+     *%0A
      * @retu
@@ -3758,24 +3758,31 @@
 sonFactory%7D%0A
+     *%0A
      * @retu

Change the config interface