summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Baumann <mail@andreasbaumann.cc>2022-10-20 20:37:12 +0200
committerAndreas Baumann <mail@andreasbaumann.cc>2022-10-20 20:37:12 +0200
commitefd569f6cfa10ffea6038277d4dfee495e251772 (patch)
tree87410f522d06b26b7c12654d3d093393df698292
parent77b12f31a5f1b70e1e092916fa98702836654545 (diff)
downloadnzzgatherer-efd569f6cfa10ffea6038277d4dfee495e251772.tar.gz
nzzgatherer-efd569f6cfa10ffea6038277d4dfee495e251772.tar.bz2
fixes and started to add per day downloads
-rw-r--r--src/main/java/cc/andreasbaumann/grabbers/nzz/Main.java51
1 files 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( );