My favorites
▼
|
Sign in
youtube-captions-uploader
YouTube captions uploader written in Java for App Engine
Project Home
Downloads
Wiki
Issues
Source
Checkout
Browse
Changes
Source path:
svn
/
trunk
/
yt-caption-uploader
/
src
/
com
/
google
/
youtube
/
captions
/
SubmitCaptionTask.java
r21
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/* Copyright 2010 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
package com.google.youtube.captions;
import com.google.appengine.api.blobstore.BlobInfo;
import com.google.appengine.api.blobstore.BlobInfoFactory;
import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
import com.google.appengine.api.channel.ChannelFailureException;
import com.google.appengine.api.channel.ChannelMessage;
import com.google.appengine.api.channel.ChannelService;
import com.google.appengine.api.channel.ChannelServiceFactory;
import com.google.appengine.api.utils.SystemProperty;
import com.google.appengine.repackaged.org.json.JSONException;
import com.google.appengine.repackaged.org.json.JSONObject;
import com.google.gdata.client.Service.GDataRequest;
import com.google.gdata.client.youtube.YouTubeService;
import com.google.gdata.util.ServiceException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@SuppressWarnings("serial")
public class SubmitCaptionTask extends HttpServlet {
private static final Logger LOG = Logger.getLogger(SubmitCaptionTask.class.getName());
private BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
private ChannelService channelService = ChannelServiceFactory.getChannelService();
private static final String CAPTION_FEED_URL_FORMAT = "http://gdata.youtube.com/feeds/api/"
+ "videos/%s/captions";
private static final String CONTENT_TYPE = "application/vnd.youtube.timedtext; charset=UTF-8";
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) {
LOG.info("Starting up...");
String fileName = null;
String channelId = null;
BlobKey blobKey = null;
try {
String blobKeyString = req.getParameter("blobKey");
if (Util.isEmptyOrNull(blobKeyString)) {
throw new IllegalArgumentException("Required parameter 'blobKey' not found.");
}
blobKeyString = URLDecoder.decode(blobKeyString, "UTF-8");
blobKey = new BlobKey(blobKeyString);
String authSubToken = req.getParameter("authSubToken");
if (Util.isEmptyOrNull(authSubToken)) {
throw new IllegalArgumentException("Required parameter 'authSubToken' not found.");
}
authSubToken = URLDecoder.decode(authSubToken, "UTF-8");
channelId = req.getParameter("channelId");
if (Util.isEmptyOrNull(channelId)) {
throw new IllegalArgumentException("Required parameter 'channelId' not found.");
}
channelId = URLDecoder.decode(channelId, "UTF-8");
LOG.fine("Channel id is " + channelId);
BlobInfoFactory blobInfoFactory = new BlobInfoFactory();
BlobInfo blobInfo = blobInfoFactory.loadBlobInfo(blobKey);
// Possible valid track name formats:
// 6VVuLGk8kVU_en.sbv
// 6VVuLGk8kVU_es_Spanish Track.srt
fileName = blobInfo.getFilename();
String regex = "(.{11})_([^_]+?)(?:_(.+))?\\.\\w{3}";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(fileName);
if (!matcher.matches()) {
throw new IllegalArgumentException(String.format("Couldn't parse video id and language "
+ "from file name '%s'.", fileName));
}
String videoId = matcher.group(1);
String languageCode = matcher.group(2);
String trackName = matcher.group(3);
LOG.info(String.format("File name is '%s', videoId is '%s', language code is '%s', track "
+ "name is '%s'.", fileName, videoId, languageCode, trackName));
YouTubeService service = new YouTubeService(SystemProperty.applicationId.get(),
Util.DEVELOPER_KEY);
service.setAuthSubToken(authSubToken);
String captionsUrl = String.format(CAPTION_FEED_URL_FORMAT, videoId);
GDataRequest request = service.createInsertRequest(new URL(captionsUrl));
request.setHeader("Content-Language", languageCode);
if (trackName != null) {
request.setHeader("Slug", trackName);
}
request.setHeader("Content-Type", CONTENT_TYPE);
request.getRequestStream().write(blobstoreService.fetchData(blobKey, 0,
blobInfo.getSize() - 1));
request.execute();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(
request.getResponseStream()));
StringBuilder builder = new StringBuilder();
String line = null;
while ((line = bufferedReader.readLine()) != null) {
builder.append(line);
}
bufferedReader.close();
String responseBody = builder.toString();
LOG.info("Response to captions request: " + responseBody);
regex = ".+reasonCode=[\"'](.+?)[\"'].+";
pattern = Pattern.compile(regex);
matcher = pattern.matcher(responseBody);
if (matcher.matches()) {
throw new ServiceException("Caption file was rejected: " + matcher.group(1));
}
JSONObject jsonResponse = new JSONObject();
jsonResponse.put("fileName", fileName);
jsonResponse.put("success", true);
try {
channelService.sendMessage(new ChannelMessage(channelId, jsonResponse.toString()));
} catch(ChannelFailureException e) {
LOG.log(Level.WARNING, "", e);
}
} catch (IllegalArgumentException e) {
reportError(channelId, fileName, e);
} catch (MalformedURLException e) {
reportError(channelId, fileName, e);
} catch (IOException e) {
reportError(channelId, fileName, e);
} catch (ServiceException e) {
reportError(channelId, fileName, e);
} catch (JSONException e) {
LOG.log(Level.WARNING, "", e);
} finally {
if (blobKey != null) {
blobstoreService.delete(blobKey);
LOG.info(String.format("Blob entry '%s' was deleted.", blobKey.getKeyString()));
}
}
}
private void reportError(String channelId, String fileName, Exception exception) {
LOG.log(Level.WARNING, "", exception);
if (!Util.isEmptyOrNull(channelId) && !Util.isEmptyOrNull(fileName)) {
try {
JSONObject jsonResponse = new JSONObject();
jsonResponse.put("fileName", fileName);
jsonResponse.put("success", false);
jsonResponse.put("error", exception.getMessage());
channelService.sendMessage(new ChannelMessage(channelId, jsonResponse.toString()));
} catch(JSONException e) {
LOG.log(Level.WARNING, "", e);
} catch(ChannelFailureException e) {
LOG.log(Level.WARNING, "", e);
}
}
}
}
Show details
Hide details
Change log
r3
by api.jeffy on Jan 6, 2011
Diff
Adding all the source files.
Go to:
.../yt-caption-uploader/.checkstyle
...k/yt-caption-uploader/.classpath
/trunk/yt-caption-uploader/.project
...nk/yt-caption-uploader/.settings
...gle.appengine.eclipse.core.prefs
...om.google.gdt.eclipse.core.prefs
...om.google.gwt.eclipse.core.prefs
/trunk/yt-caption-uploader/COPYING
/trunk/yt-caption-uploader/src
...yt-caption-uploader/src/META-INF
...oader/src/META-INF/jdoconfig.xml
/trunk/yt-caption-uploader/src/com
...-caption-uploader/src/com/google
...-uploader/src/com/google/youtube
.../src/com/google/youtube/captions
...utube/captions/AuthSubLogin.java
...tube/captions/AuthSubLogout.java
...youtube/captions/BlobUpload.java
...ptions/GenerateChannelToken.java
.../captions/GenerateUploadUrl.java
.../captions/SubmitCaptionTask.java
...oogle/youtube/captions/Util.java
...on-uploader/src/log4j.properties
/trunk/yt-caption-uploader/war
.../yt-caption-uploader/war/WEB-INF
.../war/WEB-INF/appengine-generated
...appengine-generated/local_db.bin
...er/war/WEB-INF/appengine-web.xml
...caption-uploader/war/WEB-INF/lib
.../appengine-api-1.0-sdk-1.4.0.jar
...lib/appengine-api-labs-1.4.0.jar
.../appengine-jsr107cache-1.4.0.jar
...ucleus-appengine-1.0.8.final.jar
...F/lib/datanucleus-core-1.1.5.jar
...NF/lib/datanucleus-jpa-1.1.5.jar
...WEB-INF/lib/gdata-client-1.0.jar
...NF/lib/gdata-client-meta-1.0.jar
...r/WEB-INF/lib/gdata-core-1.0.jar
.../WEB-INF/lib/gdata-media-1.0.jar
...EB-INF/lib/gdata-youtube-2.0.jar
...F/lib/gdata-youtube-meta-2.0.jar
.../geronimo-jpa_3.0_spec-1.1.1.jar
.../geronimo-jta_1.1_spec-1.1.1.jar
...F/lib/google-collect-1.0-rc1.jar
.../WEB-INF/lib/jdo2-api-2.3-eb.jar
.../WEB-INF/lib/jsr107cache-1.1.jar
...r/war/WEB-INF/logging.properties
...ion-uploader/war/WEB-INF/web.xml
/trunk/yt-caption-uploader/war/css
...ption-uploader/war/css/index.css
Project members,
sign in
to write a code review
Older revisions
All revisions of this file
File info
Size: 7518 bytes, 190 lines
View raw file
Powered by
Google Project Hosting