From efd569f6cfa10ffea6038277d4dfee495e251772 Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Thu, 20 Oct 2022 20:37:12 +0200 Subject: fixes and started to add per day downloads --- .../java/cc/andreasbaumann/grabbers/nzz/Main.java | 51 ++++++++++++---------- 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/src/main/java/cc/andreasbaumann/grabbers/nzz/Main.java b/src/main/java/cc/andreasbaumann/grabbers/nzz/Main.java index 27b776a..8f747ff 100644 --- a/src/main/java/cc/andreasbaumann/grabbers/nzz/Main.java +++ b/src/main/java/cc/andreasbaumann/grabbers/nzz/Main.java @@ -55,13 +55,13 @@ public class Main private Locale locale = DEFAULT_LOCALE; private Page page; - private void initializePlaywright( boolean headless ) + private void initializePlaywright( boolean notHeadless ) { LOGGER.info( "Starting playwright..." ); playwright = Playwright.create( ); browserType = playwright.chromium( ); browser = browserType.launch( new BrowserType.LaunchOptions( ) - .setHeadless( headless ) + .setHeadless( !notHeadless ) .setArgs( Arrays.asList( "--disable-gpu" ) ) ); context = browser.newContext( new Browser.NewContextOptions( ) @@ -72,14 +72,14 @@ public class Main private void logout( ) throws Exception { - LOGGER.info( ">>> Logging out.." ); + LOGGER.info( "Logging out.." ); page.getByText( configuration.credentials.user ).click( ); page.getByText( "Abmelden" ).click( ); } private void downloadCurrent( ) throws Exception { - LOGGER.info( ">>> Downloading current PDF..." ); + LOGGER.info( "Downloading current PDF..." ); Download download = page.waitForDownload( ( ) -> { page.locator( "div:nth-child(2) > span" ).first( ).click( ); }); @@ -87,13 +87,19 @@ public class Main Date today = new Date( ); String timeStamp = new SimpleDateFormat( "yyyyMMdd").format( today ); File file = new File( directory, "NZZ_" + timeStamp + ".pdf" ); - LOGGER.info( ">>> Saving to '" + file + "'.." ); + LOGGER.info( "Saving to '" + file + "'.." ); download.saveAs( file.toPath( ) ); } + + private void downloadDate( Date date ) throws Exception + { + String dateStr = new SimpleDateFormat( "EEEE, dd.MM.yyyy").format( date ); + LOGGER.info( "Downloading PDF of '" + dateStr + "'" ); + } private void initializePage( ) throws Exception { - LOGGER.info( ">>> Opening NZZ ePaper..." ); + LOGGER.info( "Initializing page..." ); page = context.newPage( ); page.setDefaultTimeout( DEFAULT_TIMEOUT ); @@ -109,34 +115,26 @@ public class Main private void login( ) throws Exception { - - LOGGER.info( ">>> Opening NZZ ePaper.." ); + LOGGER.info( "Opening NZZ ePaper.." ); page.navigate( "https://epaper.nzz.ch/" ); page.waitForSelector( ":text('Anmelden')" ); page.waitForLoadState( ); - LOGGER.info( ">>> Navigate to login page.." ); + LOGGER.info( "Navigate to login page.." ); page.getByText( "Anmelden" ).click( ); page.waitForSelector( ":text('E-Mail-Adresse')" ); page.waitForLoadState( ); - LOGGER.info( ">>> Inserting email data.." ); + LOGGER.info( "Inserting email data.." ); page.getByPlaceholder( "E-Mail-Adresse" ).fill( configuration.credentials.login ); page.getByRole( AriaRole.BUTTON, new Page.GetByRoleOptions( ).setName( "Weiter" ) ).click( ); page.waitForLoadState( ); - LOGGER.info( ">>> Inserting password.." ); + LOGGER.info( "Inserting password.." ); page.getByRole( AriaRole.TEXTBOX, new Page.GetByRoleOptions( ).setName( "Passwort*" ) ).fill( configuration.credentials.password ); page.waitForLoadState( ); page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions( ).setName( "Anmelden" )).click( ); page.waitForLoadState( ); - - LOGGER.info( ">>> Downloading current PDF.." ); - Download download = page.waitForDownload(() -> { - page.locator("div:nth-child(2) > span").first( ).click( ); - } ); - - Thread.sleep( 20000 ); } private void initializeFromFile( Path configurationFile ) @@ -158,23 +156,28 @@ public class Main private String configFile = DEFAULT_CONFGURATION; @Option( names = { "--download-current" }, description = "download only today's PDF" ) - private boolean downloadCurrent = true; + private boolean downloadCurrentGiven; - @Option( names = { "--headless" }, description = "do not show browser" ) - private boolean headless = true; + @Option( names = { "--not-headless" }, description = "do show the browser" ) + private boolean notHeadless; @Option( names = { "--debug" }, description = "show lots of debug output" ) - private boolean debug = false; + private boolean debug; + + @Option( names = { "-d", "--date" }, description = "download PDF of a specific date" ) + private Date date; @Override public Integer call( ) throws Exception { initializeFromFile( new File( configFile ).toPath( ) ); - initializePlaywright( headless ); + initializePlaywright( notHeadless ); initializePage( ); login( ); - if( downloadCurrent ) { + if( downloadCurrentGiven ) { downloadCurrent( ); + } else { + downloadDate( date ); } logout( ); -- cgit v1.2.3-54-g00ecf