Debian Codegraph @

src/main/java/com/akdeniz/googleplaycrawler/DownloadData.java

raw
1 package com.akdeniz.googleplaycrawler;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.security.InvalidAlgorithmParameterException;
6 import java.security.InvalidKeyException;
7 import java.security.NoSuchAlgorithmException;
8 import java.security.NoSuchProviderException;
9
10 import javax.crypto.Cipher;
11 import javax.crypto.CipherInputStream;
12 import javax.crypto.NoSuchPaddingException;
13 import javax.crypto.spec.IvParameterSpec;
14 import javax.crypto.spec.SecretKeySpec;
15
16 import com.akdeniz.googleplaycrawler.GooglePlay.AndroidAppDeliveryData;
17 import com.akdeniz.googleplaycrawler.GooglePlay.HttpCookie;
18 import com.akdeniz.googleplaycrawler.misc.Base64;
19
20 public class DownloadData {
21
22 private AndroidAppDeliveryData appDeliveryData;
23 private String downloadUrl;
24 private HttpCookie downloadAuthCookie;
25 private GooglePlayAPI api;
26
27 public DownloadData(GooglePlayAPI api, AndroidAppDeliveryData appDeliveryData) {
28 this.appDeliveryData = appDeliveryData;
29 this.api = api;
30 this.downloadUrl = appDeliveryData.getDownloadUrl();
31 this.downloadAuthCookie = appDeliveryData.getDownloadAuthCookie(0);
32 }
33
34 /**
35 * Access the APK file
36 *
37 * @return an inputstream from which the app can be read (already processed
38 * through crypto).
39 * @throws NoSuchPaddingException
40 * @throws NoSuchProviderException
41 * @throws NoSuchAlgorithmException
42 * @throws InvalidAlgorithmParameterException
43 * @throws InvalidKeyException
44 */
45 public InputStream openApp() throws IOException, NoSuchAlgorithmException,
46 NoSuchProviderException, NoSuchPaddingException, InvalidKeyException,
47 InvalidAlgorithmParameterException {
48 InputStream ret = api.executeDownload(downloadUrl, downloadAuthCookie.getName() + "="
49 + downloadAuthCookie.getValue());
50 if (appDeliveryData.hasEncryptionParams()) {
51 int version = ret.read();
52 if (version != 0) {
53 throw new IOException("Unknown crypto container!");
54 }
55 ret.skip(4); // Meta data
56 byte[] iv = new byte[16];
57 ret.read(iv);
58 byte[] encoded = appDeliveryData.getEncryptionParams().getEncryptionKey().getBytes("UTF-8");
59 byte[] decoded = Base64.decode(encoded, Base64.DEFAULT);
60 Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding", "SunJCE");
61 SecretKeySpec key = new SecretKeySpec(decoded, "AES");
62 cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));
63 return new CipherInputStream(ret, cipher);
64 }
65 else {
66 return ret;
67 }
68 }
69
70 /**
71 * Access the first expansion
72 *
73 * @return a stream or null if there is no expansion.
74 */
75 public InputStream openMainExpansion() throws IOException {
76 if (appDeliveryData.getAdditionalFileCount() < 1) {
77 return null;
78 }
79 String url = appDeliveryData.getAdditionalFile(0).getDownloadUrl();
80 return api.executeDownload(url,
81 downloadAuthCookie.getName() + "=" + downloadAuthCookie.getValue());
82 }
83
84 /**
85 * Access the second expansion
86 *
87 * @return a stream or null if there is no expansion.
88 */
89 public InputStream openPatchExpansion() throws IOException {
90 if (appDeliveryData.getAdditionalFileCount() < 2) {
91 return null;
92 }
93 String url = appDeliveryData.getAdditionalFile(1).getDownloadUrl();
94 return api.executeDownload(url,
95 downloadAuthCookie.getName() + "=" + downloadAuthCookie.getValue());
96 }
97
98 public int getPatchFileVersion() {
99 if (appDeliveryData.getAdditionalFileCount()>1) {
100 return appDeliveryData.getAdditionalFile(1).getVersionCode();
101 }
102 return -1;
103 }
104
105 public int getMainFileVersion() {
106 if (appDeliveryData.getAdditionalFileCount()>0) {
107 return appDeliveryData.getAdditionalFile(0).getVersionCode();
108 }
109 return -1;
110 }
111 }