From a02582a01309e6e9ca35a5b241b6ef9f90340dd2 Mon Sep 17 00:00:00 2001 From: Tilman Date: Wed, 19 Dec 2018 10:38:58 +0100 Subject: [PATCH] v0.1 --- cardcreator/.classpath | 2 +- .../.settings/org.eclipse.jdt.core.prefs | 14 +- cardcreator/pom.xml | 7 +- .../src/tutorial/chapter01/SimpleTable.java | 126 +++++++++++++++--- 4 files changed, 126 insertions(+), 23 deletions(-) diff --git a/cardcreator/.classpath b/cardcreator/.classpath index 9c5bd87..56ee705 100644 --- a/cardcreator/.classpath +++ b/cardcreator/.classpath @@ -1,7 +1,7 @@ - + diff --git a/cardcreator/.settings/org.eclipse.jdt.core.prefs b/cardcreator/.settings/org.eclipse.jdt.core.prefs index abec6ca..6b5aebc 100644 --- a/cardcreator/.settings/org.eclipse.jdt.core.prefs +++ b/cardcreator/.settings/org.eclipse.jdt.core.prefs @@ -1,5 +1,13 @@ eclipse.preferences.version=1 -org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 -org.eclipse.jdt.core.compiler.compliance=1.5 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning -org.eclipse.jdt.core.compiler.source=1.5 +org.eclipse.jdt.core.compiler.source=1.7 diff --git a/cardcreator/pom.xml b/cardcreator/pom.xml index 9ce0719..29a4772 100644 --- a/cardcreator/pom.xml +++ b/cardcreator/pom.xml @@ -5,7 +5,7 @@ de.tilman cardcreator 0.0.1-SNAPSHOT - + org.slf4j @@ -18,5 +18,10 @@ 7.1.4 pom + + se.michaelthelin.spotify + spotify-web-api-java + 2.1.1 + \ No newline at end of file diff --git a/cardcreator/src/tutorial/chapter01/SimpleTable.java b/cardcreator/src/tutorial/chapter01/SimpleTable.java index c2b1152..4086c8c 100644 --- a/cardcreator/src/tutorial/chapter01/SimpleTable.java +++ b/cardcreator/src/tutorial/chapter01/SimpleTable.java @@ -2,13 +2,19 @@ package tutorial.chapter01; import java.io.File; import java.io.IOException; +import java.net.URL; +import java.util.LinkedList; +import java.util.List; import com.itextpdf.barcodes.BarcodeQRCode; +import com.itextpdf.io.image.ImageDataFactory; +import com.itextpdf.kernel.colors.ColorConstants; import com.itextpdf.kernel.geom.PageSize; import com.itextpdf.kernel.pdf.PdfDocument; import com.itextpdf.kernel.pdf.PdfNumber; import com.itextpdf.kernel.pdf.PdfWriter; import com.itextpdf.layout.Document; +import com.itextpdf.layout.borders.SolidBorder; import com.itextpdf.layout.element.Cell; import com.itextpdf.layout.element.Image; import com.itextpdf.layout.element.Paragraph; @@ -16,15 +22,25 @@ import com.itextpdf.layout.element.Table; import com.itextpdf.layout.property.TextAlignment; import com.itextpdf.layout.property.UnitValue; import com.itextpdf.layout.property.VerticalAlignment; -import com.neovisionaries.i18n.CountryCode; import com.wrapper.spotify.SpotifyApi; import com.wrapper.spotify.exceptions.SpotifyWebApiException; import com.wrapper.spotify.model_objects.credentials.ClientCredentials; import com.wrapper.spotify.model_objects.specification.Album; import com.wrapper.spotify.requests.authorization.client_credentials.ClientCredentialsRequest; -import com.wrapper.spotify.requests.data.albums.GetAlbumRequest; public class SimpleTable { + + public static final String[] albums = new String[] { + "spotify:album:1X2HXE2lAqdmD8VjiBX8Tn", + "spotify:album:5nCA2mAr3rFzk1A6LZXDYm", + "spotify:album:1Vc2BZntRGIdv3Ul8AM6cz", + "spotify:album:0H3rE11E9DrD3j2gEvQJ5R", + "spotify:album:0YSWLbCg5SpxAb2VOON3mX"/*, + "http://streamtdy.ir-media-tec.com/live/mp3-128/web/play.mp3", + "hidrive/Kinder/Astrid Lindgren - Lotta zieht um", + "hidrive/Singles/04 - The Funeral.mp3"*/ + }; + public static final String DEST = "results/simple_table.pdf"; public static final PdfNumber INVERTEDPORTRAIT = new PdfNumber(180); public static final PdfNumber LANDSCAPE = new PdfNumber(90); @@ -38,31 +54,56 @@ public class SimpleTable { .setClientSecret(clientSecret).build(); private static final ClientCredentialsRequest clientCredentialsRequest = spotifyApi.clientCredentials().build(); -// private static final GetAlbumRequest getAlbumRequest = spotifyApi.getAlbum("3T4tUhGYeRNVUGevb0wThu").market(CountryCode.SE).build(); + protected List fetchData(String[] links) throws SpotifyWebApiException, IOException { + List cards = new LinkedList(); + + for (String link : links) { + Card card = new Card(link); + cards.add(card); + + if (link.startsWith("spotify:")) { + String spotifyId = link.substring(link.lastIndexOf(':')+1); + System.out.println("ID: " + spotifyId); + Album album = spotifyApi.getAlbum(spotifyId).build().execute(); + card.title = album.getName(); + card.artist = album.getArtists()[0].getName(); + card.qrcode = new BarcodeQRCode(link); + + URL url = new URL(album.getImages()[0].getUrl()); + // BufferedImage img = ImageIO.read(url); + card.cover = new Image(ImageDataFactory.create(url)); + } + } + + return cards; + } public static void main(String[] args) throws Exception { + + SimpleTable creator = new SimpleTable(); + List cards; + try { final ClientCredentials clientCredentials = clientCredentialsRequest.execute(); // Set access token for further "spotifyApi" object usage spotifyApi.setAccessToken(clientCredentials.getAccessToken()); - System.out.println("Expires in: " + clientCredentials.getExpiresIn() + " s"); + System.out.println("Spotify token expires in: " + clientCredentials.getExpiresIn() + " s"); + + cards = creator.fetchData(albums); + + File file = new File(DEST); + file.getParentFile().mkdirs(); -// final Album album = spotifyApi.getAlbum("3T4tUhGYeRNVUGevb0wThu").market(CountryCode.SE).build().execute(); - final Album album = spotifyApi.getAlbum("3T4tUhGYeRNVUGevb0wThu").build().execute(); - - System.out.println("Name: " + album.getName()); + creator.manipulatePdf(DEST, cards); + } catch (IOException | SpotifyWebApiException e) { System.out.println("Error: " + e.getMessage()); } - - File file = new File(DEST); - file.getParentFile().mkdirs(); - new SimpleTable().manipulatePdf(DEST); } - protected void manipulatePdf(String dest) throws Exception { + protected void manipulatePdf(String dest, List cards) throws Exception { PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest)); pdfDoc.setDefaultPageSize(PageSize.A4.rotate()); @@ -76,8 +117,47 @@ public class SimpleTable { Table table = new Table(colWidths); table.useAllAvailableWidth(); - for (int i = 0; i < 17; i++) { - table.addCell("hi"); +// for (int i = 0; i < 17; i++) { +// table.addCell("hi"); +// } + + for (Card card : cards) { + Cell cell = new Cell(); + cell.setPadding(0); + + Paragraph p = new Paragraph(); + p.setTextAlignment(TextAlignment.CENTER); + p.setPaddingTop(43.62f); + Image qrcode = new Image(card.qrcode.createFormXObject(pdfDoc)); + qrcode.setWidth(10 / 25.4f * 72); + p.add(qrcode); + cell.add(p); + + p = new Paragraph(); + p.setTextAlignment(TextAlignment.CENTER); + p.setPaddingTop(5 / 25.4f * 72); + Image cover = card.cover; + cover.setWidth(30 / 25.4f * 72); + p.add(cover); + cell.add(p); + + p = new Paragraph(); + p.setTextAlignment(TextAlignment.CENTER); + p.setPaddingTop(5 / 25.4f * 72); + p.setFontSize(8f); + p.add(card.artist); + cell.add(p); + + p = new Paragraph(); + p.setTextAlignment(TextAlignment.CENTER); + p.setPaddingTop(3 / 25.4f * 72); + p.setFontSize(9f); + p.setBold(); + p.add(card.title); + cell.add(p); + + cell.setBorder(new SolidBorder(ColorConstants.GRAY, 0.5f, 0.5f)); + table.addCell(cell); } BarcodeQRCode barcodeQRCode = new BarcodeQRCode("spotify:album:1X2HXE2lAqdmD8VjiBX8Tn"); @@ -93,12 +173,22 @@ public class SimpleTable { cell.add(p); table.addCell(cell); - // table.addCell(new Image(barcodeQRCode.createFormXObject(pdfDoc))); - - table.getCell(1, 1).setVerticalAlignment(VerticalAlignment.MIDDLE); +// table.getCell(1, 1).setVerticalAlignment(VerticalAlignment.MIDDLE); doc.add(table); doc.close(); } +} + +class Card { + String title; + String artist; + BarcodeQRCode qrcode; + Image cover; + String link; + + public Card(String link) { + this.link = link; + } } \ No newline at end of file