/
youtube_to_internal.req.server.java
176 lines (129 loc) · 5.03 KB
/
youtube_to_internal.req.server.java
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
172
173
174
175
176
#rights=ADMIN
//-------------------------------------------------------------------
// ==ServerScript==
// @name youtube_to_internal
// @status on
// @description
// @include http://.*\.c\.youtube\.com/videoplayback\?.*id\=.*
// @exclude
// @responsecode 200
// ==/ServerScript==
// --------------------------------------------------------------------
// Note: use httpMessage object methods to manipulate HTTP Message
// use debug(String s) method to trace items in service log (with log level >=FINE)
// ---------------
// ---------------
//import java.util.Scanner;
/*import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ParameterMetaData;
import java.sql.ResultSet;*/
import java.sql.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public void main(HttpMessage httpMessage){
//start your code from here
try{
debug(httpMessage.getRequestHeaders());
String url = httpMessage.getUrl();
String response = "nochange";
debug( "\r\n+++++request url++++\r\n");
debug( url );
if ( getvid(url).startsWith("id=") ){
setvid(url, getvid(url));
response = "http://youtube.squid.internal/" + getvid(url);
}
debug( "\r\n+++++response url++++\r\n");
debug( response.toString() );
debug( "\r\n+++++response url++++\r\n");
if( !response.startsWith("nochange")){
httpMessage.setUrl(response);
debug( "\r\n+++++ url was changed ++++\r\n");
}
debug("++rewrittenurl++\r\n"+httpMessage.getUrl() +"\r\n");
// httpMessage.setUrl();
debug( "+++end internal to youtube+++\r\n" + httpMessage.getRequestHeaders()+"\r\n");
}catch(Exception e){
e.printStackTrace();
}
}
public static String getvid(String url){
String vid = "no";
if(getid(url).toString() != "no"){
vid = getid(url) ;
}
if(getitag(url).toString() != "no"){
vid = vid.toString() + "&" +getitag(url) ;
}
if(getrange(url).toString() != "no"){
vid = vid.toString() + "&" + getrange(url);
}
return vid;
}
private static String getid(String url){
Pattern pid = Pattern.compile(".*(id\\=[a-zA-Z0-9]+).*");
Matcher mid = pid.matcher(url);
if (mid.find())
{
return mid.group(1).toString();
}
return "no";
}
private static String getrange(String url){
Pattern pr = Pattern.compile(".*(range\\=[a-zA-Z0-9\\-]+).*");
Matcher mr = pr.matcher(url);
if (mr.find())
{
return mr.group(1).toString();
}
return "no";
}
private static String getitag(String url){
Pattern pit = Pattern.compile(".*(itag\\=[0-9]+).*");
Matcher mit = pit.matcher(url);
if (mit.find())
{
return mit.group(1).toString();
}
return "no";
}
public static String geturl(String vid) throws Exception {
String url = "empty";
PreparedStatement pst = null;
Connection con = getMySqlConnection();
String query = "CALL geturl ('" + vid.toString() + "' );";
pst = con.prepareStatement(query);
ResultSet result = pst.executeQuery();
if (result.next())
{
url = result.getString(1);
}
con.close();
return url;
}
/*
* @param url
* @param vid
*/
public static void setvid(String url,String vid) throws Exception {
// System.out.println(url);
// System.out.println(vid);
PreparedStatement pst = null;
Connection con = getMySqlConnection();
// the procedure takes the first value as vid and the second as url
String query = "CALL seturl ('" + vid.toString()
+ "', '" + url.toString()+ "');";
pst = con.prepareStatement(query);
pst.executeQuery();
con.close();
}
public static Connection getMySqlConnection() throws Exception {
String driver = "org.gjt.mm.mysql.Driver";
String url = "jdbc:mysql://sql1/ytcache";
String username = "ytcache";
String password = "ytcache";
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, username, password);
return conn;
}