-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
111 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 iVideoUrl = args[0]; | ||
|
||
try { | ||
Process p = new ProcessBuilder() | ||
.directory(Paths.get(TARGET_DIR_PATH).toFile()) | ||
.command( | ||
ImmutableList.of(YOUTUBE_DOWNLOAD, | ||
iVideoUrl)).inheritIO().start(); | ||
p.waitFor(); | ||
if (p.exitValue() == 0) { | ||
System.out | ||
.println("YurlWorldResource.downloadVideoInSeparateThread() - successfully downloaded " | ||
+ iVideoUrl); | ||
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 { | ||
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, "downloadVideo()"); | ||
System.out.println("downloadVideo() - 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 + "FAILED:\n\t" + iCypherQuery + "\n\tparams: " | ||
+ iParams); | ||
try { | ||
throw new RuntimeException(IOUtils.toString(theResponse.getEntityInputStream())); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
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 + "end"); | ||
} | ||
return new JSONObject(theNeo4jResponse); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
curl -X POST -H 'Content-type: application/json' \ | ||
'http://netgear.rohidekar.com:7474/db/data/cypher' -d ' | ||
{ | ||
"query": "START n=node(37658) MATCH n-->c WHERE HAS(c.url) AND NOT HAS(c.downloaded_video) RETURN c.url", "params":{} | ||
}' |