use of com.github.mob41.osums.search.SearchFilter in project osumer by mob41.
the class OsumsServer method searchSong.
public SearchResult searchSong(String keywords, SearchFilter[] filters, int items, int page) {
PreparedStatement pst = null;
try {
String sql = "SELECT * FROM songs";
if (keywords != null && !keywords.isEmpty()) {
sql += " WHERE (id LIKE ?" + " OR artist LIKE ?" + " OR title LIKE ?" + " OR creator LIKE ?" + " OR tags LIKE ?)";
}
sql += " LIMIT ? OFFSET ?";
pst = conn.prepareStatement(sql);
int offset = 0;
if (keywords != null && !keywords.isEmpty()) {
for (int i = 1; i <= 5; i++) {
pst.setString(i, "%" + keywords + "%");
}
offset = 5;
}
pst.setInt(offset + 1, items);
pst.setInt(offset + 2, items * (page - 1));
List<SongResult> list = new ArrayList<SongResult>();
ResultSet rows = pst.executeQuery();
while (rows.next()) {
String[] tags = rows.getString("tags").split(",");
list.add(new SongResult(rows.getInt("id"), rows.getString("rank"), rows.getString("artist"), rows.getString("title"), rows.getString("creator"), tags, rows.getInt("favourites"), rows.getInt("plays")));
}
SongResult[] out = new SongResult[list.size()];
for (int i = 0; i < out.length; i++) {
out[i] = list.get(i);
}
pst.close();
int totalPages = (int) Math.ceil(getTotalSongs() / items);
return new SearchResult(out, page, totalPages);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
use of com.github.mob41.osums.search.SearchFilter in project osumer by mob41.
the class OsumsOldParser method searchOnlineMaps.
@Override
public SearchResult searchOnlineMaps(String keywords, SearchFilter[] filters, int page) throws WithDumpException {
if (keywords == null) {
keywords = "";
}
String url = SEARCH_URL + keywords;
String rank = null;
if (filters != null) {
for (SearchFilter filter : filters) {
if (filter instanceof RankFilter) {
rank = ((RankFilter) filter).getRank();
}
url = filter.handleUrl(url);
}
}
if (rank == null) {
rank = RANK_ANY;
}
if (rank.equals(RANK_RANKED)) {
url += "&r=" + RANK_RANKED_INT;
} else if (rank.equals(RANK_PENDING)) {
url += "&r=" + RANK_PENDING_INT;
} else if (rank.equals(RANK_QUALIFIED)) {
url += "&r=" + RANK_QUALIFIED_INT;
} else if (rank.equals(RANK_LOVED)) {
url += "&r=" + RANK_LOVED_INT;
} else if (rank.equals(RANK_ANY)) {
url += "&r=" + RANK_ALL_INT;
} else {
System.out.println("Unknown beatmap rank: " + rank);
}
try {
String data = getHttpCookiedContent(url + "&page=" + page);
Document doc = Jsoup.parse(data);
Element pagination = doc.getElementsByClass("pagination").first();
String paginationText = pagination.html();
int totalPages = 1;
int brIndex = paginationText.indexOf("<br>");
if (brIndex == -1) {
throw new WithDumpException(paginationText, "Get br index in pagination", "Validate brIndex != -1", "Extract pagination text before br", "Could not find at least one br element!", false);
}
String pageText = paginationText.substring(0, brIndex);
// 11
int displayingIndex = pageText.indexOf("Displaying ");
// 4
int toIndex = pageText.indexOf(" to ");
// 4
int ofIndex = pageText.indexOf(" of ");
// 9
int resultIndex = pageText.indexOf(" results.");
if (displayingIndex == -1 || toIndex == -1 || ofIndex == -1 || resultIndex == -1) {
throw new WithDumpException(paginationText, "Get all text indexes", "Validate all text indexes != -1", "Extract result pages data", "The pagination is invalid or unregonized.", false);
}
// int nowPage = -1;
int currPageMaps = -1;
int totalResultMaps = -1;
try {
// nowPage = Integer.parseInt(pageText.substring(displayingIndex + 11, toIndex));
String currPageMapsStr = pageText.substring(toIndex + 4, ofIndex);
if (!currPageMapsStr.equals("many")) {
currPageMaps = Integer.parseInt(currPageMapsStr);
}
String totalResultMapsStr = pageText.substring(ofIndex + 4, resultIndex);
if (!totalResultMapsStr.equals("many")) {
totalResultMaps = Integer.parseInt(totalResultMapsStr);
}
} catch (NumberFormatException e) {
throw new WithDumpException(pageText, "Validate all text indexes != -1", "Extract result pages data", "Get page data", "Pagination number-text data cannot be decoded as number.", false, e);
}
if (totalResultMaps != -1 && currPageMaps != -1 && currPageMaps != totalResultMaps) {
currPageMaps -= 40 * (page - 1);
// System.out.println("Using method 1 to identify Total Pages");
// System.out.println("TRM/CPM: " + totalResultMaps + " / " + currPageMaps);
float calc = ((float) totalResultMaps / currPageMaps);
totalPages = (int) calc;
if (calc != totalPages) {
// System.out.println("Calc != totalPages: " + calc + " != " + totalPages);
totalPages++;
}
// System.out.println("Now total pages: " + totalPages);
} else {
// System.out.println("Using method 2 to identify Total Pages");
Elements pageLinkEls = pagination.children();
int size = pageLinkEls.size();
if (size < 2) {
totalPages = 1;
/*
throw new WithDumpException(pageText, "Get page links size",
"Validate children size >= 2", "Get last page link element",
"Invalid page! The page has less than 2 page links!", false);
*/
} else {
Element lastPageLinkEl = pageLinkEls.get(size - 2);
if (lastPageLinkEl != null) {
int lastPageNum = -1;
try {
lastPageNum = Integer.parseInt(lastPageLinkEl.html());
} catch (NumberFormatException e) {
throw new WithDumpException(pageText, "Get last page link element", "Parse last page number String to number", "Set as total page", "Pagination last page number-text data cannot be decoded as number.", false, e);
}
if (lastPageNum > totalPages) {
// System.out.println("Last page num is bigger than total pages: " + lastPageNum + " > " + totalPages);
totalPages = lastPageNum;
} else {
// System.out.println("Last page num is sammler than total pages: " + lastPageNum + " < " + totalPages);
}
}
}
}
SongResult[] results = parseSearch(rank, doc);
return new SearchResult(results, page, totalPages);
} catch (Exception e) {
throw new WithDumpException(url, "(Try&catch try) getting search result links", "Throw debuggable exception on catch", "(End of function)", "Error occurred when getting search result links", false, e);
}
}
use of com.github.mob41.osums.search.SearchFilter in project osumer by mob41.
the class OsumsServer method downloadBasicSongResults.
public void downloadBasicSongResults() throws Exception {
logger.info("[Stage 1/2] Downloading all results from all rank status...");
String[] ranks = { OsumsNewParser.RANK_LOVED, OsumsNewParser.RANK_PENDING, OsumsNewParser.RANK_QUALIFIED, OsumsNewParser.RANK_RANKED };
SearchResult result;
SearchFilter[] filters;
String rank;
List<SongResult> list = new ArrayList<SongResult>();
for (int i = 0; i < ranks.length; i++) {
rank = ranks[i];
logger.info("[Stage 1/2] Requesting results from \"" + rank + "...");
filters = new SearchFilter[] { new StringRankFilter(rank) };
int page = 1;
do {
logger.info("[Stage 1/2] [" + rank + "] Requesting results from page " + page + "...");
result = osums.searchOnlineMaps(null, filters, page);
if (result == null) {
logger.error("[Stage 1/2] [" + rank + "] No results returned at " + page + "!");
continue;
}
list.addAll(Arrays.asList(result.getResult()));
logger.info("[Stage 1/2] [" + rank + "] Completed fetching results from page " + page + ". (" + page + "/" + result.getTotalPages() + ") (" + (int) (Math.floor((page / (float) result.getTotalPages() + i) / (float) ranks.length * 100.0)) + "%)");
if (result.getCurrentPage() < result.getTotalPages()) {
page++;
}
} while (result != null && result.getCurrentPage() < result.getTotalPages());
}
logger.info("[Stage 2/2] Importing all results to database...");
int error = 0;
PreparedStatement pst = conn.prepareStatement("INSERT INTO songs (id, rank, artist, title, creator, tags, favourites, plays) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
SongResult r;
for (int i = 0; i < list.size(); i++) {
r = list.get(i);
logger.info("[Stage 2/2] Importing " + r.getId() + " (" + (i + 1) + "/" + list.size() + ") (" + (int) (Math.floor(i / (float) list.size() * 100)) + "%)");
pst.setInt(1, r.getId());
pst.setString(2, r.getRank());
pst.setString(3, r.getArtist());
pst.setString(4, r.getTitle());
pst.setString(5, r.getCreator());
pst.setString(6, join(r.getTags(), ","));
pst.setInt(7, r.getFavourites());
pst.setInt(8, r.getPlays());
try {
pst.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
error++;
}
}
pst.close();
logger.info("SQL Statements ended with " + error + " errors.");
logger.info("All basic song results have been downloaded and imported.");
prop.put(KEY_DOWNLOAD_BASIC_RESULTS, "false");
save();
}
Aggregations