From cbfcced2eb1823fe364acf7c2748d90abe6300ea Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Sat, 22 Oct 2022 19:35:17 +0200 Subject: trying to switch between magazin type, endless scrolling for NZZS (not working yet) --- .../java/cc/andreasbaumann/grabbers/nzz/Main.java | 49 +++++++++++++++++++--- 1 file changed, 43 insertions(+), 6 deletions(-) diff --git a/src/main/java/cc/andreasbaumann/grabbers/nzz/Main.java b/src/main/java/cc/andreasbaumann/grabbers/nzz/Main.java index 8f747ff..8a5038a 100644 --- a/src/main/java/cc/andreasbaumann/grabbers/nzz/Main.java +++ b/src/main/java/cc/andreasbaumann/grabbers/nzz/Main.java @@ -41,7 +41,7 @@ public class Main public static final String USER_AGENT = "NZZ-Grabber/" + VERSION; public static final Locale DEFAULT_LOCALE = new Locale( "de", "CH" ); public static final int DEFAULT_TIMEOUT = 60000; - public static final String DEFAULT_CONFGURATION = "config.json"; + public static final String DEFAULT_CONFGURATION = "./config.json"; @Command( name = "nzzgatherer", mixinStandardHelpOptions = true, version = VERSION, description = "Grabs NZZ ePaper PDFs and stores them locally.") @@ -76,17 +76,46 @@ public class Main page.getByText( configuration.credentials.user ).click( ); page.getByText( "Abmelden" ).click( ); } + + private File getFilename( File directory, MagazinType magazinType, Date date ) + { + String timeStamp = new SimpleDateFormat( "yyyyMMdd").format( date ); + String prefix = "unknown_"; + switch( magazinType ) { + case NZZ: + prefix = "NZZ_"; + break; + case NZZ_AM_SONNTAG: + prefix = "NZZS_"; + break; + } + File file = new File( directory, prefix + timeStamp + ".pdf" ); + return file; + } + + private void chooseMagazinType( MagazinType magazinType ) + { + LOGGER.info( "Choosing magazin type '" + magazinType + "'.." ); + switch( magazinType ) { + case NZZ: + page.getByText( "Neue Zürcher Zeitung" ).first( ).click( ); + break; + case NZZ_AM_SONNTAG: + page.getByText( "NZZ am Sonntag" ).first( ).click( ); + break; + } + page.waitForLoadState( ); + } private void downloadCurrent( ) throws Exception { LOGGER.info( "Downloading current PDF..." ); Download download = page.waitForDownload( ( ) -> { - page.locator( "div:nth-child(2) > span" ).first( ).click( ); + page.getByText( "PDF HERUNTERLADEN" ).first( ).click( ); }); File directory = new File( configuration.downloads.directory ); Date today = new Date( ); - String timeStamp = new SimpleDateFormat( "yyyyMMdd").format( today ); - File file = new File( directory, "NZZ_" + timeStamp + ".pdf" ); + File file = getFilename( directory, magazinType, today ); LOGGER.info( "Saving to '" + file + "'.." ); download.saveAs( file.toPath( ) ); } @@ -136,7 +165,7 @@ public class Main page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions( ).setName( "Anmelden" )).click( ); page.waitForLoadState( ); } - + private void initializeFromFile( Path configurationFile ) { ObjectMapper objectMapper = new ObjectMapper( ); @@ -152,7 +181,7 @@ public class Main } } - @Option( names = { "-c", "--config" }, description = "file (in JSON)", defaultValue = DEFAULT_CONFGURATION ) + @Option( names = { "-c", "--config" }, description = "file (in JSON), default is '${DEFAULT-VALUE}'", defaultValue = DEFAULT_CONFGURATION ) private String configFile = DEFAULT_CONFGURATION; @Option( names = { "--download-current" }, description = "download only today's PDF" ) @@ -167,6 +196,13 @@ public class Main @Option( names = { "-d", "--date" }, description = "download PDF of a specific date" ) private Date date; + enum MagazinType { + NZZ, + NZZ_AM_SONNTAG + }; + @Option( names = { "-t", "--type" }, description = "type of magazin, default is '${DEFAULT-VALUE}', valid magazines are ${COMPLETION-CANDIDATES}", defaultValue = "NZZ" ) + private MagazinType magazinType; + @Override public Integer call( ) throws Exception { @@ -174,6 +210,7 @@ public class Main initializePlaywright( notHeadless ); initializePage( ); login( ); + chooseMagazinType( magazinType ); if( downloadCurrentGiven ) { downloadCurrent( ); } else { -- cgit v1.2.3-54-g00ecf