-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.groovy
1290 lines (1174 loc) · 50.2 KB
/
server.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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// I think the only remaining method in use here is:
// Request URL: http://netgear.rohidekar.com:44447/yurl/parent?nodeId=641476
// Everything else doesn't use port 4447
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import javax.imageio.ImageIO;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.glassfish.jersey.jdkhttp.JdkHttpServerFactory;
import org.glassfish.jersey.server.ResourceConfig;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSet.Builder;
import com.google.common.collect.Multimap;
import com.google.common.util.concurrent.SimpleTimeLimiter;
//import org.jvnet.hk2.annotations.Optional;
/**
* Deprecation doesn't mean the method can be removed. Only when index.html stops referring to it can it be removed.
*
* Neo4j dependencies removed.
*
* Only writing to the persistent store (and the associated async tasks) should remain in this thread.
*/
// TODO: rename to YurlStash
public class YurlStash {
public static final String YOUTUBE_DOWNLOAD = System.getProperty("user.home") + "/bin/youtube_download";
public static final Integer ROOT_ID = 45;
@Deprecated
private static final String CYPHER_URI = "http://netgear.rohidekar.com:7474/db/data/cypher";
// @Deprecated
private static final String TARGET_DIR_PATH = "/media/sarnobat/3TB/new/move_to_unsorted/videos/";
private static final String QUEUE_DIR = System.getProperty("user.home") + "/db.git/yurl_flatfile_db/";
//@Deprecated
//private static final String QUEUE_FILE = "/home/sarnobat/sarnobat.git/";
private static final String QUEUE_FILE = QUEUE_DIR;
private static final String QUEUE_FILE_TXT = "yurl_queue.txt";
private static final String TITLE_FILE_TXT = "yurl_titles_2017.txt";
private static final String QUEUE_FILE_TXT_MASTER = "yurl_master.txt";
private static final String QUEUE_FILE_TXT_2017 = "yurl_queue_2017.txt";// Started using this in Aug 2017. Older data is not in this file.
private static final String QUEUE_FILE_TXT_DELETE = "yurl_deleted.txt";
private static final String TARGET_DIR_PATH_IMAGES = "/media/sarnobat/3TB/new/move_to_unsorted/images/";
// usually full and we get zero size files: "/media/sarnobat/Unsorted/images/";
private static final String TARGET_DIR_PATH_IMAGES_OTHER = "/media/sarnobat/3TB/new/move_to_unsorted/images/other";
// usually full and images don't get saved "/media/sarnobat/Unsorted/images/other";
@Path("yurl")
public static class YurlResource { // Must be public
static {
// We can't put this in the constructor because multiple instances will get created
// YurlWorldResource.downloadUndownloadedVideosInSeparateThread() ;
}
private static JSONObject categoriesTreeCache;
// This only gets invoked when it receives the first request
// Multiple instances get created
public YurlResource() {
// We can't put the auto downloader in main()
// then either it will be called every time the cron job is executed,
// or not until the server terminates unexceptionally (which never happens).
}
@GET
@Path("downloadVideo")
@Produces("application/json")
public Response downloadVideoSynchronous(@QueryParam("id") Integer iRootId, @QueryParam("url") String iUrl)
throws JSONException, IOException {
DownloadVideo.getVideoDownloadJobHttpcat(iUrl, TARGET_DIR_PATH).run();
JSONObject retVal = new JSONObject();
try {
return Response.ok().header("Access-Control-Allow-Origin", "*")
.entity(retVal.toString())
.type("application/json").build();
}
catch (Exception e) {
e.printStackTrace();
return Response.serverError().header("Access-Control-Allow-Origin", "*")
.entity(e.getStackTrace())
.type("application/text").build();
}
}
@GET
@Path("parent")
@Produces("application/json")
public Response parent(@QueryParam("nodeId") Integer iNodeId)
throws JSONException, IOException {
ImmutableMap.Builder<String, Object> theParams = ImmutableMap.<String, Object>builder();
theParams.put("nodeId", iNodeId);
// TODO: order these by most recent-first (so that they appear this
// way in the UI)
JSONObject theParentNodeJson = execute(
"start n=node({nodeId}) MATCH p-[r:CONTAINS]->n RETURN id(p)",
theParams.build(), "parent()");
JSONArray theData = (JSONArray) theParentNodeJson.get("data");
JSONArray ret = new JSONArray();
for (int i = 0; i < theData.length(); i++) {
JSONArray a = theData.getJSONArray(i);
JSONObject o = new JSONObject();
String id = (String) a.get(0);
o.put("id", id);
ret.put(o);
}
return Response.ok().header("Access-Control-Allow-Origin", "*")
.entity(ret.toString()).type("application/json").build();
}
//
// Batch
//
@GET
@Path("batchInsert")
@Produces("application/json")
public Response batchInsert(@QueryParam("rootId") Integer iRootId,
@QueryParam("urls") String iUrls) throws Exception {
Preconditions.checkArgument(iRootId != null);
StringBuffer unsuccessfulLines = new StringBuffer();
String[] lines = iUrls.trim().split("\\n");
int i = 0;
while (i < lines.length) {
String first = lines[i];
try {
if (first.startsWith("http")) {
stash(URLEncoder.encode(first, "UTF-8"), iRootId);
++i;
continue;
}
// Fails if it sees the string "%)"
URLDecoder.decode(first, "UTF-8");
} catch (Exception e) {
System.out.println(e.getStackTrace());
addToUnsuccessful(unsuccessfulLines, first, "");
++i;
continue;
}
if (first.startsWith("=")) {
++i;
addToUnsuccessful(unsuccessfulLines, first, "");
continue;
}
if (first.startsWith("http")) {
++i;
addToUnsuccessful(unsuccessfulLines, first, "");
continue;
}
if (first.matches("^\\s*" + '$')) {
++i;
continue;
}
String second = lines[i + 1];
if (first.startsWith("\"") && second.startsWith("http")) {
System.out.println("to be processed: " + first);
System.out.println("to be processed: " + second);
stash(second, iRootId);
} else {
addToUnsuccessful(unsuccessfulLines, first, second);
}
i += 2;
}
JSONObject entity = new JSONObject();
entity.put("unsuccessful", unsuccessfulLines.toString());
return Response.ok().header("Access-Control-Allow-Origin", "*")
.entity(entity.toString()).type("application/json").build();
}
// TODO : remove mutable state
@Deprecated
public void addToUnsuccessful(StringBuffer unsuccessfulLines,
String first, String second) {
unsuccessfulLines.append(first);
unsuccessfulLines.append("\n");
unsuccessfulLines.append(second);
unsuccessfulLines.append("\n");
unsuccessfulLines.append("\n");
}
// ------------------------------------------------------------------------------------
// Page operations
// ------------------------------------------------------------------------------------
////
//// The main part
////
// TODO: See if you can turn this into a map-reduce
@SuppressWarnings("unused")
private JSONObject getItemsAtLevelAndChildLevels(Integer iRootId) throws JSONException, IOException {
// System.out.println("getItemsAtLevelAndChildLevels() - " + iRootId);
if (categoriesTreeCache == null) {
categoriesTreeCache = ReadCategoryTree.getCategoriesTree(YurlStash.ROOT_ID);
}
// TODO: the source is null clause should be obsoleted
JSONObject theQueryResultJson = execute(
"START source=node({rootId}) "
+ "MATCH p = source-[r:CONTAINS*1..2]->u "
+ "WHERE (source is null or ID(source) = {rootId}) and not(has(u.type)) AND id(u) > 0 "
+ "RETURN distinct ID(u),u.title,u.url, extract(n in nodes(p) | id(n)) as path,u.downloaded_video,u.downloaded_image,u.created,u.ordinal, u.biggest_image, u.user_image "
// TODO : do not hardcode the limit to 500. Category 38044 doesn't display more than 50 books since there are so many child items.
+ "ORDER BY u.ordinal DESC LIMIT 500", ImmutableMap
.<String, Object> builder().put("rootId", iRootId)
.build(), "getItemsAtLevelAndChildLevels()");
JSONArray theDataJson = (JSONArray) theQueryResultJson.get("data");
JSONArray theUncategorizedNodesJson = new JSONArray();
for (int i = 0; i < theDataJson.length(); i++) {
JSONObject anUncategorizedNodeJsonObject = new JSONObject();
_1: {
JSONArray anItem = theDataJson.getJSONArray(i);
_11: {
String anId = (String) anItem.get(0);
anUncategorizedNodeJsonObject.put("id", anId);
}
_12: {
String aTitle = (String) anItem.get(1);
anUncategorizedNodeJsonObject.put("title", aTitle);
}
_13: {
String aUrl = (String) anItem.get(2);
anUncategorizedNodeJsonObject.put("url", aUrl);
}
_14: {
try {
JSONArray path = (JSONArray) anItem.get(3);
if (path.length() == 3) {
anUncategorizedNodeJsonObject.put("parentId",
path.get(1));
} else if (path.length() == 2) {
anUncategorizedNodeJsonObject.put("parentId",
iRootId);
}
else if (path.length() == 1) {
// This should never happen
anUncategorizedNodeJsonObject.put("parentId",
path.get(0));
}
} catch (Exception e) {
e.printStackTrace();
}
}
_15: {
Object val = anItem.get(4);
if (isNotNull(val)) {
String aValue = (String) val;
anUncategorizedNodeJsonObject.put("downloaded_video", aValue);
}
}
_16: {
Object val = anItem.get(6);
if ("null".equals(val)) {
System.out.println("Is null value");
} else if (val == null) {
System.out.println("Is null string");
} else if (isNotNull(val)) {
Long aValue = (Long) val;
anUncategorizedNodeJsonObject.put("created", aValue);
}
}
_17: {
Object val = anItem.get(8);
if (isNotNull(val)) {
String aValue = (String) val;
if ("null".equals(aValue)) {
System.out.println("YurlWorldResource.getItemsAtLevelAndChildLevels() - does this ever occur? 1");
}
anUncategorizedNodeJsonObject.put("biggest_image", aValue);
}
}
_18: {
Object val = anItem.get(9);
if (isNotNull(val)) {
String aValue = (String) val;
if ("null".equals(aValue)) {
}
anUncategorizedNodeJsonObject.put("user_image", aValue);
}
}
}
theUncategorizedNodesJson.put(anUncategorizedNodeJsonObject);
}
JSONObject ret = new JSONObject();
transform : {
for (int i = 0; i < theUncategorizedNodesJson.length(); i++) {
JSONObject jsonObject = (JSONObject) theUncategorizedNodesJson
.get(i);
String parentId = (String) jsonObject.get("parentId");
if (!ret.has(parentId)) {
ret.put(parentId, new JSONArray());
}
JSONArray target = (JSONArray) ret.get(parentId);
target.put(jsonObject);
}
}
return ret;
}
private static boolean isNotNull(Object val) {
return val != null && !("null".equals(val)) && !(val.getClass().equals(YurlResource.JSON_OBJECT_NULL));
}
// -----------------------------------------------------------------------------
// Key bindings
// -----------------------------------------------------------------------------
// --------------------------------------------------------------------------------------
// Write operations
// --------------------------------------------------------------------------------------
@GET
@Path("stash")
@Produces("application/json")
public Response stash(@QueryParam("param1") String iUrl,
@QueryParam("rootId") Integer iCategoryId) throws JSONException,
IOException {
System.out.println("stash() begin");
// This will convert
String theHttpUrl = URLDecoder.decode(iUrl, "UTF-8");
System.out.println("stash() theHttpUrl = " + theHttpUrl);
try {
// TODO: append synchronously to new yurl master queue
appendToTextFileSync(iUrl, iCategoryId.toString(), QUEUE_FILE, YurlStash.QUEUE_FILE_TXT_MASTER);
launchAsynchronousTasksHttpcat(theHttpUrl, iCategoryId);
// TODO: check that it returned successfully (redundant?)
// System.out.println("stash() - node created: " + nodeId);
System.out.println("YurlStash.YurlResource.stash() sending empty json response. This should work.");
return Response.ok().header("Access-Control-Allow-Origin", "*")
.entity(new JSONObject().toString())
.type("application/json").build();
} catch (Exception e) {
e.printStackTrace();
throw new JSONException(e);
}
}
private static void launchAsynchronousTasksHttpcat(final String iUrl, Integer iCategoryId) throws IOException, InterruptedException {
appendToTextFileSync(iUrl, iCategoryId.toString(), QUEUE_DIR, YurlStash.QUEUE_FILE_TXT_2017);
// Delete the url cache file for this category. It will get
// regenrated next time we load that category page.
removeCategoryCache(iCategoryId);
// Get the title
_10: {
final String theTitle = getTitle(new URL(iUrl));
if (theTitle != null && theTitle.length() > 0) {
Runnable r = new Runnable() {
// @Override
public void run() {
String titleFileStr = YurlStash.QUEUE_FILE + "/" + YurlStash.TITLE_FILE_TXT;
File file = Paths.get(titleFileStr).toFile();
File file2 = Paths.get(YurlStash.QUEUE_FILE).toFile();
if (!file2.exists()) {
throw new RuntimeException("Non-existent: " + file.getAbsolutePath());
}
String command = "echo '" + iUrl + "::" + theTitle + "' | tee -a '" + titleFileStr + "'";
System.out.println("appendToTextFile() - " + command);
Process p;
try {
p = new ProcessBuilder()
.directory(file2)
.command("echo","hello world")
.command("/bin/sh", "-c", command)
.inheritIO().start();
try {
p.waitFor();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (p.exitValue() == 0) {
System.out.println("appendToTextFile() - successfully appended 2 "
+ iUrl);
} else {
System.out.println("appendToTextFile() - error appending " + iUrl);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
new Thread(r).start();
}
}
// This is not (yet) the master file. The master file is written to synchronously.
appendToTextFile(iUrl, iCategoryId.toString(), QUEUE_FILE);
String targetDirPathImages;
if (iCategoryId.longValue() == 29172) {
targetDirPathImages = TARGET_DIR_PATH_IMAGES_OTHER;
} else {
targetDirPathImages = TARGET_DIR_PATH_IMAGES;
}
DownloadImage.downloadImageInSeparateThreadHttpcat(iUrl, targetDirPathImages, CYPHER_URI);
DownloadVideo.downloadVideoInSeparateThreadHttpcat(iUrl, TARGET_DIR_PATH, CYPHER_URI);
if (!iUrl.contains("amazon")) {
// We end up with garbage images if we try to screen-scrape Amazon.
// The static rules result in better images.
// TODO: Store this successful image download outside Neo4j.
//BiggestImage.recordBiggestImage(iUrl, CYPHER_URI, id);
}
}
private static void removeCategoryCache(Integer iCategoryId) {
java.nio.file.Path path1 = Paths.get(System.getProperty("user.home") + "/github/yurl/tmp/urls/" + iCategoryId + ".json");
path1.toFile().delete();
System.out.println("Yurl.YurlResource.launchAsynchronousTasksHttpcat() deleted cache file 1: " + path1);
java.nio.file.Path path = Paths.get(System.getProperty("user.home") + "/github/yurl/tmp/categories/topology/" + iCategoryId + ".txt");
path.toFile().delete();
System.out.println("Yurl.YurlResource.launchAsynchronousTasksHttpcat() deleted cache file 2: " + path);
}
private static void appendToTextFileSync(final String iUrl,
final String id, final String dir, String file2, long created)
throws IOException, InterruptedException {
String queueFile = dir + "/" + file2;
File file = Paths.get(dir).toFile();
if (!file.exists()) {
throw new RuntimeException("Non-existent: " + file.getAbsolutePath());
}
String command = "echo '" + id + "::" + iUrl + "::"+created+"' | tee -a '" + queueFile + "'";
System.out.println("appendToTextFileSync() - " + command);
Process p = new ProcessBuilder()
.directory(file)
.command("echo","hello world")
.command("/bin/sh", "-c", command)
//"touch '" + queueFile + "'; echo '" + id + ":" + iUrl + "' >> '" + queueFile + "'"
.inheritIO().start();
p.waitFor();
if (p.exitValue() == 0) {
System.out.println("appendToTextFileSync() - successfully appended 3 "
+ iUrl);
} else {
System.out.println("appendToTextFileSync() - error appending " + iUrl);
}
}
private static void appendToTextFileSync(final String iUrl, final String id, final String dir, String file2) throws IOException,
InterruptedException {
String queueFile = dir + "/" + file2;
File file = Paths.get(dir).toFile();
if (!file.exists()) {
throw new RuntimeException("Non-existent: " + file.getAbsolutePath());
}
String command = "echo '" + id + "::" + iUrl + "::'`date +%s` | tee -a '" + queueFile + "'";
System.out.println("appendToTextFileSync() - " + command);
Process p = new ProcessBuilder()
.directory(file)
.command("echo","hello world")
.command("/bin/sh", "-c", command)
//"touch '" + queueFile + "'; echo '" + id + ":" + iUrl + "' >> '" + queueFile + "'"
.inheritIO().start();
p.waitFor();
if (p.exitValue() == 0) {
System.out.println("appendToTextFileSync() - successfully appended 4 "
+ iUrl);
} else {
System.out.println("appendToTextFileSync() - error appending " + iUrl);
}
}
private static void appendToTextFile(final String iUrl, final String id, final String dir) throws IOException,
InterruptedException {
Runnable r = new Runnable() {
// @Override
public void run() {
String queueFile = dir + "/" + YurlStash.QUEUE_FILE_TXT;
File file = Paths.get(dir).toFile();
if (!file.exists()) {
throw new RuntimeException("Non-existent: " + file.getAbsolutePath());
}
String command = "echo '" + id + "::" + iUrl + "::'`date +%s` | tee -a '" + queueFile + "'";
System.out.println("appendToTextFile() - " + command);
Process p;
try {
p = new ProcessBuilder()
.directory(file)
.command("echo","hello world")
.command("/bin/sh", "-c", command)
//"touch '" + queueFile + "'; echo '" + id + ":" + iUrl + "' >> '" + queueFile + "'"
.inheritIO().start();
try {
p.waitFor();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (p.exitValue() == 0) {
System.out.println("appendToTextFile() - successfully appended 5 "
+ iUrl + " to " + queueFile);
} else {
System.out.println("appendToTextFile() - error appending " + iUrl);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
new Thread(r).start();
}
private static final ExecutorService executorService = Executors.newFixedThreadPool(2);
private static class DownloadImage {
private static void downloadImageInSeparateThreadHttpcat(final String iUrl2,
final String targetDirPath, final String cypherUri) {
final String iUrl = iUrl2.replaceAll("\\?.*", "");
Runnable r = new Runnable() {
// @Override
public void run() {
System.out.println("downloadImageInSeparateThreadHttpcat() - " + iUrl + " :: " + targetDirPath);
if (iUrl.toLowerCase().contains(".jpg")) {
} else if (iUrl.toLowerCase().contains(".jpeg")) {
} else if (iUrl.toLowerCase().contains(".png")) {
} else if (iUrl.toLowerCase().contains(".gif")) {
} else if (iUrl.toLowerCase().contains("gstatic")) {
} else {
return;
}
System.out
.println("Yurl.YurlResource.DownloadImage.downloadImageInSeparateThreadHttpcat() Is of image type");
try {
System.out.println("Yurl.YurlResource.DownloadImage.downloadImageInSeparateThreadHttpcat() About to call saveimage");
saveImage(iUrl, targetDirPath);
System.out.println("Yurl.YurlResource.DownloadImage.downloadImageInSeparateThreadHttpcat() About to call execute: TODO - record the fact that we downloaded the image somwhere");
System.out.println("YurlWorldResource.downloadImageInSeparateThreadHttpcat() - image download recorded");
} catch (Exception e) {
System.out.println("YurlWorldResource.downloadImageInSeparateThreadHttpcat(): 1 Biggest image couldn't be determined" + e.getMessage());
}
}
};
new Thread(r).start();
}
private static void saveImage(String urlString, String targetDirPath)
throws IllegalAccessError, IOException {
System.out.println("saveImage() - " + urlString + "\t::\t" + targetDirPath);
String extension = FilenameUtils.getExtension(urlString);
ImageIO.write(
ImageIO.read(new URL(urlString)),
extension,
new File(determineDestinationPathAvoidingExisting(
targetDirPath + "/" + URLDecoder.decode(FilenameUtils.getBaseName(urlString)
.replaceAll("/", "-"), "UTF-8") + "." + extension)
.toString()));
System.out.println("saveImage() - SUCCESS: " + urlString + "\t::\t" + targetDirPath);
}
private static java.nio.file.Path determineDestinationPathAvoidingExisting(
String iDestinationFilePath) throws IllegalAccessError {
String theDestinationFilePathWithoutExtension = iDestinationFilePath.substring(0,
iDestinationFilePath.lastIndexOf('.'));
String extension = FilenameUtils.getExtension(iDestinationFilePath);
java.nio.file.Path oDestinationFile = Paths.get(iDestinationFilePath);
while (Files.exists(oDestinationFile)) {
theDestinationFilePathWithoutExtension += "1";
iDestinationFilePath = theDestinationFilePathWithoutExtension + "." + extension;
oDestinationFile = Paths.get(iDestinationFilePath);
}
if (Files.exists(oDestinationFile)) {
throw new IllegalAccessError("an existing file will get overwritten");
}
return oDestinationFile;
}
}
private static String getTitle(final URL iUrl) {
String title = "";
try {
title = Executors
.newFixedThreadPool(2)
.invokeAll(
ImmutableSet.<Callable<String>> of(new Callable<String>() {
public String call() throws Exception {
try {
return Jsoup.connect(iUrl.toString()).get().title();
} catch (org.jsoup.UnsupportedMimeTypeException e) {
System.out.println("YurlResource.getTitle() - " + e.getMessage());
return "";
}
}
}), 3000L, TimeUnit.SECONDS).get(0).get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
return title;
}
private static class DownloadVideo {
static void downloadVideoInSeparateThreadHttpcat(String iVideoUrl,
String TARGET_DIR_PATH, String cypherUri) {
System.out.println("YurlWorldResource.downloadVideoInSeparateThreadHttpcat() - begin: "
+ iVideoUrl);
// VGet stopped working, so now we use a shell callout
Runnable r2 = getVideoDownloadJobHttpcat(iVideoUrl, TARGET_DIR_PATH);
executorService.submit(r2);
}
static Runnable getVideoDownloadJobHttpcat(final String iVideoUrl, final String targetDirPath
) {
Runnable videoDownloadJob = new Runnable() {
@Override
public void run() {
try {
Process p = new ProcessBuilder()
.directory(Paths.get(targetDirPath).toFile())
.command(
ImmutableList.of(YurlStash.YOUTUBE_DOWNLOAD,
iVideoUrl)).inheritIO().start();
p.waitFor();
if (p.exitValue() == 0) {
System.out
.println("YurlWorldResource.downloadVideoInSeparateThreadHttpcat() - successfully downloaded "
+ iVideoUrl);
// TODO : write successful video download to file
//writeSuccessToDb(iVideoUrl, id);
} else {
System.out
.println("YurlWorldResource.downloadVideoInSeparateThreadHttpcat() - error downloading "
+ iVideoUrl);
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
};
return videoDownloadJob;
}
}
@SuppressWarnings("unused")
private static class DownloadVideosBatch {
@SuppressWarnings("unused")
private static void downloadUndownloadedVideosInSeparateThread() {
new Thread() {
public void run() {
try {
downloadUndownloadedVideosBatch();
// I don't remember what this is for
while (true) {
try {
Thread.sleep(1000 * 60 * 60);
} catch (InterruptedException ie) {
}
}
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
private static void downloadUndownloadedVideosBatch() throws JSONException, IOException {
String query = "start root=node(37658)" + " match root-[CONTAINS*]->n"
+ " where not(has(n.downloaded_video)) OR n.downloaded_video is null "
+ " return n.title, n.downloaded_video, n.url, ID(n)" + " LIMIT 20;";
JSONObject results = execute(query, ImmutableMap.<String, Object> of(),
"downloadUndownloadedVideosBatch()");
JSONArray jsonArray = results.getJSONArray("data");
for (int i = 0; i < jsonArray.length(); i++) {
final JSONArray row = jsonArray.getJSONArray(i);
System.out.println("downloadUndownloadedVideosBatch() - iteration: " + row);
Object title = row.get(0);
Object downloaded = row.get(1);
Object url = row.get(2);
Object id = row.get(3);
if (downloaded == null || downloaded == JSONObject.NULL
|| "null".equals(downloaded)) {
if (url instanceof String) {
if (id instanceof Integer) {
System.out
.println("downloadUndownloadedVideosBatch() - Starting retroactively downloading: "
+ title);
try {
new SimpleTimeLimiter().callWithTimeout(new Callable<Object>() {
@Override
public Object call() throws Exception {
downloadVideo(row.getString(2), TARGET_DIR_PATH,
Integer.toString(row.getInt(3)));
return null;
}
}, 10, TimeUnit.SECONDS, true);
} catch (Exception e) {
System.out.println("Continuing");
continue;
}
System.out
.println("downloadUndownloadedVideosBatch() - Finished retroactively downloading: "
+ title);
} else {
System.out.println("id - " + id.getClass());
}
} else {
System.out.println("url - " + url.getClass());
}
} else {
Long downloaded1 = row.getLong(1);
System.out
.println("downloadUndownloadedVideosBatch() - Already downloaded: "
+ title + "\t" + downloaded1);
}
}
}
private static void downloadVideo(String iVideoUrl, String targetDirPath, String id) {
try {
// downloadVideo(iVideoUrl, targetDirPath);
// writeSuccessToDb(iVideoUrl, id);
} catch (JSONException e) {
System.out.println("UndownloadedVideosBatchJob.downloadVideo() - ERROR recording download in database");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
private static void writeSuccessToDb(final String iVideoUrl, final String id)
throws IOException {
System.out.println("writeSuccessToDb() - Attempting to record successful download in database...");
execute("start n=node({id}) WHERE n.url = {url} SET n.downloaded_video = {date}",
ImmutableMap.<String, Object> of("id", Long.valueOf(id), "url", iVideoUrl,
"date", System.currentTimeMillis()), "downloadVideo()");
System.out.println("writeSuccessToDb() - Download recorded in database");
}
@GET
@Path("updateImage")
@Produces("application/json")
public Response changeImage(
@QueryParam("url") String imageUrl,
@QueryParam("linkUrl") String iUrl,
@QueryParam("categoryId") String iCategoryId)
throws IOException, JSONException {
System.err.println("Yurl.YurlResource.changeImage() begin");
FileUtils.write(Paths.get(System.getProperty("user.home") + "/db.git/yurl_flatfile_db/yurl_master_images.txt").toFile(), iUrl + "::" + imageUrl + "\n", "UTF-8", true);
System.err.println("Yurl.YurlResource.changeImage() - success: " + iUrl + " :: " + imageUrl);
removeCategoryCacheAsync(iCategoryId);
// TODO: remove the Neo4j part
// JSONObject execute = execute(
// "START n=node({nodeIdToChange}) "
// + "SET n.user_image = {imageUrl}"
// + "RETURN n",
// ImmutableMap.<String, Object> of("nodeIdToChange",
// nodeIdToChange, "imageUrl",
// imageUrl), "changeImage()");
return Response
.ok()
.header("Access-Control-Allow-Origin", "*")
.entity(new JSONObject().toString()).type("application/json")
.build();
}
private void removeCategoryCacheAsync(final String iCategoryId) {
new Thread() {
@Override
public void run() {
removeCategoryCache(Integer.parseInt(iCategoryId));
}
}.start();
}
// I don't think this is of any use anymore
@GET
@Path("removeImage")
@Produces("application/json")
public Response removeImage(
@QueryParam("id") Integer nodeIdToChange,
@QueryParam("parentId") Integer parentId) throws Exception {
System.out.println("removeImage() - begin");
try {
Response r = Response
.ok()
.header("Access-Control-Allow-Origin", "*")
.entity(execute("START n=node({nodeIdToChange}) "
+ "REMOVE n.user_image, n.biggest_image " + "RETURN n",
ImmutableMap.<String, Object> of("nodeIdToChange", nodeIdToChange),
"removeImage()")).type("application/json").build();
if (parentId == null) {
System.err.println("YurlWorldResource.removeImage() - Warning: cache not updated, because parentId was not passed");
} else {
}
return r;
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
/** No existing relationships get deleted */
@GET
@Path("relateCategoriesToItem")
@Produces("application/json")
public Response relateCategoriesToItem(
@QueryParam("nodeId") Integer iNodeToBeTagged,
@QueryParam("newCategoryIds") String iCategoriesToBeAddedTo)
throws JSONException, IOException {
System.out.println("relateCategoriesToItem(): begin");
JSONArray theCategoryIdsToBeAddedTo = new JSONArray(
URLDecoder.decode(iCategoriesToBeAddedTo, "UTF-8"));
for (int i = 0; i < theCategoryIdsToBeAddedTo.length(); i++) {
System.out.println("relateCategoriesToItem(): "
+ theCategoryIdsToBeAddedTo.getInt(i) + " --> "
+ iNodeToBeTagged);
createNewRelation(theCategoryIdsToBeAddedTo.getInt(i),
iNodeToBeTagged);
}
return Response.ok().header("Access-Control-Allow-Origin", "*")
.entity(new JSONObject().toString())
.type("application/json").build();
}
private Integer createCategory(String iCategoryName) throws IOException {
// TODO: first check if there is already a node with this name,
// which is for re-associating the keycode with the category
return Integer
.parseInt((String) ((JSONArray) ((JSONArray) execute(
"CREATE (n { name : {name} , created: {created} , type :{type}}) " +
"RETURN id(n)",
ImmutableMap.<String, Object> builder()
.put("name", iCategoryName)
.put("type", "categoryNode")
.put("created", System.currentTimeMillis())
.build(), "createCategory()").get("data")).get(0)).get(0));
}
@GET
@Path("createAndRelate")
@Produces("application/json")
public Response createSubDirAndMoveItem(
@QueryParam("newParentName") String iNewParentName,
@QueryParam("childId") Integer iItemId,
@QueryParam("currentParentId") Integer iCurrentParentId)
throws JSONException, IOException {
System.out.println("createSubDirAndMoveItem() - begin");
JSONObject relateToExistingCategory = relateToExistingCategory(iItemId,
iCurrentParentId,
createNewCategoryUnderExistingCategory(iNewParentName, iCurrentParentId));
Response rResponse = Response.ok().header("Access-Control-Allow-Origin", "*")
.entity(relateToExistingCategory.toString())
.type("application/json").build();
return rResponse;
}
private Integer createNewCategoryUnderExistingCategory(String iNewParentName,
Integer iCurrentParentId) throws IOException {
// Create new category node
Integer theNewCategoryNodeIdString = createCategory(iNewParentName);
// Associate new category with current category
createNewRelation(iCurrentParentId, theNewCategoryNodeIdString);
return theNewCategoryNodeIdString;
}
@Deprecated // Uses Neo4j
private JSONObject relateToExistingCategory(Integer iItemId, Integer iCurrentParentId,
Integer theNewCategoryNodeIdString) throws IOException {
// delete any existing contains relationship with the
// specified existing parent (but not with all parents since we
// could have a many-to-one contains)
deleteExistingRelationship(iItemId, iCurrentParentId);
// Relate the item to the new category
JSONObject moveHelper = createNewRelation(theNewCategoryNodeIdString, iItemId);
return moveHelper;
}
/**
* This MOVES a node to a new subcategory. It deletes the relationship
* with the existing parent
* @throws InterruptedException
*/
@GET
@Path("relate")
@Produces("application/json")
public Response move(@QueryParam("parentId") final Integer iNewParentId,
@QueryParam("url") String iUrl,
@QueryParam("currentParentId") final Integer iCurrentParentId,
@QueryParam("created") Long created)
throws JSONException, IOException, InterruptedException {
System.out.println("Yurl.YurlResource.move() begin");
appendToTextFileSync(iUrl, iNewParentId.toString(), QUEUE_FILE, YurlStash.QUEUE_FILE_TXT_MASTER, created);
System.out.println("Yurl.YurlResource.move() 2");
appendToTextFileSync(iUrl, "-" + iCurrentParentId.toString(), QUEUE_DIR, YurlStash.QUEUE_FILE_TXT_DELETE, created);
System.out.println("Yurl.YurlResource.move() 4");
new Thread() {
@Override
public void run() {
removeCategoryCache(iNewParentId);
}
}.start();
new Thread() {
@Override
public void run() {
removeCategoryCache(iCurrentParentId);
}
}.start();
// JSONObject moveHelper = relateToExistingCategory(iChildId, iCurrentParentId,
// iNewParentId);
return Response.ok().header("Access-Control-Allow-Origin", "*")
.entity(new JSONObject().toString())
.type("application/json").build();
}
private void deleteExistingRelationship(Integer iChildId, Integer iCurrentParentId)
throws IOException {
execute("START oldParent = node({currentParentId}), child = node({childId}) MATCH oldParent-[r:CONTAINS]-child DELETE r",
ImmutableMap.<String, Object> builder()
.put("currentParentId", iCurrentParentId)
.put("childId", iChildId).build(), "deleteExistingRelationship()");
}