-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.java
185 lines (149 loc) · 8.2 KB
/
Main.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
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
package wallabag;
import wallabag.apiwrapper.*;
import wallabag.apiwrapper.exceptions.UnsuccessfulResponseException;
import wallabag.apiwrapper.models.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.*;
public class Main {
private static final Logger LOG = LoggerFactory.getLogger(Main.class);
public static void main(String[] args) {
String baseUrl = "https://wallabag.example.com"; // point to your wallabag instance
String username = "";
String password = "";
String clientID = "";
String clientSecret = "";
String refreshToken = "";
String accessToken = ""; // provide at least the access token
WallabagService service = WallabagService.instance(baseUrl, new BasicParameterHandler(
username, password, clientID, clientSecret, refreshToken, accessToken) {
@Override
public boolean tokensUpdated(TokenResponse token) {
LOG.info("Got token: " + token);
return super.tokensUpdated(token);
}
});
try {
String serverVersion = service.getVersion();
System.out.println("Server version: " + serverVersion);
System.out.println("Info: " + service.getInfo());
String testUrl = "https://doc.wallabag.org/en/";
Article article = service.addArticleBuilder(testUrl)
.starred(true)
.tag("new_test_tag1").tag("new_test_tag2").tag("test_rm")
.title("Custom title test")
.execute();
System.out.println("Reloaded article is not null: " + (service.reloadArticle(article.id) != null));
System.out.println("Exported as text: " + service.exportArticle(
article.id, WallabagService.ResponseFormat.TXT).string());
article = service.modifyArticleBuilder(article.id)
.title("Modified title for API documentation article").execute();
System.out.println("Modified article title: " + article.title);
System.out.println("Exists: " + service.articleExists(testUrl));
if (CompatibilityHelper.isArticleExistsWithIdSupported(serverVersion)) {
System.out.println("Exists id: " + service.articleExistsWithId(testUrl));
}
List<String> urls = new ArrayList<>();
urls.add(testUrl);
urls.add("http://google.com");
for (Map.Entry<String, Boolean> entry : service.articlesExistByUrls(urls).entrySet()) {
System.out.println("URL: " + entry.getKey() + ", exists: " + entry.getValue());
}
if (CompatibilityHelper.isArticleExistsWithIdSupported(serverVersion)) {
for (Map.Entry<String, Integer> entry : service.articlesExistByUrlsWithId(urls).entrySet()) {
System.out.println("URL: " + entry.getKey() + ", id: " + entry.getValue());
}
}
BatchExistQueryBuilder existQueryBuilder = service.getArticlesExistQueryBuilder();
existQueryBuilder.addUrl(testUrl);
existQueryBuilder.addUrl("http://google.com");
for (Map.Entry<String, Boolean> entry : existQueryBuilder.execute().entrySet()) {
System.out.println("URL: " + entry.getKey() + ", exists: " + entry.getValue());
}
System.out.println("Article title: " + service.getArticle(article.id).title);
List<String> additionalTags = new ArrayList<>();
additionalTags.add("additional_tag1");
additionalTags.add("additional_tag2");
additionalTags.add("additional_tag3");
article = service.addTags(article.id, additionalTags);
int tagIdForRemoval = -1;
for (Tag tag : article.tags) {
if ("additional_tag2".equals(tag.label)) {
tagIdForRemoval = tag.id;
break;
}
}
if (tagIdForRemoval >= 0) System.out.println("Deleted tag from article: "
+ service.deleteTag(article.id, tagIdForRemoval).title);
List<Tag> tags = service.getTags();
int idForRemoval = -1;
for (Tag tag : tags) {
System.out.println("Tag id: " + tag.id);
System.out.println("Tag label: " + tag.label);
if ("test_rm".equals(tag.label)) idForRemoval = tag.id;
}
if (idForRemoval >= 0) System.out.println("Deleted tag label: " + service.deleteTag(idForRemoval).label);
Annotation.Range range = new Annotation.Range();
range.startOffset = 9;
range.endOffset = 12;
range.start = "/p[3]";
range.end = "/ul[1]/li[1]";
List<Annotation.Range> ranges = new ArrayList<>(1);
ranges.add(range);
Annotation annotation = service.addAnnotation(article.id, ranges, "Test annotation from API", null);
System.out.println("Updated annotation text: " + service.updateAnnotation(annotation.id, "Test updated annotation from API").text);
Annotations annotations = service.getAnnotations(article.id);
for (Annotation an : annotations.rows) {
System.out.println("Annotation id: " + an.id);
System.out.println("Annotation text: " + an.text);
System.out.println("Annotation quote: " + an.quote);
}
System.out.println("Deleted annotation text: " + service.deleteAnnotation(annotation.id).text);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, 2000);
article = service.modifyArticleBuilder(article.id)
.content("Modified content")
.language("eo")
.previewPicture("https://example.com/pic")
.publishedAt(calendar.getTime())
.author("author1").author("author2")
.originUrl("https://example.com/origin")
.isPublic(true)
.execute();
System.out.println(String.format("Modified article content: %s, language: %s, preview picture: %s, " +
"publishedAt: %s, authors: %s, originUrl: %s, isPublic: %s, publicUid: %s",
article.content, article.language, article.previewPicture, article.publishedAt, article.authors,
article.originUrl, article.isPublic, article.publicUid));
service.deleteArticle(article.id);
System.out.println("Deleted article");
article = service.addArticleBuilder("https://example.com/test")
.title("Test article")
.content("Test content")
.language("eo")
.previewPicture("https://example.com/pic")
.publishedAt(calendar.getTime())
.author("author1").author("author2")
.originUrl("https://example.com/origin")
.execute();
System.out.println(String.format("Test article title: %s, content: %s, language: %s, preview picture: %s, " +
"publishedAt: %s, authors: %s, originUrl: %s",
article.title, article.content, article.language, article.previewPicture, article.publishedAt,
article.authors, article.originUrl));
service.deleteArticle(article.id);
System.out.println("Deleted article");
Articles articles = service.getArticlesBuilder().perPage(3).execute();
System.out.println("Items length: " + articles.embedded.items.size());
for (Article a : articles.embedded.items) {
System.out.println("ID: " + a.id);
System.out.println("Title: " + a.title);
System.out.println("Is archived: " + a.archived);
System.out.println("Created at: " + a.createdAt);
}
} catch (IOException e) {
LOG.error("IOException", e);
} catch (UnsuccessfulResponseException e) {
LOG.error("UnsuccessfulResponseException", e);
}
}
}