-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDescriptor.java
160 lines (142 loc) · 5.59 KB
/
Descriptor.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
/*
* Copyright (c) 2020 by Naohide Sano, All rights reserved.
*
* Programmed by Naohide Sano
*/
import java.io.IOException;
import java.net.URI;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import vavix.util.screenscrape.annotation.JsonPathParser;
import vavix.util.screenscrape.annotation.PlainInputHandler;
import vavix.util.screenscrape.annotation.Target;
import vavix.util.screenscrape.annotation.WebScraper;
import static java.nio.file.FileVisitResult.CONTINUE;
/**
* cloud drive description adder
*
* @author <a href="mailto:umjammer@gmail.com">Naohide Sano</a> (umjammer)
* @version 0.00 2020/09/08 umjammer initial version <br>
* @see "https://manablog.org/google-books-apis/"
*/
public final class Descriptor {
/**
* @param args 0: email, 1: dir
*/
public static void main(String[] args) throws IOException {
String email = args[0];
String cwd = args[1];
URI uri = URI.create("onedrive:///?id=" + email);
try (FileSystem fs = FileSystems.newFileSystem(uri, Collections.EMPTY_MAP)) {
Path root = fs.getPath(cwd);
FileSearcher fileSearcher = new FileSearcher();
Files.walkFileTree(root, fileSearcher);
// String query = args[2];
// Result result = query(query);
//System.err.println(result);
}
//Thread.getAllStackTraces().keySet().forEach(System.err::println);
}
@WebScraper(url = "https://www.googleapis.com/books/v1/volumes?q={0}",
parser = JsonPathParser.class,
input = PlainInputHandler.class,
value = "$..items",
isDebug = false,
isCollection = false)
public static class Result {
@Target(value = "$.volumeInfo.title")
String title;
@Target(value = "$.volumeInfo.authors", optional = true)
List<String> authors;
@Target(value = "$.volumeInfo.publishedDate", optional = true)
String publishedDate;
@Target(value = "$.volumeInfo.description", optional = true)
String description;
@Target(value = "$.volumeInfo.industryIdentifiers[0].identifier", optional = true)
String isbn10;
@Target(value = "$.volumeInfo.industryIdentifiers[1].identifier", optional = true)
String isbn13;
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Result [title=");
builder.append(title);
builder.append(", authors=");
builder.append(authors);
builder.append(", publishedDate=");
builder.append(publishedDate);
builder.append(", description=");
builder.append(description);
builder.append(", isbn10=");
builder.append(isbn10);
builder.append(", isbn13=");
builder.append(isbn13);
builder.append("]");
return builder.toString();
}
public String toFormatedString() {
StringBuilder builder = new StringBuilder();
builder.append(authors == null ? "[]" : authors);
builder.append(" ");
builder.append(title);
builder.append("\n");
builder.append(publishedDate == null ? "" : publishedDate);
builder.append("\n");
if (isbn10 != null) {
builder.append(isbn10);
builder.append("\n");
}
if (isbn13 != null) {
builder.append(isbn13);
builder.append("\n");
}
builder.append("\n");
builder.append(description == null ? "" : description);
return builder.toString();
}
}
static Result query(String query) throws IOException {
return WebScraper.Util.scrape(Result.class, query).get(0);
}
static class FileSearcher extends SimpleFileVisitor<Path> {
static final Pattern pattern = Pattern.compile("^\\(一般小説\\)\\s\\[(.+?)\\]\\s(.+?)(\\(.+?\\)){0,1}\\..+$");
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attr) {
if (attr.isRegularFile()) {
String filename = file.getFileName().toString();
//System.err.println(filename);
Matcher matcher = pattern.matcher(filename);
if (matcher.find()) {
//System.out.println(matcher.group(1) + " - " + matcher.group(2));
try {
byte[] bytes = (byte[]) Files.getAttribute(file, "user:description");
if (bytes == null || bytes.length == 0) {
Result result = query(matcher.group(1) + " " + matcher.group(2));
String description = result.toFormatedString();
System.out.println(description + "\n\n");
Files.setAttribute(file, "user:description", description.getBytes());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
return CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
System.err.println(file);
exc.printStackTrace();
return CONTINUE;
}
}
}