diff --git a/cardcreator/src/tutorial/chapter01/SimpleTable.java b/cardcreator/src/tutorial/chapter01/SimpleTable.java index 4086c8c..b569701 100644 --- a/cardcreator/src/tutorial/chapter01/SimpleTable.java +++ b/cardcreator/src/tutorial/chapter01/SimpleTable.java @@ -21,11 +21,11 @@ import com.itextpdf.layout.element.Paragraph; 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.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.model_objects.specification.Track; import com.wrapper.spotify.requests.authorization.client_credentials.ClientCredentialsRequest; public class SimpleTable { @@ -35,11 +35,15 @@ public class SimpleTable { "spotify:album:5nCA2mAr3rFzk1A6LZXDYm", "spotify:album:1Vc2BZntRGIdv3Ul8AM6cz", "spotify:album:0H3rE11E9DrD3j2gEvQJ5R", - "spotify:album:0YSWLbCg5SpxAb2VOON3mX"/*, + "spotify:album:0YSWLbCg5SpxAb2VOON3mX", + "spotify:track:7oolFzHipTMg2nL7shhdz2", "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"*/ + "hidrive/Singles/04 - The Funeral.mp3" }; + + public static final int cardsPerLine = 9; + public static final boolean retrieveImageFiles = true; public static final String DEST = "results/simple_table.pdf"; public static final PdfNumber INVERTEDPORTRAIT = new PdfNumber(180); @@ -54,25 +58,41 @@ public class SimpleTable { .setClientSecret(clientSecret).build(); private static final ClientCredentialsRequest clientCredentialsRequest = spotifyApi.clientCredentials().build(); - protected List fetchData(String[] links) throws SpotifyWebApiException, IOException { + protected List fetchSpotifyData(String[] links) throws SpotifyWebApiException, IOException { List cards = new LinkedList(); for (String link : links) { + System.out.println(link); + Card card = new Card(link); + card.qrcode = new BarcodeQRCode(link); cards.add(card); - if (link.startsWith("spotify:")) { - String spotifyId = link.substring(link.lastIndexOf(':')+1); - System.out.println("ID: " + spotifyId); + if (link.startsWith("spotify:album:")) { + String spotifyId = link.substring(14); 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)); } + else if (link.startsWith("spotify:track:") ) { + String spotifyId = link.substring(14); + Track track = spotifyApi.getTrack(spotifyId).build().execute(); + card.title = track.getName(); + card.artist = track.getArtists()[0].getName(); + URL url = new URL(track.getAlbum().getImages()[0].getUrl()); + card.cover = new Image(ImageDataFactory.create(url)); + } + else if (link.startsWith("http://")) { + card.title = link; + card.artist = ""; + } + else { + card.title = link; + card.artist = ""; + } } return cards; @@ -91,7 +111,7 @@ public class SimpleTable { System.out.println("Spotify token expires in: " + clientCredentials.getExpiresIn() + " s"); - cards = creator.fetchData(albums); + cards = creator.fetchSpotifyData(albums); File file = new File(DEST); file.getParentFile().mkdirs(); @@ -119,6 +139,18 @@ public class SimpleTable { // for (int i = 0; i < 17; i++) { // table.addCell("hi"); +// } + + // TODO +// if (cardsPerLine < 9) { +// int i = 0; +// +// do { +// table.addCell(""); +// i++; +// table.addCell(""); +// i++; +// } while (cardsPerLine + i < 9); // } for (Card card : cards) { @@ -136,9 +168,14 @@ public class SimpleTable { 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); + if (card.cover != null) { + Image cover = card.cover; + cover.setWidth(30 / 25.4f * 72); + p.add(cover); + } + else { + p.setPaddingBottom(30 / 25.4f * 72); + } cell.add(p); p = new Paragraph(); @@ -154,28 +191,19 @@ public class SimpleTable { p.setFontSize(9f); p.setBold(); p.add(card.title); + + cell.setHeight(105 / 25.4f * 72); cell.add(p); cell.setBorder(new SolidBorder(ColorConstants.GRAY, 0.5f, 0.5f)); table.addCell(cell); } - BarcodeQRCode barcodeQRCode = new BarcodeQRCode("spotify:album:1X2HXE2lAqdmD8VjiBX8Tn"); - Cell cell = new Cell(); - cell.setVerticalAlignment(VerticalAlignment.MIDDLE); - cell.setHeight(297.637795276f); // mm / 25.4 = in | in * 72 = pt - - Paragraph p = new Paragraph(); - p.setTextAlignment(TextAlignment.CENTER); - Image qrcode = new Image(barcodeQRCode.createFormXObject(pdfDoc)); - qrcode.setWidth(10 / 25.4f * 72); - p.add(qrcode); - cell.add(p); - table.addCell(cell); - -// table.getCell(1, 1).setVerticalAlignment(VerticalAlignment.MIDDLE); - doc.add(table); + + // create backside +// doc.add(new AreaBreak(AreaBreakType.NEXT_PAGE)); + doc.close(); }