55import java .io .BufferedReader ;
66import java .io .IOException ;
77import java .io .InputStreamReader ;
8- import java .net .HttpURLConnection ;
9- import java .net .URL ;
10- import java .net .URLEncoder ;
8+ import java .net .*;
9+ import java .nio .charset .StandardCharsets ;
1110import java .util .List ;
1211
13- // TODO Javadoc
14-
12+ /**
13+ * class responsible for all things with the DeArrow api and data formatting
14+ */
1515public class DeArrow {
1616
1717 /**
1818 * get DeArrow information
1919 *
20- * @param videoID
20+ * @param videoID id of the individual video
2121 * @return response as String
2222 * @throws IOException
2323 */
2424 public String getInitialInformation (String videoID ) throws IOException {
2525 String apiUrl = "https://sponsor.ajay.app/api/branding" ;
2626
2727 // Encode the videoID parameter
28- String encodedVideoID = URLEncoder .encode (videoID , "UTF-8" );
28+ String encodedVideoID = URLEncoder .encode (videoID , StandardCharsets . UTF_8 );
2929
3030 // Construct the URL with query parameters
3131 String queryParameters = String .format ("?videoID=%s" , encodedVideoID );
3232 String completeUrl = apiUrl + queryParameters ;
3333
34- URL url = new URL (completeUrl );
35- HttpURLConnection con = (HttpURLConnection ) url .openConnection ();
34+ // connect to api and return response if successful
35+ HttpURLConnection con = initializeConnection (completeUrl );
36+ if (con == null ) return null ;
3637
37- // TODO move to Config / separate Object
38- con .setRequestMethod ("GET" );
39- con .setRequestProperty ("Content-Type" , "application/json" );
40- con .setRequestProperty ("Accept" , "application/json" );
41- // handle everything that's not 200
42- if (con .getResponseCode () != HttpURLConnection .HTTP_OK ) {
43- return null ;
44- }
4538 try (BufferedReader br = new BufferedReader (
46- new InputStreamReader (con .getInputStream (), "utf-8" ))) {
39+ new InputStreamReader (con .getInputStream (), StandardCharsets . UTF_8 ))) {
4740 StringBuilder response = new StringBuilder ();
4841 String responseLine ;
4942 while ((responseLine = br .readLine ()) != null ) {
@@ -57,25 +50,17 @@ public String getInitialInformation(String videoID) throws IOException {
5750 * helper class representing the extracted information
5851 */
5952 public static class ProcessedInformation {
60- private String title ;
61- private String url ;
53+ private final String title ;
54+ private final String url ;
6255
6356 public String getTitle () {
6457 return title ;
6558 }
6659
67- public void setTitle (String title ) {
68- this .title = title ;
69- }
70-
7160 public String getUrl () {
7261 return url ;
7362 }
7463
75- public void setUrl (String url ) {
76- this .url = url ;
77- }
78-
7964 public ProcessedInformation (String title , String url ) {
8065 this .title = title ;
8166 this .url = url ;
@@ -85,7 +70,7 @@ public ProcessedInformation(String title, String url) {
8570 /**
8671 * post process information from DeArrow api
8772 *
88- * @param jsonResponse
73+ * @param jsonResponse api response from DeArrow
8974 * @return title and url as ProcessedInformation (null if not existing)
9075 */
9176 public ProcessedInformation processInformation (String videoID , String jsonResponse ) throws IOException {
@@ -125,11 +110,12 @@ public ProcessedInformation processInformation(String videoID, String jsonRespon
125110 }
126111 return new ProcessedInformation (title , url );
127112 }
113+
128114 /**
129115 * fetches thumbnail image
130116 *
131- * @param videoID
132- * @param number
117+ * @param videoID id of video in question
118+ * @param number Thumbnail timestamp (DeArrow internal number)
133119 * @return image url or null
134120 * @throws IOException
135121 */
@@ -138,31 +124,32 @@ public String getImageInformation(String videoID, double number) throws IOExcept
138124 String apiUrl = "https://dearrow-thumb.ajay.app/api/v1/getThumbnail" ;
139125
140126 // Encode the videoID parameter
141- String encodedVideoID = URLEncoder .encode (videoID , "UTF-8" );
127+ String encodedVideoID = URLEncoder .encode (videoID , StandardCharsets . UTF_8 );
142128
143129 // Construct the URL with query parameters
144130 String queryParameters = String .format ("?videoID=%s&time=%s" , encodedVideoID , number );
145131 String completeUrl = apiUrl + queryParameters ;
146-
132+ // get api response and return complete url if successful
133+ HttpURLConnection con = initializeConnection (completeUrl );
134+ if (con == null ) return null ;
135+ else return completeUrl ;
136+ }
137+ /**
138+ * create GET connection to provided url with json as request/ response
139+ * @param completeUrl url you want to connect to (please encode UTF-8)
140+ * @return connection ready to go or null if connection is not ok
141+ * @throws IOException
142+ */
143+ private HttpURLConnection initializeConnection (String completeUrl ) throws IOException {
147144 URL url = new URL (completeUrl );
148145 HttpURLConnection con = (HttpURLConnection ) url .openConnection ();
149146 con .setRequestMethod ("GET" );
150147 con .setRequestProperty ("Content-Type" , "application/json" );
151148 con .setRequestProperty ("Accept" , "application/json" );
152-
153- int responseCode = con .getResponseCode ();
154- if (responseCode == HttpURLConnection .HTTP_OK ) {
155- return completeUrl ;
156- } else return null ;
157-
158- // no need for receiving and process the image
159- /*try (BufferedReader br = new BufferedReader(
160- new InputStreamReader(con.getInputStream(), "utf-8"))) {
161- StringBuilder response = new StringBuilder();
162- String responseLine;
163- while ((responseLine = br.readLine()) != null) {
164- response.append(responseLine.trim());
165- }
166- }*/
149+ // handle everything that's not 200
150+ if (con .getResponseCode () != HttpURLConnection .HTTP_OK ) {
151+ return null ;
152+ }
153+ return con ;
167154 }
168155}
0 commit comments