From f90a8cf182fb264a1256a495f83f54ab433e5eb9 Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Thu, 20 Oct 2022 09:50:04 +0200 Subject: small fixes around static methods and command line options --- README | 2 +- .../java/cc/andreasbaumann/grabbers/nzz/Main.java | 58 ++++++++++++---------- 2 files changed, 33 insertions(+), 27 deletions(-) diff --git a/README b/README index c9215ef..57c1b19 100644 --- a/README +++ b/README @@ -5,7 +5,7 @@ build: mvn -DskipTests clean install run: -./nzzgatherer -c config.json +./nzzgrabber -c config.json links ----- diff --git a/src/main/java/cc/andreasbaumann/grabbers/nzz/Main.java b/src/main/java/cc/andreasbaumann/grabbers/nzz/Main.java index 6c637ed..27b776a 100644 --- a/src/main/java/cc/andreasbaumann/grabbers/nzz/Main.java +++ b/src/main/java/cc/andreasbaumann/grabbers/nzz/Main.java @@ -47,21 +47,21 @@ public class Main description = "Grabs NZZ ePaper PDFs and stores them locally.") static class NZZGatherer implements Callable { - private static Configuration configuration; - private static Playwright playwright; - private static BrowserType browserType; - private static Browser browser = null; - private static BrowserContext context; - private static Locale locale = DEFAULT_LOCALE; - private static Page page; + private Configuration configuration; + private Playwright playwright; + private BrowserType browserType; + private Browser browser = null; + private BrowserContext context; + private Locale locale = DEFAULT_LOCALE; + private Page page; - private static void initializePlaywright( boolean notHeadless ) + private void initializePlaywright( boolean headless ) { LOGGER.info( "Starting playwright..." ); playwright = Playwright.create( ); browserType = playwright.chromium( ); browser = browserType.launch( new BrowserType.LaunchOptions( ) - .setHeadless( !notHeadless ) + .setHeadless( headless ) .setArgs( Arrays.asList( "--disable-gpu" ) ) ); context = browser.newContext( new Browser.NewContextOptions( ) @@ -70,14 +70,14 @@ public class Main ); } - private static void logout( ) throws Exception + private void logout( ) throws Exception { LOGGER.info( ">>> Logging out.." ); page.getByText( configuration.credentials.user ).click( ); page.getByText( "Abmelden" ).click( ); } - private static void downloadCurrent( ) throws Exception + private void downloadCurrent( ) throws Exception { LOGGER.info( ">>> Downloading current PDF..." ); Download download = page.waitForDownload( ( ) -> { @@ -91,20 +91,23 @@ public class Main download.saveAs( file.toPath( ) ); } - private static void initialize( ) throws Exception + private void initializePage( ) throws Exception { LOGGER.info( ">>> Opening NZZ ePaper..." ); page = context.newPage( ); page.setDefaultTimeout( DEFAULT_TIMEOUT ); - page.route( "**", route -> { - LOGGER.info( route.request( ).url( ) ); - route.resume( ); - } ); - page.onLoad( p -> LOGGER.info( "Page loaded!" ) ); - page.onDOMContentLoaded( p -> LOGGER.info( "Page DOM content loaded!" ) ); + + if( debug ) { + page.route( "**", route -> { + LOGGER.info( route.request( ).url( ) ); + route.resume( ); + } ); + page.onLoad( p -> LOGGER.info( "Page loaded!" ) ); + page.onDOMContentLoaded( p -> LOGGER.info( "Page DOM content loaded!" ) ); + } } - private static void login( ) throws Exception + private void login( ) throws Exception { LOGGER.info( ">>> Opening NZZ ePaper.." ); @@ -136,7 +139,7 @@ public class Main Thread.sleep( 20000 ); } - private static void initializeFromFile( Path configurationFile ) + private void initializeFromFile( Path configurationFile ) { ObjectMapper objectMapper = new ObjectMapper( ); objectMapper.enable( Feature.ALLOW_UNQUOTED_FIELD_NAMES ); @@ -154,18 +157,21 @@ public class Main @Option( names = { "-c", "--config" }, description = "file (in JSON)", defaultValue = DEFAULT_CONFGURATION ) private String configFile = DEFAULT_CONFGURATION; - @Option( names = { "--download-current" }, description = "download only todays PDF" ) - private boolean downloadCurrent = false; + @Option( names = { "--download-current" }, description = "download only today's PDF" ) + private boolean downloadCurrent = true; - @Option( names = { "--not-headless" }, description = "show browser" ) - private boolean notHeadless = false; + @Option( names = { "--headless" }, description = "do not show browser" ) + private boolean headless = true; + + @Option( names = { "--debug" }, description = "show lots of debug output" ) + private boolean debug = false; @Override public Integer call( ) throws Exception { initializeFromFile( new File( configFile ).toPath( ) ); - initializePlaywright( notHeadless ); - initialize( ); + initializePlaywright( headless ); + initializePage( ); login( ); if( downloadCurrent ) { downloadCurrent( ); -- cgit v1.2.3-54-g00ecf