Skip to content

Commit

Permalink
Merge pull request #6 from KaikyuDev/develop
Browse files Browse the repository at this point in the history
Added SINoAlice support
  • Loading branch information
KaikyuTest authored Aug 18, 2020
2 parents 2166fb7 + 89f0343 commit 269ac41
Show file tree
Hide file tree
Showing 15 changed files with 180 additions and 58 deletions.
2 changes: 1 addition & 1 deletion moe-moe-secretary.iml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Python 3.7 interpreter library" level="application" />
<orderEntry type="library" name="Maven: org.jsoup:jsoup:1.11.3" level="project" />
<orderEntry type="library" name="Maven: org.jsoup:jsoup:1.13.1" level="project" />
<orderEntry type="library" name="Maven: commons-io:commons-io:2.6" level="project" />
<orderEntry type="library" name="Maven: com.google.code.gson:gson:2.8.5" level="project" />
<orderEntry type="library" name="Maven: org.yaml:snakeyaml:1.25" level="project" />
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

<gson.version>2.8.5</gson.version>
<vorbisspi.version>1.0.3.3</vorbisspi.version>
<jsoup.version>1.11.3</jsoup.version>
<jsoup.version>1.13.1</jsoup.version>
<commons-io.version>2.6</commons-io.version>
<snakeyaml.version>1.25</snakeyaml.version>
<httpclient.version>4.5.11</httpclient.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,21 @@ public abstract class IWaifuAdapter {

private final long startTimeMillis;

protected String name;
private final String configName;

protected WaifuData data;

protected abstract WaifuData loadFromCustomSource() throws Exception;

public IWaifuAdapter(String name) throws IOException {
this.startTimeMillis = System.currentTimeMillis();
this.name = name;

Util.checkFolders(this.name); // Create folders
this.configName = name;
}

public final void init() {
try {
Util.checkFolders(getName());

if (hasSavedFile()) {
data = getDataFromFile();
} else {
Expand All @@ -53,6 +54,7 @@ public final void init() {
throw new StartFailedException("No images found for this waifu");
}

afterInit();
} catch (HttpStatusException e) {
String message = "Wiki status code: " + e.getStatusCode();
if (e.getStatusCode() == 404) {
Expand Down Expand Up @@ -151,11 +153,11 @@ public List<Dialog> getDialogs(String event) {
}

public String getShowableName() {
return this.name;
return this.configName;
}

public String getName() {
return name;
return configName;
}

public String onTouchEventKey() {
Expand All @@ -178,4 +180,5 @@ public String onLoginEventKey() {
return ON_LOGIN_EVENT_KEY;
}

public abstract void afterInit();
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public Arknights(String name) throws IOException {
super(name);
}

@Override
public void afterInit() {
// Empty impl
}

@Override
protected WaifuData loadFromCustomSource() throws InterruptedException {
System.out.println("Getting Arknights character data");
Expand Down Expand Up @@ -74,7 +79,7 @@ protected WaifuData loadFromCustomSource() throws InterruptedException {
}

System.out.println("Parsing data...");
Character character = characterMap.get().getWithName(name);
Character character = characterMap.get().getWithName(getName());
List<Skin> skins = skinData.get().ofCharacter(character);
List<String> skinsUrls = skins.stream().map(Skin::composeUrl).collect(Collectors.toList());
List<Dialog> dialogs = charwordMap.get().ofCharacter(character).parallelStream().map(Charword::asDialog).collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,17 @@ public AzurLane(String name) throws IOException {
super(name);
}

@Override
public void afterInit() {
// Empty impl
}

@Override
protected WaifuData loadFromCustomSource() throws IOException {
System.out.println("Getting ship quotes");
Document quotesDoc = Jsoup.connect(BASE_URL + "/" + name + "/Quotes").get();
Document quotesDoc = Jsoup.connect(BASE_URL + "/" + getName() + "/Quotes").get();
System.out.println("Getting ship images");
Document skinsDoc = Jsoup.connect(BASE_URL + "/" + name + "/Gallery").get();
Document skinsDoc = Jsoup.connect(BASE_URL + "/" + getName() + "/Gallery").get();
System.out.println("Parsing data...");
return new WaifuData(loadDialogs(quotesDoc), loadSkinUrls(skinsDoc));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public File(String shipName) throws IOException, StartFailedException {
super(shipName);
}

@Override
public void afterInit() {
// Empty impl
}

@Override
protected WaifuData loadFromCustomSource() {
throw new StartFailedException("File adapter needs a configuration folder, please click here to open the guide!",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,19 @@ public GirlsFrontline(String name) throws IOException {
super(name);
}

@Override
public void afterInit() {
// Empty impl
}

/**
* Loads data from Wiki, we MUST use it only once in a while
*/
protected WaifuData loadFromCustomSource() throws IOException {
System.out.println("Getting weapon home page");
Document mainDoc = Jsoup.connect(BASE_URL + "/wiki/" + name).get();
Document mainDoc = Jsoup.connect(BASE_URL + "/wiki/" + getName()).get();
System.out.println("Getting weapon quotes");
Document quotesDoc = Jsoup.connect(BASE_URL + "/wiki/" + name + "/Quotes").get();
Document quotesDoc = Jsoup.connect(BASE_URL + "/wiki/" + getName() + "/Quotes").get();
System.out.println("Parsing data...");

return new WaifuData(loadDialogs(quotesDoc), loadImageSources(mainDoc));
Expand Down Expand Up @@ -107,7 +112,7 @@ private List<String> loadImageSources(Document doc) {
.stream()
.map(e -> e.attr("href"))
.filter(a -> !a.contains("_S"))
.filter(a -> a.contains(name))
.filter(a -> a.contains(getName()))
.map(this::getFullImageLink)
.collect(Collectors.toList());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,19 @@ public Github(String name) throws IOException, URISyntaxException {
folderUrl = createWaifuFolderUrl();
}

@Override
public void afterInit() {
// Empty impl
}

private String createWaifuFolderUrl() throws URISyntaxException {
String repo = Settings.getGithubRepo();
String branch = Settings.getGithubBranch();
if (repo == null || branch == null) {
throw new StartFailedException("github.url or github.branch are not been set in the configuration file");
}
return new URIBuilder("https://raw.githubusercontent.com/")
.setPath(repo + "/" + branch + "/" + name)
.setPath(repo + "/" + branch + "/" + getName())
.build()
.normalize()
.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,15 @@ public MirageMemorial(String name) throws IOException {
super(name);
}

@Override
public void afterInit() {
// Empty impl
}

@Override
protected WaifuData loadFromCustomSource() throws IOException {
System.out.println("Getting servant from wiki");
Document skinsDoc = Jsoup.connect(BASE_URL + "Special:Images?file=" + name + ".png").get();
Document skinsDoc = Jsoup.connect(BASE_URL + "Special:Images?file=" + getName() + ".png").get();
return new WaifuData(Collections.emptyList(), loadSkinUrls(skinsDoc));
}

Expand All @@ -46,11 +51,11 @@ private List<String> loadSkinUrls(Document doc) throws IOException {
if (image != null) return Collections.singletonList(image);
}

throw new StartFailedException("Cannot find servant named " + name);
throw new StartFailedException("Cannot find servant named " + getName());
}

private String getImageUrlFromDocument(Document doc) {
Element image = Selector.select(String.format(IMAGE_SELECTOR, name), doc).first();
Element image = Selector.select(String.format(IMAGE_SELECTOR, getName()), doc).first();
if (image == null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,14 @@ public SIFIdol(String code) throws IOException {
super(code);
}

@Override
public void afterInit() {
// Empty impl
}

@Override
protected WaifuData loadFromCustomSource() throws IOException {
Document mainDoc = Jsoup.connect(String.format(CARD_URL, this.name)).get();
Document mainDoc = Jsoup.connect(String.format(CARD_URL, getName())).get();
this.idolName = getIdolName(mainDoc);

List<String> urls = getIdolSkinUrls(mainDoc);
Expand All @@ -65,7 +70,7 @@ private String elaborateDialog(String dialog, String info, boolean hasJap) {
elaboratedDialog = elaboratedDialog.split(" ", 2)[1];
}

if (info != null && !"".equals(info) && info.contains("#") && !info.contains(this.name)) {
if (info != null && !"".equals(info) && info.contains("#") && !info.contains(getName())) {
return null;
}
return elaboratedDialog;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.kitsunecode.mms.core.adapters.impl;

import com.kitsunecode.mms.core.adapters.IWaifuAdapter;
import com.kitsunecode.mms.core.entities.Settings;
import com.kitsunecode.mms.core.entities.WaifuData;
import com.kitsunecode.mms.core.entities.annotations.Adapter;
import com.kitsunecode.mms.core.entities.exceptions.StartFailedException;
import org.apache.http.client.utils.URIBuilder;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Selector;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Collections;

@Adapter
public class SinoAlice extends IWaifuAdapter {

private static final String META_OGI_SELECTOR = "meta[property='og:image']";

private static final String WIKI_URL = "https://sinoalice.game-db.tw/";

private final String waifuId;

public SinoAlice(String name) throws IOException {
super(name);
String charName = Settings.getWaifuName();

try {
Document mainDoc = Jsoup.connect("https://sinoalice.game-db.tw/characters/" + charName).get();
waifuId = Selector.select(META_OGI_SELECTOR, mainDoc)
.first()
.attr("content")
.split("\\.jpg")[0]
.split("CharacterIcon")[1];
} catch (Exception e) {
throw new StartFailedException("There was an error while trying to get the character.<br>\n" +
"Please click here and search for your waifu, paste her name in waifu.name",
"https://sinoalice.game-db.tw/characters/", e);
}
}

@Override
public void afterInit() {
// Empty impl
}

@Override
public String getName() {
return waifuId;
}

@Override
protected WaifuData loadFromCustomSource() throws URISyntaxException {
// Creates an URL like https://sinoalice.game-db.tw/images/character_l/245.png
String skinUrl = new URIBuilder(WIKI_URL).setPath("/images/character_l/" + getName() + ".png").build().toString();
return new WaifuData(Collections.emptyList(), Collections.singletonList(skinUrl));
}
}
Loading

0 comments on commit 269ac41

Please sign in to comment.