-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideo_download.groovy
103 lines (94 loc) · 3.66 KB
/
video_download.groovy
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
import java.io.IOException;
import java.nio.file.Paths;
import java.util.Map;
import javax.ws.rs.core.MediaType;
import org.apache.commons.io.IOUtils;
import org.json.JSONObject;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.json.JSONConfiguration;
public class VideoDownloadNeo4j {
private static final String YOUTUBE_DOWNLOAD = "/home/sarnobat/bin/youtube_download";
private static final Integer ROOT_ID = 45;
private static final String CYPHER_URI = "http://netgear.rohidekar.com:7474/db/data/cypher";
// private static final String TARGET_DIR_PATH = "/media/sarnobat/Unsorted/Videos/";
public static void main(String[] args) {
String iTargetDirPath = args[0];
String iVideoUrl = args[1];
try {
Process p = new ProcessBuilder()
.directory(Paths.get(iTargetDirPath).toFile())
.command(
ImmutableList.of(YOUTUBE_DOWNLOAD,
iVideoUrl)).inheritIO().start();
p.waitFor();
if (p.exitValue() == 0) {
writeSuccessToDb(iVideoUrl);
} else {
System.err
.println("YurlWorldResource.downloadVideoInSeparateThread() - error downloading "
+ iVideoUrl);
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void writeSuccessToDb(final String iVideoUrl)
throws IOException {
System.out.println("writeSuccessToDb() - Attempting to record successful download in database...");
execute("start n=node(*) WHERE n.url = {url} SET n.downloaded_video = {date}",
ImmutableMap.<String, Object> of("url", iVideoUrl,
"date", new Long(System.currentTimeMillis())), true, "writeSuccessToDb()");
System.out.println("writeSuccessToDb() - Download recorded in database");
}
private static JSONObject execute(String iCypherQuery,
Map<String, Object> iParams, boolean doLogging, String... iCommentPrefix) {
String commentPrefix = iCommentPrefix.length > 0 ? iCommentPrefix[0] + " " : "";
if (doLogging) {
System.out.println(commentPrefix + "-\t" + iCypherQuery);
System.out.println(commentPrefix + "-\tparams - " + iParams);
}
ClientConfig clientConfig = new DefaultClientConfig();
clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING,
Boolean.TRUE);
// POST {} to the node entry point URI
ClientResponse theResponse = Client.create(clientConfig).resource(
CYPHER_URI)
.accept(MediaType.APPLICATION_JSON)
.type(MediaType.APPLICATION_JSON).entity("{ }")
.post(ClientResponse.class, ImmutableMap
.<String, Object> of("query", iCypherQuery, "params",
Preconditions.checkNotNull(iParams)));
if (theResponse.getStatus() != 200) {
System.out.println(commentPrefix + "-\tFAILED:\n\t" + iCypherQuery + "\n\tparams: "
+ iParams);
try {
throw new RuntimeException(IOUtils.toString(theResponse.getEntityInputStream()));
} catch (IOException e) {
throw new RuntimeException(e);
}
} else {
String theNeo4jResponse ;
try {
// Do not inline this. We need to close the stream after
// copying
theNeo4jResponse = IOUtils.toString(theResponse.getEntityInputStream());
theResponse.getEntityInputStream().close();
theResponse.close();
if (doLogging) {
System.out.println(commentPrefix + "-\tSUCCESS - end");
}
return new JSONObject(theNeo4jResponse);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}