-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequestAndResponse.java
More file actions
171 lines (162 loc) · 6.73 KB
/
requestAndResponse.java
File metadata and controls
171 lines (162 loc) · 6.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
public final class requestAndResponse {
private static int maxVids;
private static String search;
private static void makeHtml(String request) {
String fileInput = "<!DOCTYPE html>\n" + //
"<head>\n" + //
" <title>\n" + //
" FREE YOUTUBE\n" + //
" </title>\n" + //
" <meta charset=\"utf-8\">\n" + //
" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n"
+ //
" <link rel=\"stylesheet\" type=\"text/css\" href=\"../css/main.css\">\n"
+ //
" <script src=\"../javascript/youtubeSearchEngine.js\"></script>\n"
+ //
" <style>\n" + //
" .youtube-player{\n" + //
" border: 1px solid #1D740C;\n" + //
" height: 390px;\n" + //
" width: 480px;\n" + //
" padding: 0px;\n" + //
" margin-left: 25px;\n" + //
" margin-bottom: 25px;\n" + //
" }\n" + //
" </style>\n" + //
"</head>\n" + //
"<body>\n" + //
" <h1>" + request + "</h1>\n";
System.out.println("Fetching request: " + request);
String responses = serverRequest2(request);
String[] ytId = getYoutubeIds(responses);
String[] newytId = removeDuplicates(ytId);
for (int i = 0; i < newytId.length && i < maxVids; i++) {
//System.out.println(newytId[i]);
if (newytId[i] != null) {
fileInput += "<iframe src=\"" + newytId[i]
+ "\" class=\"youtube-player\" allowfullscreen=\"\" scrolling=\"no\" allow=\"encrypted-media\"></iframe>\n";
}
}
fileInput += "</body>";
fileMan.out("index.html", fileInput);
File file = new File("index.html");
try {
Desktop.getDesktop().browse(file.toURI());
} catch (IOException e) {
System.out.println(
"So... kinda awkward but I can't seemm to open the index.html file that I just wrote to...");
StringBuilder sb = new StringBuilder(file.getAbsolutePath());
sb.deleteCharAt(2);
String newFilePath = sb.toString();
System.out.println(
"You can try openning it this way: file://" + newFilePath);
}
}
private static String[] getYoutubeIds(String responses) {
String[] ytId = new String[maxVids * 10];
String[] ttl = new String[maxVids * 10];
// Regular expression pattern to match YouTube video IDs
String nr = responses;
String pattern = "\"videoId\":\"";
int si = pattern.length();
int i = 0;
while (nr.length() > 0 && nr.indexOf(pattern) > 0 && i < maxVids * 10) {
nr = nr.substring(nr.indexOf(pattern) + si);
ytId[i] = "https://www.youtube.com/embed/" + nr.substring(0, 11);
i++;
}
// pattern = "";
// i = 0;
// nr = responses;
// while (nr.length() > 0 && nr.indexOf(pattern) > 0 && i < maxVids * 10) {
// nr = nr.substring(nr.indexOf(pattern) + si);
// ytId[i] = "https://www.youtube.com/embed/" + nr.substring(0, 11);
// i++;
// }
return ytId;
}
private static String serverRequest2(String Request) {
System.out.println("Request: ");
String newRequest = Request.replace(' ', '+');
// Create a neat value object to hold the URL
String htmlContent = "";
try {
// System.out.println("https://www.youtube.com/results?search_query=" +
// Request);
HttpURLConnection connection = (HttpURLConnection) new URL(
"https://www.youtube.com/" + search + newRequest)
.openConnection();
InputStream inputStream = connection.getInputStream();
try (Scanner scanner = new Scanner(inputStream,
StandardCharsets.UTF_8).useDelimiter("\\A")) {
htmlContent = scanner.hasNext() ? scanner.next() : "";
}
} catch (IOException e) {
System.out.println(
"Yeah, the server hates me some times. Ignore or try again.");
e.printStackTrace();
}
// Manually converting the response body InputStream to APOD using Jackson
// Finally we have the response
return htmlContent;
}
static String[] removeDuplicates(String str[]) {
//System.out.println(str.length);
String[] str2 = new String[str.length];
int k = 0;
String uniVal = "";
for (int i = 0; i < str.length; i++) {
if (uniVal != null && !uniVal.equals(str[i])) {
uniVal = str[i];
str2[k] = str[i];
k++;
}
}
return str2;
}
public static void main(String[] args) {
search = "results?search_query=";
String vidString = fileMan.in("maxVids.txt");
maxVids = Integer.parseInt(vidString);
Scanner in = new Scanner(System.in);
String input;
do {
System.out.print("What would you like to search? ");
input = in.nextLine();
if (input != null && !input.isEmpty()) {
if (input.charAt(0) != '/') {
makeHtml(input);
} else {
if (input.equals("/vidNum")) {
System.out.print(
"How many videos would you like to see per page? ");
int num = in.nextInt();
fileMan.out("maxVids.txt", String.valueOf(num));
input = "";
} else if (input.equals("/channel")) {
System.out.print(
"What channel would you like to search? ");
search = "@";
String channel = in.nextLine();
channel.toLowerCase();
channel += "/videos";
makeHtml(channel);
} else {
System.out.println(
"Sorry, I don't know that command yet");
}
}
}
} while (input != null && input != "");
in.close();
}
}