summaryrefslogtreecommitdiff
path: root/src/main/java/cc/andreasbaumann/grabbers/nzz/Main.java
blob: 6c637edabc8833cbba89a23b75b94529d3c6c516 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package cc.andreasbaumann.grabbers.nzz;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.charset.StandardCharsets;

import java.io.File;
import java.io.BufferedReader;
import java.io.IOException;

import com.fasterxml.jackson.core.JsonParser.Feature;
import com.fasterxml.jackson.databind.ObjectMapper;

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;
import picocli.CommandLine.Help.Ansi;
import java.util.concurrent.Callable;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.microsoft.playwright.Playwright;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserContext;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Download;
import com.microsoft.playwright.options.AriaRole;

import java.util.Locale;
import java.util.Arrays;
import java.util.Date;
import java.text.SimpleDateFormat;

public class Main 
{
	private static final Logger LOGGER = LogManager.getFormatterLogger( Main.class );
	private static final String VERSION = "1.0";
	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";

	@Command( name = "nzzgatherer", mixinStandardHelpOptions = true, version = VERSION,
		description = "Grabs NZZ ePaper PDFs and stores them locally.")
	static class NZZGatherer implements Callable<Integer>
	{
		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 static void initializePlaywright( boolean notHeadless )
		{
			LOGGER.info( "Starting playwright..." );
			playwright = Playwright.create( );
			browserType = playwright.chromium( );
			browser = browserType.launch( new BrowserType.LaunchOptions( )
				.setHeadless( !notHeadless )
				.setArgs( Arrays.asList( "--disable-gpu" ) ) 			
			);
			context = browser.newContext( new Browser.NewContextOptions( )
				.setUserAgent( USER_AGENT )
				.setLocale( locale.toString( ) )
			);
		}

		private static void logout( ) throws Exception
		{
			LOGGER.info( ">>> Logging out.." );
			page.getByText( configuration.credentials.user ).click( );
			page.getByText( "Abmelden" ).click( );
		}

		private static void downloadCurrent( ) throws Exception
		{
			LOGGER.info( ">>> Downloading current PDF..." );
			Download download = page.waitForDownload( ( ) -> {
				page.locator( "div:nth-child(2) > span" ).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" );
			LOGGER.info( ">>> Saving to '" + file + "'.." );
			download.saveAs( file.toPath( ) );
		}

		private static void initialize( ) 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!" ) );
		}
		
		private static void login( ) throws Exception
		{

			LOGGER.info( ">>> Opening NZZ ePaper.." );
			page.navigate( "https://epaper.nzz.ch/" );
			page.waitForSelector( ":text('Anmelden')" );
			page.waitForLoadState( );

			LOGGER.info( ">>> Navigate to login page.." );
			page.getByText( "Anmelden" ).click( );
			page.waitForSelector( ":text('E-Mail-Adresse')" );
			page.waitForLoadState( );

			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.." );
			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 static void initializeFromFile( Path configurationFile )
		{
			ObjectMapper objectMapper = new ObjectMapper( );
			objectMapper.enable( Feature.ALLOW_UNQUOTED_FIELD_NAMES );
			objectMapper.enable( Feature.ALLOW_COMMENTS );

			try( BufferedReader configurationReader = Files.newBufferedReader( configurationFile, StandardCharsets.UTF_8 ) ) {
				LOGGER.info( "Reading configuration from '" + configurationFile + "'.." );
				configuration = objectMapper.readValue( configurationReader, Configuration.class );
			} catch ( IOException e ) {
				LOGGER.error( "Failed to read the configuration file '" + configurationFile + "':\n", e );
				System.exit( 1 );
			}
		}
		
		@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 = { "--not-headless" }, description = "show browser" )
		private boolean notHeadless = false;
		
		@Override
		public Integer call( ) throws Exception
		{
			initializeFromFile( new File( configFile ).toPath( ) );
			initializePlaywright( notHeadless );
			initialize( );
			login( );
			if( downloadCurrent ) {
				downloadCurrent( );
			}
			logout( );
			
			return 0;
		}
	}
	
	public static void main( String... args )
	{
		try {
			int exitCode = new CommandLine( new NZZGatherer( ) ).execute( args );
			System.exit( exitCode );
		} catch( Exception e ) {
			LOGGER.error( e );
			System.exit( 1 );
		}
	}
}