This commit is contained in:
2018-12-19 10:38:58 +01:00
parent b55d3a0a18
commit a02582a013
4 changed files with 126 additions and 23 deletions
+1 -1
View File
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<classpath> <classpath>
<classpathentry kind="src" path="src"/> <classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
<attributes> <attributes>
<attribute name="module" value="true"/> <attribute name="module" value="true"/>
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
@@ -1,5 +1,13 @@
eclipse.preferences.version=1 eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.compliance=1.5 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.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.5 org.eclipse.jdt.core.compiler.source=1.7
+5
View File
@@ -18,5 +18,10 @@
<version>7.1.4</version> <version>7.1.4</version>
<type>pom</type> <type>pom</type>
</dependency> </dependency>
<dependency>
<groupId>se.michaelthelin.spotify</groupId>
<artifactId>spotify-web-api-java</artifactId>
<version>2.1.1</version>
</dependency>
</dependencies> </dependencies>
</project> </project>
@@ -2,13 +2,19 @@ package tutorial.chapter01;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.URL;
import java.util.LinkedList;
import java.util.List;
import com.itextpdf.barcodes.BarcodeQRCode; 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.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument; import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfNumber; import com.itextpdf.kernel.pdf.PdfNumber;
import com.itextpdf.kernel.pdf.PdfWriter; import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document; import com.itextpdf.layout.Document;
import com.itextpdf.layout.borders.SolidBorder;
import com.itextpdf.layout.element.Cell; import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Image; import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.element.Paragraph; 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.TextAlignment;
import com.itextpdf.layout.property.UnitValue; import com.itextpdf.layout.property.UnitValue;
import com.itextpdf.layout.property.VerticalAlignment; import com.itextpdf.layout.property.VerticalAlignment;
import com.neovisionaries.i18n.CountryCode;
import com.wrapper.spotify.SpotifyApi; import com.wrapper.spotify.SpotifyApi;
import com.wrapper.spotify.exceptions.SpotifyWebApiException; import com.wrapper.spotify.exceptions.SpotifyWebApiException;
import com.wrapper.spotify.model_objects.credentials.ClientCredentials; import com.wrapper.spotify.model_objects.credentials.ClientCredentials;
import com.wrapper.spotify.model_objects.specification.Album; import com.wrapper.spotify.model_objects.specification.Album;
import com.wrapper.spotify.requests.authorization.client_credentials.ClientCredentialsRequest; import com.wrapper.spotify.requests.authorization.client_credentials.ClientCredentialsRequest;
import com.wrapper.spotify.requests.data.albums.GetAlbumRequest;
public class SimpleTable { 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 String DEST = "results/simple_table.pdf";
public static final PdfNumber INVERTEDPORTRAIT = new PdfNumber(180); public static final PdfNumber INVERTEDPORTRAIT = new PdfNumber(180);
public static final PdfNumber LANDSCAPE = new PdfNumber(90); public static final PdfNumber LANDSCAPE = new PdfNumber(90);
@@ -38,31 +54,56 @@ public class SimpleTable {
.setClientSecret(clientSecret).build(); .setClientSecret(clientSecret).build();
private static final ClientCredentialsRequest clientCredentialsRequest = spotifyApi.clientCredentials().build(); private static final ClientCredentialsRequest clientCredentialsRequest = spotifyApi.clientCredentials().build();
// private static final GetAlbumRequest getAlbumRequest = spotifyApi.getAlbum("3T4tUhGYeRNVUGevb0wThu").market(CountryCode.SE).build(); protected List<Card> fetchData(String[] links) throws SpotifyWebApiException, IOException {
List<Card> cards = new LinkedList<Card>();
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 { public static void main(String[] args) throws Exception {
SimpleTable creator = new SimpleTable();
List<Card> cards;
try { try {
final ClientCredentials clientCredentials = clientCredentialsRequest.execute(); final ClientCredentials clientCredentials = clientCredentialsRequest.execute();
// Set access token for further "spotifyApi" object usage // Set access token for further "spotifyApi" object usage
spotifyApi.setAccessToken(clientCredentials.getAccessToken()); spotifyApi.setAccessToken(clientCredentials.getAccessToken());
System.out.println("Expires in: " + clientCredentials.getExpiresIn() + " s"); System.out.println("Spotify token expires in: " + clientCredentials.getExpiresIn() + " s");
// final Album album = spotifyApi.getAlbum("3T4tUhGYeRNVUGevb0wThu").market(CountryCode.SE).build().execute(); cards = creator.fetchData(albums);
final Album album = spotifyApi.getAlbum("3T4tUhGYeRNVUGevb0wThu").build().execute();
System.out.println("Name: " + album.getName());
} catch (IOException | SpotifyWebApiException e) {
System.out.println("Error: " + e.getMessage());
}
File file = new File(DEST); File file = new File(DEST);
file.getParentFile().mkdirs(); file.getParentFile().mkdirs();
new SimpleTable().manipulatePdf(DEST);
creator.manipulatePdf(DEST, cards);
} catch (IOException | SpotifyWebApiException e) {
System.out.println("Error: " + e.getMessage());
}
} }
protected void manipulatePdf(String dest) throws Exception { protected void manipulatePdf(String dest, List<Card> cards) throws Exception {
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest)); PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
pdfDoc.setDefaultPageSize(PageSize.A4.rotate()); pdfDoc.setDefaultPageSize(PageSize.A4.rotate());
@@ -76,8 +117,47 @@ public class SimpleTable {
Table table = new Table(colWidths); Table table = new Table(colWidths);
table.useAllAvailableWidth(); table.useAllAvailableWidth();
for (int i = 0; i < 17; i++) { // for (int i = 0; i < 17; i++) {
table.addCell("hi"); // 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"); BarcodeQRCode barcodeQRCode = new BarcodeQRCode("spotify:album:1X2HXE2lAqdmD8VjiBX8Tn");
@@ -93,12 +173,22 @@ public class SimpleTable {
cell.add(p); cell.add(p);
table.addCell(cell); 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.add(table);
doc.close(); doc.close();
} }
} }
class Card {
String title;
String artist;
BarcodeQRCode qrcode;
Image cover;
String link;
public Card(String link) {
this.link = link;
}
}