summaryrefslogtreecommitdiff
path: root/src/main/java/cc/andreasbaumann/grabbers/nzz/Main.java
blob: 8a5038abc6be704524c0cd96c16b41b09de766a7 (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
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 Configuration configuration;
		private Playwright playwright;
		private BrowserType browserType;
		private Browser browser = null;
		private BrowserContext context;
		private Locale locale = DEFAULT_LOCALE;
		private Page page;
		
		private 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 void logout( ) throws Exception
		{
			LOGGER.info( "Logging out.." );
			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.getByText( "PDF HERUNTERLADEN" ).first( ).click( );
			});
			File directory = new File( configuration.downloads.directory );
			Date today = new Date( );
			File file = getFilename( directory, magazinType, today );
			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( "Initializing page..." );
			page = context.newPage( );
			page.setDefaultTimeout( DEFAULT_TIMEOUT );
			
			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 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( );
		}
				
		private 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), default is '${DEFAULT-VALUE}'", defaultValue = DEFAULT_CONFGURATION )
		private String configFile = DEFAULT_CONFGURATION;

		@Option( names = { "--download-current" }, description = "download only today's PDF" )
		private boolean downloadCurrentGiven;

		@Option( names = { "--not-headless" }, description = "do show the browser" )
		private boolean notHeadless;
		
		@Option( names = { "--debug" }, description = "show lots of debug output" )
		private boolean debug;
		
		@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
		{
			initializeFromFile( new File( configFile ).toPath( ) );
			initializePlaywright( notHeadless );
			initializePage( );
			login( );
			chooseMagazinType( magazinType );
			if( downloadCurrentGiven ) {
				downloadCurrent( );
			} else {
				downloadDate( date );
			}
			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 );
		}
	}
}