This commit is contained in:
2020-06-23 22:01:01 +02:00
parent ee78b8afc7
commit cb3c731c52
@@ -8,6 +8,8 @@ import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import org.apache.hc.core5.http.ParseException;
import com.itextpdf.barcodes.BarcodeQRCode;
import com.itextpdf.barcodes.qrcode.EncodeHintType;
import com.itextpdf.io.image.ImageDataFactory;
@@ -31,11 +33,16 @@ import com.wrapper.spotify.model_objects.specification.Album;
import com.wrapper.spotify.model_objects.specification.Playlist;
import com.wrapper.spotify.model_objects.specification.Track;
import com.wrapper.spotify.requests.authorization.client_credentials.ClientCredentialsRequest;
import com.wrapper.spotify.requests.data.playlists.GetPlaylistCoverImageRequest;
public class CardCreator {
public static final float mmUnit = 72f/25.4f; // see https://itextpdf.com/en/resources/faq/technical-support/itext-7/how-define-width-cell
public static final int cardsPerLine = 6;
public static final float cardWidth = 40;
public static final float pageWidth = 297;
public static final float borderWidth = 1;
public static final String DEST = "results/simple_table.pdf";
public static final PdfNumber INVERTEDPORTRAIT = new PdfNumber(180);
@@ -59,7 +66,9 @@ public class CardCreator {
final ClientCredentials clientCredentials = clientCredentialsRequest.execute();
// Set access token for further "spotifyApi" object usage
spotifyApi.setAccessToken(clientCredentials.getAccessToken());
String accessToken = clientCredentials.getAccessToken();
System.out.println(accessToken);
spotifyApi.setAccessToken(accessToken);
cards = fetchCardData(links);
@@ -80,7 +89,7 @@ public class CardCreator {
}
}
protected List<Card> fetchCardData(List<String> links) throws SpotifyWebApiException, IOException {
protected List<Card> fetchCardData(List<String> links) throws SpotifyWebApiException, IOException, ParseException {
List<Card> cards = new LinkedList<Card>();
HashMap<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
hints.put(EncodeHintType.CHARACTER_SET, "ISO-8859-15");
@@ -113,7 +122,7 @@ public class CardCreator {
Playlist playlist = spotifyApi.getPlaylist(spotifyId).build().execute();
card.title = playlist.getName();
card.artist = playlist.getDescription();
URL url = new URL(playlist.getImages()[0].getUrl());
URL url = new URL(spotifyApi.getPlaylistCoverImage(spotifyId).build().execute()[0].getUrl());
card.cover = new Image(ImageDataFactory.create(url));
}
else {
@@ -197,9 +206,10 @@ public class CardCreator {
protected void createFoldingPdf(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, 28.5f * mmUnit, 0, 28.5f * mmUnit);
float docMargin = (pageWidth - (cardWidth * cardsPerLine)) / 2 * mmUnit;
doc.setMargins(0, docMargin, 0, docMargin - borderWidth); // leave space for the first border
UnitValue[] colWidths = new UnitValue[cardsPerLine];
for (int i = 0; i < colWidths.length; i++)
@@ -207,15 +217,13 @@ public class CardCreator {
for (List<Card> line : lines) {
Cell oldCell = null;
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.setBorder(new SolidBorder(ColorConstants.GRAY, borderWidth, 0.5f));
cell.setPadding(0);
if (card != null) {
@@ -243,11 +251,11 @@ public class CardCreator {
p.setPaddingTop(5 * mmUnit);
if (card.cover != null) {
Image cover = card.cover;
cover.setWidth(28 * mmUnit);
cover.setWidth(30 * mmUnit);
p.add(cover);
}
else {
p.setPaddingBottom(28 * mmUnit);
p.setPaddingBottom(30 * mmUnit);
}
cell.add(p);
@@ -267,7 +275,7 @@ public class CardCreator {
cell.add(p);
}
oldCell = cell;
cell.setWidth((cardWidth * mmUnit) - borderWidth);
table.addCell(cell.setRotationAngle(Math.PI));
}
@@ -519,9 +527,34 @@ public class CardCreator {
"NAS/share/Drachenreiter - Das Hörspiel",
"NAS/share/Das große Abenteuer Musik",
"spotify:album:7tfjSYq5aWFwKlYWRzmPMc", // Das kleine Gespenst
"spotify:album:4kGTkO7CP063Bk0j14829K", // Der Räuber Hotzenplotz und die Mondrakete*/
"spotify:album:4tzPsEeFw4kR42NE4NqJvX", // Immer lustig in Bullerbü
"spotify:album:1zuWVjkOPIK5OHFnlgDQcf" // Die Häschenschule
"spotify:album:4kGTkO7CP063Bk0j14829K", // Der Räuber Hotzenplotz und die Mondrakete
*//* "spotify:album:4tzPsEeFw4kR42NE4NqJvX", // Immer lustig in Bullerbü
"spotify:album:1zuWVjkOPIK5OHFnlgDQcf", // Die Häschenschule
"spotify:album:71a7CDdtrILqGPtF5YfNGr", // Conni auf dem Bauernhof
// "spotify:album:2bOk55WwZUaXEz6PJpGiMa", // Conni tanzt
// "spotify:album:0VfB018ZTsA9Ai8OWAyUzS", // Conni zieht um
"spotify:album:4I5pPWLv63Fz955uoO7qYA", // Conni hat Geburtstag
// "spotify:album:48VfZGz16VLLpdayUedbOq", // Conni übernachtet bei Julia
//"spotify:playlist:6cdT1UHxAPOfQv4bVSnn00", // Peppa Wutz Playlist
//"spotify:playlist:3aZMwVy8suXz2FI0nI1XBM", // Paw Patrol Playlist
//temp
"https://upload.wikimedia.org/wikipedia/commons/thumb/4/4b/Radio_Teddy_Logo.svg/1200px-Radio_Teddy_Logo.svg.png|Radio Teddy|http://streamtdy.ir-media-tec.com/live/mp3-128/web/play.mp3",
"NAS/share/Astrid Lindgren - Wir Kinder aus Büllerbü",
"NAS/share/Benjamin Blümchen/Benjamin Blümchen - 122 - Benjamin Blümchen als Polizist",
"NAS/share/Das Grüffelokind - Hörspiel zum Film",
"NAS/share/Der Grüffelo und Das Grüffelokind",
*/ "NAS/share/Der kleine Drache Kokosnuss kommt in die Schule",
"NAS/share/Giraffenaffen/Giraffenaffen 5",
"NAS/share/Kikaninchen - Dibedibedab!",
/* "spotify:album:1FAupt6dQ7w849zXeJd5JB", // Reisemaus in Dänemark
"NAS/share/Karlchen-Geschichten",
"NAS/share/Sing mit mir Kinderlieder/Sing mit mir Kinderlieder",
*/ "NAS/share/In der Weihnachtsbäckerei - Das Musical-Hörspiel"
);
new CardCreator(links);