-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuizletAPI.java
191 lines (175 loc) · 5.44 KB
/
QuizletAPI.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
186
187
188
189
190
191
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Arrays;
/**
* The QuizletAPI was made with the intention of making
* connecting to quizlet sets an easier task to accomplish.
*
* @author Amin
* @version 1.0
* @since 2017-09-17
*/
public class QuizletAPI {
private static String CLIENT_ID;
private static String SET_ID;
private ArrayList<String> cards;
private String title;
private ArrayList<String> terms, definitions, image, rank;
public QuizletAPI(String clientID, String setID){
CLIENT_ID = clientID;
SET_ID = setID;
String base = "https://api.quizlet.com/2.0/sets/" + SET_ID;
String title_url = base + "?client_id=" + CLIENT_ID;
cards = new ArrayList<>();
String title_text = getTextFromWebsite(title_url);
cards.addAll(Arrays.asList(title_url.split(Choice.TERM.getChoice())[1].replace("[", "")
.replace("]", "")
.replace("{","").split("},")));
terms = getItemFromCards(Choice.TERM);
definitions = getItemFromCards(Choice.DEFINITION);
image = getItemFromCards(Choice.IMAGE);
rank = getItemFromCards(Choice.RANK);
title = title_text.split("title")[1].split("\",")[0].substring(3);
}
/**
* This method returns the Client ID being used
* to access the set.
* @return Returns the CLIENT_ID
*/
public String getClientID() {
return CLIENT_ID;
}
/**
* This method returns the ID of the set.
* @return Returns the SET_ID
*/
public String getSetID() {
return SET_ID;
}
private String getTextFromWebsite(String url){
URL uri;
try {
uri = new URL(url);
URLConnection ec = uri.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
ec.getInputStream(), "UTF-8"));
String inputLine;
StringBuilder a = new StringBuilder();
while ((inputLine = in.readLine()) != null)
a.append(inputLine);
in.close();
return a.toString();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private ArrayList<String> getItemFromCards(Choice choice){
ArrayList<String> listOfChoice = new ArrayList<>();
for (String card : cards) {
String item = card.split(choice.getChoice())[1].split(",\"")[0].replace("\"","");
listOfChoice.add(decode(item));
}
return listOfChoice;
}
/**
* This method returns the terms within
* the set.
* @return The list of terms.
*/
public ArrayList<String> getTerms() {
return terms;
}
/**
* This method returns the definitions
* within the set.
* @return The list of definitions.
*/
public ArrayList<String> getDefinitions() {
return definitions;
}
/**
* This method returns definition of a term.
* @param term The case-sensitive term within the set.
* @return The definition of a term.
*/
public String getDefinitionFromTerm(String term){
return getDefinitions().get(getTerms().indexOf(term));
}
/**
* This method returns the links of the
* images being used within the set.
* @return The list of images.
*/
public ArrayList<String> getImages() {
return image;
}
/**
* @return The name of the quizlet set.
*/
public String getTitle()
{
return title;
}
/**
* This method returns the ranks within
* the set.
* @return The String ArrayList of the ranks.
*/
public ArrayList<String> getRanks() {
return rank;
}
/**
* This method returns the index of a term
* if the given non case-sensitive parameter
* exists within the terms of the set.
* @param checking The term that is being checked for.
* @return -1 if the term doesn't exist, else the index of the term.
*/
public int isTermInSet(String checking){
for (String term : getTerms()) {
if (term.equalsIgnoreCase(decode(checking))) {
return getTerms().indexOf(term);
}
}
return -1;
}
public enum Choice{
ID("\"id\":"),
TERM("\"term\":"),
DEFINITION("\"definition\":"),
IMAGE("\"image\":"),
RANK("\"rank\":");
String choice;
Choice(String choice) {
this.choice=choice;
}
private String getChoice(){
return choice;
}
}
private static String decode(final String in)
{
String working = in;
int index;
index = working.indexOf("\\u");
while(index > -1)
{
int length = working.length();
if(index > (length-6))break;
int numStart = index + 2;
int numFinish = numStart + 4;
String substring = working.substring(numStart, numFinish);
int number = Integer.parseInt(substring,16);
String stringStart = working.substring(0, index);
String stringEnd = working.substring(numFinish);
working = stringStart + ((char)number) + stringEnd;
index = working.indexOf("\\u");
}
return working;
}
}