Layout and Spotify done

This commit is contained in:
2018-12-20 15:39:35 +01:00
parent 02f561d9f0
commit 930eb2f651
2 changed files with 305 additions and 222 deletions
@@ -0,0 +1,305 @@
package tutorial.chapter01;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Arrays;
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;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.property.TextAlignment;
import com.itextpdf.layout.property.UnitValue;
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 CardCreator {
public static final int slotsPerLine = 9;
public static final int cardsPerLine = 7;
public static final boolean generateBacksides = true;
public static final boolean retrieveImageFiles = true;
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);
public static final PdfNumber PORTRAIT = new PdfNumber(0);
public static final PdfNumber SEASCAPE = new PdfNumber(270);
private static final String clientId = "d104434912874fbf9ff42e5a91db5179";
private static final String clientSecret = "6acdb874210942128ba4991c4f7228b5";
private static final SpotifyApi spotifyApi = new SpotifyApi.Builder().setClientId(clientId)
.setClientSecret(clientSecret).build();
private static final ClientCredentialsRequest clientCredentialsRequest = spotifyApi.clientCredentials().build();
public CardCreator(List<String> links) {
List<Card> cards;
try {
final ClientCredentials clientCredentials = clientCredentialsRequest.execute();
// Set access token for further "spotifyApi" object usage
spotifyApi.setAccessToken(clientCredentials.getAccessToken());
cards = fetchCardData(links);
File file = new File(DEST);
file.getParentFile().mkdirs();
createPdf(DEST, sortCards(cards));
} catch (IOException | SpotifyWebApiException e) {
System.out.println("Error: " + e.getMessage());
e.printStackTrace();
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
e.printStackTrace();
}
}
protected List<Card> fetchCardData(List<String> links) throws SpotifyWebApiException, IOException {
List<Card> cards = new LinkedList<Card>();
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:album:")) {
String spotifyId = link.substring(14);
Album album = spotifyApi.getAlbum(spotifyId).build().execute();
card.title = album.getName();
card.artist = album.getArtists()[0].getName();
URL url = new URL(album.getImages()[0].getUrl());
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;
}
private List<List<Card>> sortCards(List<Card> cards) {
List<List<Card>> lines = new LinkedList<List<Card>>();
List<Card> line = new LinkedList<Card>();
for (Card card : cards) {
if (line.size() < cardsPerLine) {
line.add(card);
}
else {
lines.add(line);
line = new LinkedList<Card>();
line.add(card);
}
}
if (line.size() > 0)
lines.add(line);
if (cardsPerLine < slotsPerLine) {
for (List<Card> l : lines) {
boolean addToStart = true;
while (l.size() < slotsPerLine) {
if (addToStart)
l.add(0, null);
else
l.add(null);
addToStart = !addToStart;
}
}
}
return lines;
}
protected void createPdf(String dest, List<List<Card>> lines) throws Exception {
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
pdfDoc.setDefaultPageSize(PageSize.A4.rotate());
Document doc = new Document(pdfDoc);
doc.setMargins(0, 0, 0, 0);
UnitValue[] colWidths = new UnitValue[slotsPerLine];
for (int i = 0; i < colWidths.length; i++)
colWidths[i] = new UnitValue(UnitValue.PERCENT, 100/slotsPerLine);
Table firstLine = null;
for (List<Card> line : lines) {
Table table = new Table(colWidths);
table.useAllAvailableWidth();
for (Card card : line) {
Cell cell = new Cell();
cell.setBorder(new SolidBorder(ColorConstants.GRAY, 0.5f, 0.5f));
cell.setPadding(0);
if (card != null) {
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);
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();
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);
}
table.addCell(cell);
}
table.setMinHeight(105 / 25.4f * 72);
table.setMaxHeight(105 / 25.4f * 72);
doc.add(table);
if (generateBacksides) {
if (firstLine == null && line != lines.get(lines.size() - 1)) {
firstLine = table;
}
else {
Table tbl1 = new Table(colWidths);
tbl1.useAllAvailableWidth();
Table tbl2 = new Table(colWidths);
tbl2.useAllAvailableWidth();
for (int i = slotsPerLine - 1; i > 0 ; i--) {
if (firstLine != null)
tbl1.addCell(firstLine.getCell(0, i));
tbl2.addCell(table.getCell(0, i));
}
tbl1.setMinHeight(105 / 25.4f * 72);
tbl1.setMaxHeight(105 / 25.4f * 72);
doc.add(tbl1);
tbl2.setMinHeight(105 / 25.4f * 72);
tbl2.setMaxHeight(105 / 25.4f * 72);
doc.add(tbl2);
firstLine = null;
}
}
}
doc.close();
}
public static void main(String[] args) throws Exception {
List<String> links = Arrays.asList(
"spotify:album:1X2HXE2lAqdmD8VjiBX8Tn",
"spotify:album:5nCA2mAr3rFzk1A6LZXDYm",
"spotify:album:1Vc2BZntRGIdv3Ul8AM6cz",
"spotify:album:0H3rE11E9DrD3j2gEvQJ5R",
"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",
"DUMMY01.mp3",
"DUMMY02.mp3",
"DUMMY03.mp3",
"DUMMY04.mp3",
"DUMMY05.mp3",
"DUMMY06.mp3",
"DUMMY07.mp3",
"DUMMY08.mp3",
"DUMMY09.mp3",
"DUMMY10.mp3",
"DUMMY11.mp3",
"DUMMY12.mp3",
"DUMMY13.mp3",
"DUMMY14.mp3",
"DUMMY15.mp3",
"DUMMY16.mp3",
"DUMMY17.mp3",
"DUMMY18.mp3",
"DUMMY19.mp3",
"DUMMY20.mp3"
);
new CardCreator(links);
}
}
class Card {
String title;
String artist;
BarcodeQRCode qrcode;
Image cover;
String link;
public Card(String link) {
this.link = link;
}
}
@@ -1,222 +0,0 @@
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;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.property.TextAlignment;
import com.itextpdf.layout.property.UnitValue;
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 {
public static final String[] albums = new String[] {
"spotify:album:1X2HXE2lAqdmD8VjiBX8Tn",
"spotify:album:5nCA2mAr3rFzk1A6LZXDYm",
"spotify:album:1Vc2BZntRGIdv3Ul8AM6cz",
"spotify:album:0H3rE11E9DrD3j2gEvQJ5R",
"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"
};
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);
public static final PdfNumber LANDSCAPE = new PdfNumber(90);
public static final PdfNumber PORTRAIT = new PdfNumber(0);
public static final PdfNumber SEASCAPE = new PdfNumber(270);
private static final String clientId = "d104434912874fbf9ff42e5a91db5179";
private static final String clientSecret = "6acdb874210942128ba4991c4f7228b5";
private static final SpotifyApi spotifyApi = new SpotifyApi.Builder().setClientId(clientId)
.setClientSecret(clientSecret).build();
private static final ClientCredentialsRequest clientCredentialsRequest = spotifyApi.clientCredentials().build();
protected List<Card> fetchSpotifyData(String[] links) throws SpotifyWebApiException, IOException {
List<Card> cards = new LinkedList<Card>();
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:album:")) {
String spotifyId = link.substring(14);
Album album = spotifyApi.getAlbum(spotifyId).build().execute();
card.title = album.getName();
card.artist = album.getArtists()[0].getName();
URL url = new URL(album.getImages()[0].getUrl());
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;
}
public static void main(String[] args) throws Exception {
SimpleTable creator = new SimpleTable();
List<Card> cards;
try {
final ClientCredentials clientCredentials = clientCredentialsRequest.execute();
// Set access token for further "spotifyApi" object usage
spotifyApi.setAccessToken(clientCredentials.getAccessToken());
System.out.println("Spotify token expires in: " + clientCredentials.getExpiresIn() + " s");
cards = creator.fetchSpotifyData(albums);
File file = new File(DEST);
file.getParentFile().mkdirs();
creator.manipulatePdf(DEST, cards);
} catch (IOException | SpotifyWebApiException e) {
System.out.println("Error: " + e.getMessage());
}
}
protected void manipulatePdf(String dest, List<Card> cards) throws Exception {
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
pdfDoc.setDefaultPageSize(PageSize.A4.rotate());
Document doc = new Document(pdfDoc);
doc.setMargins(0, 0, 0, 0);
UnitValue[] colWidths = new UnitValue[9];
for (int i = 0; i < colWidths.length; i++)
colWidths[i] = new UnitValue(UnitValue.PERCENT, 11.10f);
Table table = new Table(colWidths);
table.useAllAvailableWidth();
// 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) {
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);
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();
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.setHeight(105 / 25.4f * 72);
cell.add(p);
cell.setBorder(new SolidBorder(ColorConstants.GRAY, 0.5f, 0.5f));
table.addCell(cell);
}
doc.add(table);
// create backside
// doc.add(new AreaBreak(AreaBreakType.NEXT_PAGE));
doc.close();
}
}
class Card {
String title;
String artist;
BarcodeQRCode qrcode;
Image cover;
String link;
public Card(String link) {
this.link = link;
}
}