小修小改

main
lensfrex 3 years ago
parent fe53b334c7
commit 51545510a1
Signed by: lensfrex
GPG Key ID: 947ADABD8533C476
  1. 19
      Dogename/src/main/java/me/lensferno/dogename/data/Data.java
  2. 2
      Dogename/src/main/java/me/lensferno/dogename/data/History.java
  3. 21
      Dogename/src/main/java/me/lensferno/dogename/voice/TokenManager.java
  4. 2
      Dogename/src/main/java/me/lensferno/dogename/voice/VoicePlayer.java

@ -13,18 +13,16 @@ public class Data {
public static final int IGNORELIST_NAME_ONLY = 0; public static final int IGNORELIST_NAME_ONLY = 0;
public static final int IGNORELIST_NUMBER_ONLY = 1; public static final int IGNORELIST_NUMBER_ONLY = 1;
public static final int IGNORELIST_ALL = 2; public static final int IGNORELIST_ALL = 2;
File dataFile;
SecureRandom secRandom = new SecureRandom(); private final File dataFile = new File(FilePath.toSpecificPathForm("files/Namelist.data"));
Random random = new Random(); private final SecureRandom secRandom = new SecureRandom();
private final Random random = new Random();
private List<String> nameList; private List<String> nameList;
private final IgnoreList ignoreList = new IgnoreList(); private final IgnoreList ignoreList = new IgnoreList();
public Data() { public Data() {
dataFile = new File(FilePath.toSpecificPathForm("files/Namelist.data"));
try { try {
if (!dataFile.exists()) { if (!dataFile.exists()) {
dataFile.getParentFile().mkdirs(); dataFile.getParentFile().mkdirs();
dataFile.createNewFile(); dataFile.createNewFile();
@ -48,7 +46,6 @@ public class Data {
System.out.println("Failed to load data file."); System.out.println("Failed to load data file.");
e.printStackTrace(); e.printStackTrace();
} }
ignoreList.readIgnoreList(); ignoreList.readIgnoreList();
} }
@ -90,9 +87,7 @@ public class Data {
System.out.println("error in import namelist:" + e); System.out.println("error in import namelist:" + e);
e.printStackTrace(); e.printStackTrace();
} }
} }
} }
public void makeMass() { public void makeMass() {
@ -209,7 +204,7 @@ public class Data {
return ignoreList.getNumberIgnoreListSize(); return ignoreList.getNumberIgnoreListSize();
} }
class IgnoreList { static class IgnoreList {
private final File nameIgnoreFile = new File(FilePath.toSpecificPathForm("files/IgnoredNameList.data")); private final File nameIgnoreFile = new File(FilePath.toSpecificPathForm("files/IgnoredNameList.data"));
private final File numbIgnoreFile = new File(FilePath.toSpecificPathForm("files/IgnoredNumberList.data")); private final File numbIgnoreFile = new File(FilePath.toSpecificPathForm("files/IgnoredNumberList.data"));
@ -251,7 +246,7 @@ public class Data {
public void readIgnoreList() { public void readIgnoreList() {
readNameIgnoreList(); readNameIgnoreList();
readNumberIgnoreList(); readNumberIgnoreList();
System.out.println(String.format("There are %d names and %d numbers ignored", ignoreNameList.size(), ignoreNumberList.size())); System.out.printf("There are %d names and %d numbers ignored", ignoreNameList.size(), ignoreNumberList.size());
} }
private void readNameIgnoreList() { private void readNameIgnoreList() {

@ -16,7 +16,6 @@ public class History {
File historyFile = new File(HISTORY_FILE); File historyFile = new File(HISTORY_FILE);
try { try {
if (!historyFile.exists()) { if (!historyFile.exists()) {
historyFile.getParentFile().mkdirs(); historyFile.getParentFile().mkdirs();
historyFile.createNewFile(); historyFile.createNewFile();
@ -57,7 +56,6 @@ public class History {
File historyFile = new File(HISTORY_FILE); File historyFile = new File(HISTORY_FILE);
try { try {
if (!historyFile.exists()) { if (!historyFile.exists()) {
historyFile.createNewFile(); historyFile.createNewFile();
} }

@ -32,7 +32,6 @@ public class TokenManager {
if (netAvailable()) { if (netAvailable()) {
refreshToken(); refreshToken();
} }
if (checkTokenAvailable() != 0) { if (checkTokenAvailable() != 0) {
tokenStatus = TOKEN_BAD; tokenStatus = TOKEN_BAD;
} }
@ -55,19 +54,13 @@ public class TokenManager {
if (tokenFile.exists()) { if (tokenFile.exists()) {
loadToken(); loadToken();
updateTokenStatus(checkTokenAvailable()); updateTokenStatus(checkTokenAvailable());
} else { } else {
if (netAvailable()) { if (netAvailable()) {
refreshToken(); refreshToken();
updateTokenStatus(checkTokenAvailable()); updateTokenStatus(checkTokenAvailable());
} else { } else {
tokenStatus = TOKEN_BAD; tokenStatus = TOKEN_BAD;
} }
} }
} }
@ -98,17 +91,10 @@ public class TokenManager {
return 0; return 0;
} }
void fetchToken() { void fetchToken() {
try { try {
token = new Gson().fromJson( token = new Gson().fromJson(
NetworkUtil.getHtml( NetworkUtil.getHtml(String.format("%s?grant_type=client_credentials&client_id=%s&client_secret=%s", TOKEN_API_URL, API_KEY, SEC_KEY), true), Token.class);
TOKEN_API_URL
+ "?grant_type=client_credentials&client_id=" + API_KEY
+ "&client_secret=" + SEC_KEY,
true)
, Token.class);
token.setExpTime(); token.setExpTime();
} catch (Exception e) { } catch (Exception e) {
System.out.println("Error to get Token:" + e); System.out.println("Error to get Token:" + e);
@ -118,7 +104,6 @@ public class TokenManager {
private boolean netAvailable() { private boolean netAvailable() {
try { try {
URL sourcesURL = new URL("http://www.baidu.com"); URL sourcesURL = new URL("http://www.baidu.com");
HttpURLConnection connection = (HttpURLConnection) sourcesURL.openConnection(); HttpURLConnection connection = (HttpURLConnection) sourcesURL.openConnection();
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36"); connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36");
@ -136,9 +121,8 @@ public class TokenManager {
} }
private void loadToken() { private void loadToken() {
ObjectInputStream ois;
try { try {
ois = new ObjectInputStream(new FileInputStream(tokenFile)); ObjectInputStream ois = new ObjectInputStream(new FileInputStream(tokenFile));
this.token = (Token) ois.readObject(); this.token = (Token) ois.readObject();
ois.close(); ois.close();
} catch (Exception e) { } catch (Exception e) {
@ -148,7 +132,6 @@ public class TokenManager {
} }
private void writeToken() { private void writeToken() {
try { try {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(tokenFile)); ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(tokenFile));
oos.writeObject(token); oos.writeObject(token);

@ -18,7 +18,7 @@ public class VoicePlayer {
Token token; Token token;
StreamPlayer streamPlayer = new StreamPlayer(); StreamPlayer streamPlayer = new StreamPlayer();
private VoiceConfig voiceConfig = null; private final VoiceConfig voiceConfig;
public VoicePlayer(Token token, VoiceConfig voiceConfig) { public VoicePlayer(Token token, VoiceConfig voiceConfig) {
this.token = token; this.token = token;

Loading…
Cancel
Save